'How to import checked options on Google Forms checkbox with Apps Script?
I have a survey in which there are several questions, each one can be a different type (paragraph, checkbox, scale). I know how to import the responses of all the questions, except the checkbox.
var form = FormApp.openById(idg);
var formResponses = form.getResponses();
var linhaform = formResponses[j];
var linharesponses = linhaform.getItemResponses();
After that I extract the responses as linharesponses[i] (for loop)
The problem is: if the question is a checkbox, in which the user can select multiple options, this method only import the first answer.
Other thing I tried is
var chkItem = linharesponses[i].getItem().asCheckboxItem();
//and then importing as
chkItem.getChoices()[j].getValue();
but this imports all the options, not the selected ones.
Can you help me find a way to import selected options?
Solution 1:[1]
- For the checkbox on the Google Form, you want to retrieve the values of checkboxes which were checked from the response.
- In your question, I understood
import selected optionsas you want to retrieve the values of the checked checkboxes.
- In your question, I understood
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
Sample script:
function myFunction() {
var idg = "###"; // Please set Form ID.
var form = FormApp.openById(idg);
var formResponses = form.getResponses();
for (var j = 0; j < formResponses.length; j++) {
var linhaform = formResponses[j];
var linharesponses = linhaform.getItemResponses();
for (var k = 0; k < linharesponses.length; k++) {
var itemResponse = linharesponses[k];
if (itemResponse.getItem().getType() == FormApp.ItemType.CHECKBOX) {
var response = itemResponse.getResponse();
Logger.log("%s, %s", itemResponse.getItem().getTitle(), response)
}
}
}
}
Note:
- For example, if the title of checkbox is "sample checkbox", and the names of each checkbox are "foo", "bar" and "baz", when the checkboxes of "foo" and "baz" were checked and submitted, this script returns
sample checkbox, [foo, baz]. - In this sample script, the response values of checkbox are retrieved. If you want to retrieve the response values of all items, please remove
if (itemResponse.getItem().getType() == FormApp.ItemType.CHECKBOX) {.
References:
If I misunderstood your question and this was not the direction you want, I apologize.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tanaike |
