Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Script for Google Forms.
- "Fixes" other... field in checkbox and radio groups.
- Whenever form is submitted and "other" option is selected,
- input goes to corresponding checkbox/radio group in form
- as a new option, so next respondents don't have to put it
- in manually.
- To use copy-paste code to your forms project and assign
- onSubmit function to project's "on form submit" trigger.
- */
- function onSubmit(event) {
- var form = event.source;
- var response= event.response;
- var checkboxes = form.getItems(FormApp.ItemType.CHECKBOX);
- var radios = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
- var choosers = checkboxes.concat(radios);
- choosers.filter(function(c) {
- var type = c.getType();
- return (type == FormApp.ItemType.CHECKBOX) ? c.asCheckboxItem().hasOtherOption():
- (type == FormApp.ItemType.MULTIPLE_CHOICE) ? c.asMultipleChoiceItem().hasOtherOption():
- false;
- });
- for (i = 0; i < choosers.length; i++) {
- var chooser = choosers[i];
- chooser = (chooser.getType() == FormApp.ItemType.CHECKBOX) ? chooser.asCheckboxItem():
- (chooser.getType() == FormApp.ItemType.MULTIPLE_CHOICE) ? chooser.asMultipleChoiceItem():
- "Something went wrong. It is definitely Google's fault!";
- var chooserResponse = response.getResponseForItem(choosers[i]);
- if (chooserResponse == null) return;
- chooserResponse = chooserResponse.getResponse();
- var otherField = chooserResponse[chooserResponse.length-1];
- var choices = chooser.getChoices();
- if (!mContains(choices, otherField)) {
- var newChoice = chooser.createChoice(otherField);
- choices.push(newChoice);
- chooser.setChoices(choices);
- }
- }
- }
- function mContains(choices, item) {
- for (i = 0; i < choices.length; i++) {
- if (choices[i].getValue() == item) {return true;}
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement