Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. /*
  2. Script for Google Forms.
  3.  
  4. "Fixes" other... field in checkbox and radio groups.
  5.  
  6. Whenever form is submitted and "other" option is selected,
  7. input goes to corresponding checkbox/radio group in form
  8. as a new option, so next respondents don't have to put it
  9. in manually.
  10.  
  11. To use copy-paste code to your forms project and assign
  12. onSubmit function to project's "on form submit" trigger.
  13. */
  14.  
  15. function onSubmit(event) {
  16. var form = event.source;
  17. var response= event.response;
  18.  
  19. var checkboxes = form.getItems(FormApp.ItemType.CHECKBOX);
  20. var radios = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
  21. var choosers = checkboxes.concat(radios);
  22.  
  23. choosers.filter(function(c) {
  24. var type = c.getType();
  25. return (type == FormApp.ItemType.CHECKBOX) ? c.asCheckboxItem().hasOtherOption():
  26. (type == FormApp.ItemType.MULTIPLE_CHOICE) ? c.asMultipleChoiceItem().hasOtherOption():
  27. false;
  28. });
  29.  
  30.  
  31. for (i = 0; i < choosers.length; i++) {
  32. var chooser = choosers[i];
  33. chooser = (chooser.getType() == FormApp.ItemType.CHECKBOX) ? chooser.asCheckboxItem():
  34. (chooser.getType() == FormApp.ItemType.MULTIPLE_CHOICE) ? chooser.asMultipleChoiceItem():
  35. "Something went wrong. It is definitely Google's fault!";
  36.  
  37.  
  38. var chooserResponse = response.getResponseForItem(choosers[i]);
  39. if (chooserResponse == null) return;
  40.  
  41. chooserResponse = chooserResponse.getResponse();
  42. var otherField = chooserResponse[chooserResponse.length-1];
  43. var choices = chooser.getChoices();
  44.  
  45. if (!mContains(choices, otherField)) {
  46. var newChoice = chooser.createChoice(otherField);
  47. choices.push(newChoice);
  48. chooser.setChoices(choices);
  49. }
  50. }
  51.  
  52. }
  53.  
  54. function mContains(choices, item) {
  55. for (i = 0; i < choices.length; i++) {
  56. if (choices[i].getValue() == item) {return true;}
  57. }
  58. return false;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement