2

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

  • I'm not sure I understand. The response `Mexico` (or whatever it is) will not be seen in the `Questions` tab, but in the `Responses` tab ([screenshot](https://i.ibb.co/b3hb3XX/Screenshot-2020-11-26-at-1-30-14-PM-Display-2.png)). Where are you seeing the blank field? – Iamblichus Nov 26 '20 at 12:32
  • Sorry if my question is not much descriptive. There is one multiple choice question, where there are two option - United States and Others (feature in google form which allows users to enter any other Text which is not in multiple choice). While submitting form from script , we are sending answer to this multiple choice - if we are sending answer as United States it is accepting and displaying too. I am trying to find a way to send Others as answer to this multiple choice question. – roshan kumar Nov 26 '20 at 12:42
  • Yes, I understood that. My question was related to **what makes you think the answer is not getting sent**. I tried to reproduce this, and it is certainly getting sent for me (I can see the response in the `Responses` tab for which I sent a screenshot). As long as I add `formResponse.submit()`, of course. – Iamblichus Nov 26 '20 at 12:46
  • @lamblichus My answer is being sent, but when I am opening the form the answer is not visible. But as you have sent in screenshot, you are having two option 1. United states and 2. Mexico. But I have two option as 1.United States and 2. Other:____ , where in when you choose other option , you have to manually write the country name. I am trying to submit form with this Other option as answer. If I have two radio button such as US and Mexico, then its working fine as in your screenshot. – roshan kumar Nov 26 '20 at 13:11
  • No. In the `Responses` tab it shows `Mexico` because that's what was written in the `Other:` option. There's no option `Mexico` in my form, just `United States` and `Other`: [that's what my form looks like](https://i.ibb.co/7kz1fYV/Screenshot-2020-11-26-at-2-17-53-PM-Display-2.png). And of course, with this form, if I submit an answer by typing `Mexico` in `Other:`, that response can be seen successfully. My guess, for what it's worth, is that you're looking for the answer `Mexico` in the wrong place. You should check the `Responses` tab. – Iamblichus Nov 26 '20 at 13:21
  • @lamblichus thanks for your detailed answer. Can you share the code snap for reference. Is it same as shared by me or do you suggest few changes – roshan kumar Nov 26 '20 at 14:10
  • I used the same code you provided before editing your question, adding `formResponse.submit()` at the end (actually, there's no need to check if `territoryvalue` is `United States`, the code should do the same. Have you checked the `Responses` tab? What can you see there? – Iamblichus Nov 26 '20 at 14:43

3 Answers3

1

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
Diego
  • 9,261
  • 2
  • 19
  • 33
  • I tried the same way but it doesnt shows up. Only for United States it shows up , if I am passing any other value it shows in backend response google sheet but not in the form – roshan kumar Nov 26 '20 at 14:46
  • @roshankumar I suspect you're asking about how to add another country as an option in the form, not how to submit an other response. Please check my updated answer. – Diego Nov 26 '20 at 15:02
1

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.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
0

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"
Jason E.
  • 1,201
  • 1
  • 3
  • 10
  • I tried the same way but it doesn't shows up. Only for United States it shows up , if I am "Other" it shows in backend response google sheet but not in the form – roshan kumar Nov 26 '20 at 14:47
  • I see. Please see my updated answer and let me know if this solves your problem. – Jason E. Nov 26 '20 at 15:31
  • @roshankumar let me know if this solves your problem. :) – Jason E. Nov 27 '20 at 10:27
  • I tried this, When I am only sending __other_option__ then the radio button for other field is getting clicked but I am not able to send an other country name with it. Looks like a bug. I have raised it in google issue tracker. Please mark it as star if you are also able to replicate this issue. https://issuetracker.google.com/issues/174303428 – roshan kumar Nov 29 '20 at 08:29
  • what do you mean by you are not able to send an other country name with it? have you checked my updated answer? whenever I submit "__other_option__ Mexico", the "Other:" option is being ticked and the country Mexico is being recorded. – Jason E. Nov 30 '20 at 13:03