I want to create a survey using Google Froms, but only limited people who have the codes can submit the form and do the survey. I have already created a list of codes inside a Google Sheet. When I try to link the Form and Sheet by using Apps Script, it does not work somehow.
Wonder how should i fix my code? or is there another way or plugins that are useful for setting restrictions of filling the form?
function checkCodes(e) {
var form = FormApp.getActiveForm();
var response = e.response;
var itemResponses = response.getItemResponses();
var codeQuestionIndex = 0;
var codeQuestion = itemResponses[codeQuestionIndex].getResponse();
var sheet = SpreadsheetApp.openByUrl('MY_SHEET_LINK');
var codeSheet = sheet.getSheetByName('Sheet1');
var codeRange = codeSheet.getRange('A:A'); // all the codes are in the column A
var codes = codeRange.getValues().flat();
if (!codes.includes(codeQuestion)) {
form.setAcceptingResponses(false);
}
}
2
Answers
It is not possible to restrict access to Google forms only for specific accounts or based on tokens/correct answers. You can however filter the responses post submission of responses and discard the responses not submitted with a proper access token.
You can run a script after a response has been submitted, but not while a user is entering data in a form. But you do not need a script in the first place. In the Google Forms editor:
Enter password
as the first thing in the form.shibboleth
(or another password of your choosing).Incorrect password.
This is an important step — the default warning message would reveal the correct password, which you do not want.When a user opens the form, they will only see the password prompt, and will need to enter the correct password to proceed past that prompt.