Windows 10环境下Google Forms限制重复链接提交实现方案咨询(新手级)
Absolutely! You can definitely set this up using Google Apps Script—no fancy tech background needed, even as a rookie. Let me break down exactly how to make your Google Form block duplicate restaurant links and show that specific error message when someone tries to submit one.
First, make sure your Google Form has a Short answer field where people input the restaurant link (name it something clear like "Restaurant Link"). Also, confirm your form is linked to a Google Sheet (this happens automatically when you first create responses, but you can check by clicking the "Responses" tab in the form and looking for the green sheet icon).
In your Google Form, click the three dots in the top-right corner, then select "Script editor". This will open a new tab with a blank script file named Code.gs.
Delete the default myFunction() code, then paste in this script. I’ve added comments so you know what parts to adjust for your setup:
function validateDuplicateLink(e) { // Get the form response data var response = e.response; var itemResponses = response.getItemResponses(); // Replace this with the EXACT title of your link field in the form var linkFieldTitle = "Restaurant Link"; var submittedLink = ""; // Pull out the link the user just submitted for (var i = 0; i < itemResponses.length; i++) { var item = itemResponses[i]; if (item.getItem().getTitle() === linkFieldTitle) { submittedLink = item.getResponse().trim(); break; } } // If no link was entered, skip the check if (submittedLink === "") return; // Access the linked Google Sheet var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Adjust this range to match where your links are stored in the sheet: // The format is getRange(startRow, columnNumber, numberOfRows, numberOfColumns) // Example: If links are in column 2 and start at row 2 (row 1 is headers), use getRange(2, 2, sheet.getLastRow()-1, 1) var allSubmittedLinks = sheet.getRange(2, sheet.getLastColumn(), sheet.getLastRow()-1, 1).getValues(); // Check if the submitted link already exists for (var j = 0; j < allSubmittedLinks.length; j++) { if (allSubmittedLinks[j][0].trim() === submittedLink) { // Throw the error message you want users to see throw new Error("That link has already been submitted"); } } } // This triggers the validation every time someone submits the form function onFormSubmit(e) { validateDuplicateLink(e); }
Quick Tweaks You Need to Make:
- Change
linkFieldTitleto match the exact name of your link input field in the form (capitalization and spaces matter!). - Adjust the
getRangeline if your links aren’t in the last column of the sheet. For example, if your links are in column 3, replacesheet.getLastColumn()with3.
The script needs to run every time someone submits the form. Here’s how to set that up:
- In the Script Editor, click the clock icon (Triggers) on the left sidebar.
- Click Add Trigger in the bottom-right corner.
- Configure the trigger like this:
- Choose which function to run:
onFormSubmit - Choose which deployment to run:
Head - Select event source:
From form - Select event type:
On form submit
- Choose which function to run:
- Click Save. You’ll need to authorize the script to access your form and sheet—this is safe (it’s your own script!). If Google says "Unverified app", click Advanced then Go to [Your Script Name] (unsafe) to complete the authorization.
Submit a link through the form, then try submitting the same link again. You should see the error message "That link has already been submitted" pop up, and the duplicate response won’t be saved to the sheet.
内容的提问来源于stack exchange,提问作者TheBoldRoller

