Ashirwada Manamudali
Ashirwada Manamudali

Reputation: 31

Allowing only one answer in Google forms

Hi I want to create a Google form which i can collect student data, And i want to make a way to allow my students to only enter one answer one time

in example I want to use this method in the question which asks for the admission number, admission number is a uniqe number which will not be repeated. So if a student enter an admission number that is already entered , I want them to see an error message with "The admission number has been already used"

Upvotes: 1

Views: 2121

Answers (1)

Iamblichus
Iamblichus

Reputation: 19309

Issue:

Avoid multiple form responses to be submitted with the same value for a specific form item.

Workflow:

  • In order to avoid certain responses to be submitted, you have to use some kind of data validation for the Admission number item, as shown in Set rules for your form.
  • This data validation should be updated every time a form response is submitted, since the newly submitted Admission number should be forbidden in future responses.
  • In order to update this validation every time the form is submitted, you could use an Apps Script onFormSubmit() trigger, which would then fire a function on every form submission. Let's call this function updateDataValidation.
  • The onFormSubmit trigger can be installed either manually, following these steps, or programmatically, via executing the function createTrigger from the code sample below once.

Every time updateDataValidation fires, it should do the following:

  • Find all the previous responses for your Admission number item. You could first retrieve all form responses via Form.getResponses() and then find the responses related to your item by checking its title (see Item.getTitle()).
  • Transform the array of admission numbers retrieved in previous step into a regex pattern that can be used for the data validation, which could be like this: ^{number1}$|^{number2}$|^{number3}$....
  • Use TextValidation in order to update the validation rules for your item.

Code sample:

function updateDataValidation(e) {
  const itemTitle = "Admission number"; // Name of the form item. Change accordingly
  const errorMessage = "The admission number has been already used.";
  const form = e ? e.source : FormApp.getActiveForm();
  const responses = form.getResponses();
  const admissionNumbers = responses.map(response => {
    return response.getItemResponses().find(itemResponse => {
      return itemTitle === itemResponse.getItem().getTitle();
    });
  }).map(itemResponse => itemResponse.getResponse());
  const numbersPattern = admissionNumbers.map(number => `^${number}\$`).join("|");
  const item = form.getItems().find(item => item.getTitle() === itemTitle);
  let validation = FormApp.createTextValidation()
                          .setHelpText(errorMessage)
                          .requireTextDoesNotMatchPattern(numbersPattern)
                          .build();
  item.asTextItem().setValidation(validation);
}

function createTrigger() {
  const form = FormApp.getActiveForm();
  ScriptApp.newTrigger('updateDataValidation')
    .forForm(form)
    .onFormSubmit()
    .create();
}

Notes:

  • In the code sample above, the item Admission number is assumed to be named Admission number. Change that from the code if that's not the case.
  • In the code sample above, the item Admission number is assumed to be a text item, like Short answer.

Upvotes: 2

Related Questions