I'm trying to create a script in Google Spreadsheet to reject form submissions if the answer field is not = 12. This is the hopefully screen non-human submissions and decrease on spam submissions through the Google Forms.
In the form I have a text field: Name and text field: Answer with hint "5 + 7 ="
function myFunction(e) {
var answer = "";
try{
for(var field in e.namedValues) {
if (field == 'Answer') {
answer = e.namedValues[field].toString();
if ( Answer !== 12) { // check to make sure input is 12
throw "not the right answer"; // throw an exception with the error message
}
} //end if
}//end for
} catch(e) {
throw "try failed";
} //end try
}
After saving this script, I then created a trigger to run myFunction | From Spreadsheet | On form submit. Once this was in place, I tried it on the live form, Bob | 13 and the entry still appears in the spreadsheet.
Any suggestions why this is not working?
Answerandanswerare different variable names...Javascript is case sensitive. Also, thethrow "try failed";in the catch block should readthrow e;That way you aren't just throwing another error over the first one. - HardScale