skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    1. Insert a short answer question titled Enter password as the first thing in the form.
    2. Click More ︙ and choose Response validation.
    3. Select Text > Contains > shibboleth (or another password of your choosing).
    4. In the Custom error text field, enter Incorrect password. This is an important step — the default warning message would reveal the correct password, which you do not want.
    5. Click Add section 🟰.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search