Reputation: 31
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
Reputation: 19309
Avoid multiple form responses to be submitted with the same value for a specific form item.
Admission number
item, as shown in Set rules for your form.Admission number
should be forbidden in future responses.updateDataValidation
.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:
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()).^{number1}$|^{number2}$|^{number3}$...
.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();
}
Admission number
is assumed to be named Admission number
. Change that from the code if that's not the case.Admission number
is assumed to be a text item, like Short answer
.Upvotes: 2