如何在Google Scripts中以预填充URL为输入提交Google Form表单响应?
Yes, this is totally feasible! A Google Form pre-filled URL encodes all the response data in its query parameters, which we can parse and use in Google Apps Script to programmatically submit a new response matching those pre-filled values.
Here's a step-by-step breakdown and example code to achieve this:
Step 1: Understand the pre-filled URL structure
A typical pre-filled URL looks like this:https://docs.google.com/forms/d/{FORM_ID}/viewform?entry.{ITEM_ID_1}=Value1&entry.{ITEM_ID_2}=Value2
Each entry.{ITEM_ID} parameter corresponds to a form field, with the value being the pre-filled content.
Step 2: Write the Google Script function
This function takes a pre-filled URL as input, parses its parameters, and submits the response to the form:
function submitFormFromPrefilledUrl(prefilledUrl) { try { // Parse the URL to extract form ID and query parameters const url = new URL(prefilledUrl); const formId = url.pathname.split('/')[3]; // Extract form ID from the URL path const form = FormApp.openById(formId); // Get all query parameters from the URL const params = new URLSearchParams(url.search); const itemResponses = []; // Iterate over each form item to match with URL parameters form.getItems().forEach(item => { const itemId = item.getId().toString(); const paramKey = `entry.${itemId}`; if (params.has(paramKey)) { const value = params.get(paramKey); // Handle different form item types switch(item.getType()) { case FormApp.ItemType.TEXT: case FormApp.ItemType.PARAGRAPH_TEXT: case FormApp.ItemType.MULTIPLE_CHOICE: case FormApp.ItemType.DROPDOWN: case FormApp.ItemType.LINEAR_SCALE: itemResponses.push(item.asTextItem().createResponse(value)); break; case FormApp.ItemType.CHECKBOX: // Checkbox values are comma-separated in pre-filled URLs const checkboxValues = value.split(',').map(val => val.trim()); itemResponses.push(item.asCheckboxItem().createResponse(checkboxValues)); break; case FormApp.ItemType.DATE: itemResponses.push(item.asDateItem().createResponse(new Date(value))); break; case FormApp.ItemType.TIME: // Split time into hours and minutes (assuming format like "14:30") const [hours, minutes] = value.split(':').map(Number); const time = new Date(); time.setHours(hours, minutes, 0); itemResponses.push(item.asTimeItem().createResponse(time)); break; // Add more cases for other item types as needed default: console.log(`Skipping unsupported item type: ${item.getType()}`); break; } } }); // Create and submit the form response const formResponse = form.createResponse(); itemResponses.forEach(response => formResponse.withItemResponse(response)); formResponse.submit(); console.log("Response submitted successfully!"); return true; } catch (error) { console.error(`Error submitting form: ${error.message}`); return false; } }
Step 3: How to use the function
- Open your Google Apps Script project (or create a new one).
- Paste the code above.
- Call the function with your pre-filled URL, e.g.:
submitFormFromPrefilledUrl("https://docs.google.com/forms/d/your-form-id/viewform?entry.123456=Test+Name&entry.789012=test@example.com");
Important Notes
- Permissions: The user running the script must have at least "View" access to the form (if it's public) or appropriate permissions if it's restricted. For restricted forms, the script runner needs to be an editor or have submission access.
- Required Fields: Ensure all required form fields are present in the pre-filled URL's parameters. Missing required fields will cause the submission to fail.
- Item Type Handling: The example covers common item types, but you may need to add more cases for less common types (like grid questions) depending on your form.
- URL Validation: Make sure the input URL is a valid Google Form pre-filled URL. Invalid URLs will throw errors.
Content of the question originates from Stack Exchange, question author JohnTyler




