Categories: Apps ScriptForms

Google Form Script to Automatically Add New Choices from “Other:” Form Responses

Whit this Google Form Script you can Automatically Add the “Other:” response from the multiple-choice question or checkbox question to the list of choices for the next user.

There is a plugin for that, but this way you can just paste the script in your Google Form and it will automatically work for you by adding “Other” responses into the list of choices.

So, when a user submits an answer under the “Other:” option from a multiple-choice or checkbox question, that answer is:

  • automatically added to the list of choices, and
  • it will be automatically available to the next user

How to implement

The implementation is very easy, maybe easier than installing a plugin. So, all you have to do is:

  • open the Script editor from the Google Form
  • paste the script there
  • and just run it once

Here are the detailed instructions:

1. Open the script editor from the three-dot menu on the top right part of the screen.

2. Paste the following script into the editor, first delete everything there.

function firstRun(){
  ScriptApp.newTrigger('onFormSubmit')
  .forForm(FormApp.getActiveForm())
  .onFormSubmit()
  .create();
}
//Check for "Other..." in Form submission and
//add them to the multiple choice or checkbox questions
function onFormSubmit() {
  //Get the active Form
  var form = FormApp.getActiveForm();
  //Get the latest Form response
  var formResponses = form.getResponses();
  var formResponse = formResponses[formResponses.length - 1];
  var itemResponses = formResponse.getItemResponses();
  //get all the Multiple-choice questions
  var mc = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
  //get all the Checkbox questions
  var cb = form.getItems(FormApp.ItemType.CHECKBOX);
  //Go through the latest response
  for (var j = 0; j < itemResponses.length; j++) {
    var item = itemResponses[j];
    //Go through all the multiple choice questions
    for (var i = 0; i < mc.length; i++)
      //check for the same multiple choice question title
      if (mc[i].getTitle() == item.getItem().getTitle()) {
        //get the available choices from the question
        var multi = mc[i].asMultipleChoiceItem();
        var mValues = multi.getChoices();
        var mArray = [];
        //set hit to false
        var mHit = false;
        //go through the multiple choice question values
        //and compare with the response value
        for (var y = 0; y < mValues.length; y++) {
          //also, add the values to array
          mArray[y] = mValues[y].getValue();
          if (mArray[y] == item.getResponse()) {
            //if value is found
            //set hit to true
            mHit = true;
          }
        }
        //if hit is false (submitted value is not in the question values)
        if (!mHit) {
          //add the submited value to the question values array
          mArray.push(item.getResponse());
          //add that array to the multiple choice question
          multi.setChoiceValues(mArray);
        }
      }
    //Go through all the checkbox questions
    for (var i = 0; i < cb.length; i++)
      //check for the same checkbox question title
      if (cb[i].getTitle() == item.getItem().getTitle()) {
        //get the available choices from the question
        var cBox = cb[i].asCheckboxItem();
        var cValues = cBox.getChoices();
        var cArray = [];
        //set hit to false
        var cHit = false;
        var cBoxResponses = item.getResponse();
        //go through the checkbox question values
        //and compare with the response value
        for (var y = 0; y < cValues.length; y++) {
          //also, add the values to array
          cArray[y] = cValues[y].getValue();
          //the checkbox response is an array, so we must check for values inside
          for (var x = 0; x < cBoxResponses.length; x++) {
            if (cArray[y] == cBoxResponses[x]) {
              //if value is found
              //set hit to true
              cHit = true;
              //remove that value from the response
              cBoxResponses.splice(x, 1);
              //in case there are more answers in the response
              //set hit to false
              if (cBoxResponses.length > 0) cHit = false;
            }
          }
        }
        //if hit is false (submitted value is not in the question values)
        if (!cHit) {
          //add the submitted value to the question values array
          cArray.push(cBoxResponses[0]);
          //add that array to the checkbox question
          cBox.setChoiceValues(cArray);
        }
      }
  }
}

3. Select the “firstRun” function, from the function list, and run it

4. Google Form script will inform you that authorization is required, so click on Review permissions and follow the onscreen instructions. If you get stuck, you can follow this guide: Google Script Authorization: Review and Accept the Permission Guide.

That’s it. Now go back to your form and test it, the script will work with Multiple choice questions and with Checkbox questions.

SavoE

I'm an IT Manager and a big Google fan. In my free time, I like to write articles for How to GApps and help people on Google help communities (My Profile) and Quora. Check the About page for more. If you appreciate my work, maybe you can help me pay my hosting bill on Buy Me a Coffee.

View Comments

  • I received an error when I tried this Script
    Exception: Unexpected error while getting the method or property create on object ScriptApp.FormTriggerBuilder.
    firstRun@ Code.gs:5

    • Hi Deb,

      This error is generated if the method or property doesn't exist, so if you put this script in a Spreadsheet you can get this error. Can you confirm that you paste the script in Google Form?

      Best regards.

Recent Posts

Google Form to Slides + Save to Drive and/or send it in email as a PDF

On your request, as an addition to the how-to: Google Form to PDF (save to Drive and/or send it in…

1 day ago

Google Form to PDF (save to Drive and/or send it in email)

This solution will create a PDF file from a Google Form submission using a Google Doc as a template. You…

3 months ago

Automated e-card sender – Google Spreadsheet Script to Send Birthday Cards via Email

Sending an email to wish your customers a happy birthday it's usually a thing that big companies do. There is…

4 months ago

Embed Google Form, Sheet, Chart and Doc in WordPress

You have a plugin for that too, but adding more plugins to your WordPress site can make it slower, and…

6 months ago

Google Form Script to AutoFill and Email a Doc Template

I have used a similar solution for creating documents from a template, but it was a spreadsheet script and used…

10 months ago

Add Timer to Google Form Quiz

Finally, I fund a way to add a timer to a google form, and yes, it forces the user to…

10 months ago