Google Form选“Submit another response”时如何传递名称数据并提供可选功能
Great question! Since you're new to Google Apps Script for Forms, let's break down a straightforward, real-time solution that avoids waiting on spreadsheet data—perfect for your use case where you want to prefill the name immediately after submission, with an option to skip it.
Core Idea
Instead of using Google Forms' default "Submit another response" link, we'll create a custom confirmation page using Apps Script. This page will generate two links: one that pre-fills the name from the last submission, and another that opens a fresh, empty form. This happens instantly when the user submits, no spreadsheet delays involved.
Step 1: Prep Your Form
First, make sure your form has a short-text field titled something like "姓名" (adjust this to match your actual field name). You'll need two key pieces of info later:
- Your Form ID: Found in the form URL, between
d/e/and/viewform(e.g.,abc123XYZinhttps://docs.google.com/forms/d/e/abc123XYZ/viewform) - The entry ID for your name field: We'll get this with a quick script.
Step 2: Get the Name Field's Entry ID
To find the unique ID for your name field, run this small script in the Apps Script editor:
function getFormFieldIDs() { const form = FormApp.openById("YOUR_FORM_ID"); // Replace with your Form ID const items = form.getItems(); items.forEach(item => { // Match the exact title of your name field here if (item.getTitle() === "姓名") { console.log(`Name field entry ID: entry.${item.asTextItem().getId()}`); } }); }
- Open your form → Click the three dots (⋮) → Select "Script editor"
- Paste the code, replace
YOUR_FORM_ID, and run the function (click the play button). - Check the "Logs" (View → Logs) to get the entry ID (looks like
entry.123456789). Save just the numeric part (123456789) for later.
Step 3: Build the Submission Trigger Script
This script will run every time someone submits the form. It grabs the submitted name, generates pre-filled and fresh form links, and replaces the default confirmation message with a custom HTML page.
function onFormSubmit(e) { // 1. Fetch the submitted name from the response const nameResponse = e.response.getItemResponses().find(response => { // Match your name field's exact title here return response.getItem().getTitle() === "姓名"; }); const submittedName = nameResponse ? nameResponse.getResponse() : ""; // 2. Replace these with your actual Form ID and name field entry ID (numeric part) const FORM_ID = "YOUR_FORM_ID"; const NAME_FIELD_ENTRY_ID = "123456789"; // Example: from entry.123456789 // 3. Generate pre-filled and fresh form links const prefilledFormUrl = `https://docs.google.com/forms/d/e/${FORM_ID}/viewform?entry.${NAME_FIELD_ENTRY_ID}=${encodeURIComponent(submittedName)}`; const freshFormUrl = `https://docs.google.com/forms/d/e/${FORM_ID}/viewform`; // 4. Create a custom confirmation page with both options const customConfirmation = HtmlService.createHtmlOutput(` <!DOCTYPE html> <html> <body style="font-family: Arial, sans-serif; max-width: 600px; margin: 2rem auto; padding: 0 1rem;"> <h2>提交成功!</h2> <p>感谢你提交参赛信息。</p> <div style="margin-top: 2rem;"> <a href="${prefilledFormUrl}" target="_blank" style="display: inline-block; padding: 0.8rem 1.5rem; background: #4285F4; color: white; text-decoration: none; border-radius: 4px; margin-right: 1rem;">提交另一条带姓名的信息</a> <a href="${freshFormUrl}" target="_blank" style="display: inline-block; padding: 0.8rem 1.5rem; background: #34A853; color: white; text-decoration: none; border-radius: 4px;">提交新的参赛信息</a> </div> </body> </html> `); // 5. Replace the default confirmation message with our custom page e.response.setConfirmationMessage(customConfirmation.getContent()); }
Step 4: Set Up the Trigger
To make the script run automatically when someone submits the form:
- In the Apps Script editor, click the clock icon (Triggers) in the left sidebar.
- Click "Add Trigger" in the bottom right.
- Configure the trigger:
- Choose function:
onFormSubmit - Choose deployment: Head
- Select event source:
From form - Select event type:
On form submit
- Choose function:
- Click "Save" and authorize the script when prompted (follow the steps to allow permissions—this is safe, you're granting access to your own form).
How It Works
- When a user submits the form, the
onFormSubmitfunction instantly grabs their name from the response. - It generates a pre-filled link by appending the encoded name to the form URL (using
encodeURIComponentto handle special characters like spaces or Chinese characters). - The custom confirmation page shows both options, so users can choose to reuse their name or start fresh.
Bonus Tips
- Customize the HTML in the script to match your brand (add logos, adjust colors, or add extra instructions).
- If you ever need to modify the name field, just update the field title in the script and re-run
getFormFieldIDsto get the new entry ID.
内容的提问来源于stack exchange,提问作者Pete Banks

