Mike
Mike

Reputation: 5770

How to use Google Apps Script retrieve the value a user typed in for an Other field in Google Forms?

I have a Google Form that is a simple checkbox list with an "Other" option. As author of the form it looks like this:

enter image description here

when I fill out the form, I can check the "Other" checkbox and type in a value, and submit.

enter image description here

but when my Google Apps Script runs,

  var choices = checkboxItem.getChoices();
  Logger.log("Choices array length: %s", choices.length);

  var results = [];
  for (var i = 0; i < choices.length; ++i) {
    results.push(choices[i].getValue());
  }

  Logger.log("getFormChoicesAsStrings %s", JSON.stringify(results));

it isn't seeing what I typed in for the "Other" value (in this example: "Steve Jobs"), as evidenced by the log output:

[19-01-12 20:31:59:164 PST] Logger.log([Choices array length: %s, [1.0]]) [0 seconds]
[19-01-12 20:31:59:165 PST] Logger.log([getFormChoicesAsStrings %s, [["John Doe"]]]) [0 seconds]

The API for CheckboxItem allows a true/false whether or not an Other option is displayed, and the API for Choice doesn't offer anything around the Other option.

What API call can I do to read the value of the Other option?

Upvotes: 2

Views: 264

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • Users check the checkbox and put the values to "Other", and click the submit button.
  • In above situation, you want to retrieve the response values including the values of "Other" section.

If my understanding is correct, how about this modification?

Modification point:

  • In your script, although I cannot see the whole script, I thought that the items from form are retrieved. This is not the response values.

Modified script:

Please copy and paste the following script and install the trigger for myFunction().

// FormApp.getActiveForm() // This is used for adding a scope of https://www.googleapis.com/auth/forms
function myFunction(obj) {
  var r = obj.response.getItemResponses();
  r.forEach(function(e) {
    var r1 = e.getItem().getTitle();
    var r2 = e.getResponse();
    Logger.log(r1); // "Who attended?"
    Logger.log(r2); // ["John Doe","Steve Jobs"]
  });
}

Note:

  • When you use this script, please install myFunction() as a Installable Triggers.
    • "Choose which function to run" is "myFunction".
    • "Select event source" is "From form".
    • "Select event type" is "On form sumit".

References:

If I misunderstand your question, please tell me. I would like to modify it.

Upvotes: 1

Related Questions