Creating a 2D Array for .createResponse in a CHECKBOX_GRID Form Item

246 views
Skip to first unread message

AdamD

unread,
Mar 3, 2022, 7:15:20 PM3/3/22
to Google Apps Script Community
I'm trying to figure out how the 2D array for creating a response in a Checkbox Grid question of a Form.  The "help" window states this:
-------------------------------------------------
createResponse(responses: string[][]): FormApp.ItemResponse

an array of arrays of valid answers for this checkbox grid item

Creates a new ItemResponse for this checkbox grid item. The argument responses must be a String[][] array of arrays containing as many values as the number of inputs in the checkbox grid. A null element for a non-required checkbox grid question indicates no response to that row. Throws an exception if any of the values does not match a valid choice.

--------------------------------------------------

If I have a simple Checkbox Grid question that is one row called "Row1" and two options or Columns called C1 and C2, how do I correctly structure the array so that the Form will accept it?

For example, if I want to enter the response of C1 and C2 (both items checked) in the Form, would the array be

[['C1']['C2']]
['Row1']['C1','C2']]
[[true],[true]]
['C1','C2']
[true,true]
something else?

The developers pages do not provide an example and everything I have tried is not working.

Adam

Message has been deleted

Edward Ulle

unread,
Mar 4, 2022, 9:04:50 AM3/4/22
to Google Apps Script Community
You create a 2D array like this var a = [[1,2,3].[4,5,6],[7,8,9],[10,11,12]].  Where each dimension of the array is wrapped in square barckets "[]".  And each element of an array is seperated by a comma ",".  So in my example I've created a 2D array of 4 elements, each element is itself an array of 3 elements.  So a[1] is [4,5,6] and a[2][0] is 7.  In your case its [["C1"],["C2"]]

Clark Lind

unread,
Mar 4, 2022, 10:26:41 AM3/4/22
to Google Apps Script Community
I don't think that is how it works. Creating a response is adding another choosable option, not setting the actual response. 

Edward Ulle

unread,
Mar 4, 2022, 10:48:34 AM3/4/22
to Google Apps Script Community
You asked how to create a 2D array.  I leave it up to you to construct it properly for your use.

AdamD

unread,
Mar 4, 2022, 4:52:58 PM3/4/22
to Google Apps Script Community
Thank you for the replies but the problem I'm having is specific to Forms and how it "accepts" responses via GAS.  To clarify, when you use .createResponse(), this creates a new response in the Form that can then be found in the responses, on the linked Sheet, as well as in the automatic summaries.  It is not adding a new option for a response.  That is done with .setChoiceValues() or a similar function based on the question item type.

The problem with the instructions that are available for the CHECKBOX_GRID item response is that the values must be in a 2D array - but there's nothing that says how the array is supposed to reference the options in the Form.  The .createResponse will throw an error if the values in the .createResponse do not exactly match the options in the Form.  I'm hoping that someone here knows how the array is supposed to be structured so that it will be accepted as a valid value with the .createResponse.

Adam

AdamD

unread,
Mar 4, 2022, 7:54:52 PM3/4/22
to Google Apps Script Community
In a Form i created a 1 row, 1 column Checkbox question.  This should mean that the 2D array should be array[0][0].  When I set the variables manually, I'm still getting an error.  Here's the code that I'm using to test this.

//Inserting Checkbox Grid Response

//Global Variables for Form
var ss = SpreadsheetApp.getActiveSpreadsheet();
var form = FormApp.openByUrl(ss.getFormUrl());
var formResponses = form.getResponses();
var formCreate = form.createResponse();
var formItems = form.getItems();

function setResponses(){
try {
var cv = [['C1']];
var itemResponse = formItems[0].asCheckboxGridItem().createResponse(cv[0][0]);
formCreate.withItemResponse(itemResponse);
} catch(err) {
console.log("error");
}
}

When I console.log( cv[0][0] ), it reports C1 as the value.  The code seems to fail when I'm defining the itemResponse variable.

Any help is appreciated!

Adam

Edward Ulle

unread,
Mar 5, 2022, 10:23:17 AM3/5/22
to Google Apps Script Community
I'm not that familiar with forms, but you perked my interest and I started exploring forms and your question in particular.  The Google documentation on createResponse() for checkbox is sorely lacking.  I couldn't find anything on https://stackoverflow.com/ which is usually a pretty good resource. And to be honest with you I don't even know what createResponse() does since I can't seem to make it work.  I've tried the spreadsheet column header, the checkbox title and "true" to create a valid response but its a guessing game.  I'm using a spreadsheet and responses seem to be the contents of the spreadsheet rows.   Maybe if you describe what you are trying to achieve there may be an alternative.  

AdamD

unread,
Mar 5, 2022, 11:38:54 AM3/5/22
to Google Apps Script Community
I'm glad to know that I'm not the only one who cannot figure out how this is supposed to be done.

The use case is that I will have teachers who change Form questions after they have received submissions from sections of their class.  The change will cause the previous data to no longer be included in the Form response summary page or the individual responses.  Instead the data is hidden from view and only accessible through downloading the csv of the responses or the data found in the linked Sheet (provided the teacher linked the Sheet before editing the Form).  The current work around is to use the Form's Pre-Filled-URL feature and then create a URL for each row of data through the use of formulas in the Sheet.  The teacher must then manually click on each link to enter the data back into the Form.  This is not only time consuming but many of the teachers cannot create a formula that will create the individual url links.

To solve this, I'm creating a script that I can use with these teachers when this occurs.  I have everything figured out except for the grid and time items.  Here's a stripped down version of the script that applies only to a checkbox_grid question.

https://docs.google.com/spreadsheets/d/1NlMrCTOVHFqQ_wS1AcZFF5SF0RurwUJ6Pjv42nZCvXA/edit?usp=sharing

I've done some more digging on this and I found that when I push an array to create the 2D array, I'm getting unexpected results.  For example,

Array = [['C1'],['C2']]
gives me the expected results of Array[0][0] = 'C1' and Array[1][0] = 'C2' but the following error is being returned:

Exception: Wrong number of responses: 2. Expected exactly: 1.

Thoughts?

Adam

AdamD

unread,
Mar 5, 2022, 11:43:21 AM3/5/22
to Google Apps Script Community
I just found this "explanation" in the developers site for grid items:

For GridItem questions, this returns a String[] array in which the answer at index n corresponds to the question at row n + 1 in the grid. If a respondent did not answer a question in the grid, that answer is returned as ''.

For CheckboxGridItem questions, this returns a String[][] array in which the answers at row index n corresponds to the question at row n + 1 in the checkbox grid. If a respondent did not answer a question in the grid, that answer is returned as ''.


Any idea what index n is referring to?

Adam

AdamD

unread,
Mar 6, 2022, 9:02:10 AM3/6/22
to Google Apps Script Community
I received an answer to this on Stack overflow.  The answer is here.  

Adam

Edward Ulle

unread,
Mar 6, 2022, 12:12:18 PM3/6/22
to Google Apps Script Community
I've been following this and I'm glad you took it to stackoverflow.  At first their moderators thought it was a duplicate but it wasn't.  Took some persuading to get an answer.  Good work.

AdamD

unread,
Mar 6, 2022, 7:42:01 PM3/6/22
to Google Apps Script Community
I'm glad you jumped in.  I don't know if I was going to get a response.  The focus seemed to be on making sure I was using the correct mark-up language for the question.
Reply all
Reply to author
Forward
0 new messages