Google form Multiple Choice Other option not visible when submitted from app script

1.6k views Asked by At

I have a google form where I have a multiple choice question "Territory field". This field is having two valid answer

  1. United States and
  2. Others (When you choose other option you can write down any Country name in text area) Now I am trying to submit this form using google app script. While submitting from app script if I am sending answer to this multiple choice question as "United States" its workiing. I want to send Others as answer to this question while submitting the form. Can anyone help me out. I am including picture of field and script below for reference .

Picture of form field with multiple choice

I am trying to submit this form using google script (part of script mentioned below).

  var formID = "xxxxxxxxxxxxxxxxxxxxxx";
  var Formobj= FormApp.openById(formID);
  var formResponse = Formobj.createResponse();
  var items = Formobj.getItems();
  var territory =  items[1]
  var territoryvalue = "United States"
  
  if (territoryvalue.indexOf("United States") !== -1)
  {
  var territoryfield = territory.asMultipleChoiceItem()
  var territoryresponse = territoryfield .createResponse([["United States"]])
  formResponse.withItemResponse(territoryresponse);
  }

Field value after form is submitted with United states as answer

Need help in submitting this form with "Others" as answer

3

There are 3 answers

0
Iamblichus On

I saw you filed a bug in Issue Tracker, and based on your explanation there I could finally understand this issue.

This seems to be a bug:

In Forms, if a Multiple choice item gets submitted with an Other choice via Apps Script, this other choice is not populated when trying to edit the response. If the response is submitted via UI, the Other option is populated.

It's important to note that the response itself is getting submitted successfully, and can be seen in the Responses tab of the Form; it's just not populated when trying to edit the response.

Steps to reproduce:

  1. Create a new Form.
  2. Create a Multiple choice item for the Form, if there is not one already.
  3. Click add "Other" in order to have an Other option.
  4. Click Settings (gear icon) and check the option Respondents can: Edit after submit.
  5. Open the script editor bound to the Form, and copy and run the following code:
function submitAndGetEditResponse() {
  var form = FormApp.getActiveForm();  
  var item = form.getItems()[0].asMultipleChoiceItem();
  var otherOption = "Option 2"; // Not one of the named options
  var itemResponse = item.createResponse([[otherOption]]);
  var formResponse = form.createResponse().withItemResponse(itemResponse);  
  var editUrl = formResponse.submit().getEditResponseUrl();
  console.log(editUrl);
  return editUrl;
}
  1. Access the URL (editUrl) with the browser.
  2. Option 2 should be populated when trying to edit the response, but it's not.

Issue Tracker:

The issue you filed in Issue Tracker just got forwarded internally by Google:

Anyone affected by this, please consider clicking the star on the top-left in order to keep track of it and to help prioritizing it.

2
Diego On

The documentation for MultipleChoiceItem.createResponse() reads:

Creates a new ItemResponse for this multiple-choice item. Throws an exception if the response argument does not match a valid choice for this item, unless showOtherOption(enabled) is set to true.

So to submit an "other" response, just pass it the value.

var territoryresponse = territoryfield.createResponse([["Other Country"]])

If instead you want to add a new value as a selectable choice in the form, then you can create a new choice and then set the item choices.

var territory = items[0].asMultipleChoiceItem();
var newCountryChoice = territory.createChoice("Another Country");
var choices = territory.getChoices(); // Get the existing choices
choices.push(newCountryChoice); // Append the new choice to the list of existing
territory.setChoices(choices); // Set the updated choices
5
Jason E. On

I tried replicating your code and got the solution for what you wanted.

If you want your "Other" answer to be ticked in the individual responses. Your response string should look like this: "other_option (Country)"

For example, if you want Mexico to appear in the "Other:" option, you should write "__other_option Mexico" as the value for your territoryvalue variable. It should look like this in that part of your code:

var territoryvalue = "__other_option__ Mexico"