How to place objects into 2D array

1,058 views
Skip to first unread message

AdamD

unread,
Mar 13, 2022, 11:02:49 AM3/13/22
to Google Apps Script Community
I am getting values from a sheet and needing to convert the values into a 2D array.  In the spreadsheet, the values for cell[0] = Paris, NYC and cell[1] = LA.

function testFunction1() {
const ss = SpreadsheetApp.getActiveSheet();
const form = FormApp.openByUrl(ss.getFormUrl());
var arr = [];
for(i = 0; i<2; i++){
var cell = ss.getRange(2,i+2).getValue();
cell = cell.split(',');
arr.push(cell);
}
var newRes = form
.getItems()[0]
.asCheckboxGridItem()
.createResponse(arr);
form.createResponse().withItemResponse(newRes).submit();
}

When I split each cell, I get:
split cell[0] = ['Paris', 'NYC']
split cell[1] = ['LA']

After pushing the items into arr, I get:
arr = [ ['Paris', 'NYC'], ['LA'] ]

This looks like a 2D array but I then .createResponse(arr) gives the error of "Exception: Invalid response submitted to checkbox grid question."

If I manually enter [ ['Paris', 'NYC'], ['LA'] ] into the .createResponse like this:

function testFunction2() {
const ss = SpreadsheetApp.getActiveSheet();
const form = FormApp.openByUrl(ss.getFormUrl());
var newRes = form
.getItems()[0]
.asCheckboxGridItem()
.createResponse([ ['Paris','NYC'], ['LA'] ]);
form.createResponse().withItemResponse(newRes).submit();
}

then there is no error and the response is added to the Form correctly.

The only thing I can think of is that the cell values are objects and they are not being placed into the 2D array correctly. How can I pass the values from cells into a 2D array correctly?

Adam

AdamD

unread,
Mar 13, 2022, 2:02:47 PM3/13/22
to Google Apps Script Community
I have also tried:

function testFunction1() {
const ss = SpreadsheetApp.getActiveSheet();
const form = FormApp.openByUrl(ss.getFormUrl());
var arr = [[]];
for(i = 0; i<2; i++){
var cell = ss.getRange(2,i+2).getValue();
cell = cell.split(',');
arr[0][i] = cell;
console.log(cell);
}
var cell = arr;
console.log(cell[0]);
var newRes = form
.getItems()[0]
.asCheckboxGridItem()
.createResponse(cell[0]);
form.createResponse().withItemResponse(newRes).submit();
}

This throws the error of "Exception: Invalid response submitted to checkbox grid question." at the .createResponse(cell[0]).

Also if the .createResponse(cell[0]) is changed to .createResponse(cell), then the following error is received, "Exception: The parameters (number[]) don't match the method signature for FormApp.CheckboxGridItem.createResponse."

Adam

Robert B Johnson

unread,
Mar 14, 2022, 8:45:40 AM3/14/22
to Google Apps Script Community
For some reason the apps script can only use a array in an array ?? meaning that the first space for data is [0][0] and the second space is [0][1], 
if traditionally you wanted an array to look like this  [0,1,2,3,4,5]   in apps script it must be established like [[0,1,2,3,4,5]]  or   [[0],[1],[2], ........ ]

don't know if that helps
Bob

Clark Lind

unread,
Mar 14, 2022, 8:50:02 AM3/14/22
to Google Apps Script Community
Maybe try getting all responses into a single array:   arr = ["Paris", "NYC", "LA"],  then try adding the single array:  .createResponse([arr]).  I haven't worked with a form like this,
but that should help simplify things. 

var arr = [];
cell[0].forEach( (item0) => arr.push(item0));
cell[1].forEach( (item1) => arr.push(item1));  
var newRes = form
.getItems()[0]
.asCheckboxGridItem()
.createResponse([ arr ]); //because arr is already an array, just drop it in inside another array
form.createResponse().withItemResponse(newRes).submit();

Clark Lind

unread,
Mar 14, 2022, 9:54:04 AM3/14/22
to Google Apps Script Community
Ok, disregard my previous post. Are you trying to add items to the form as options to select, or are you trying to actually submit responses as if someone answered the question?
createResponse() actually submits a response as if someone answered the question. If you want to add items to the question, I think you use a combination of setRows() and setColumns().

AdamD

unread,
Mar 14, 2022, 4:37:02 PM3/14/22
to Google Apps Script Community
Here's the answer to the problem. https://stackoverflow.com/questions/71466651/how-to-create-a-valid-2d-array-that-google-apps-script-form-will-accept-for-chec/71469743#71469743
The best guess is that trailing spaces were being added to the arrays and trim was needed.

To clarify, this code is being used to create responses, as if someone answered the checkbox grid question.

Adam

Reply all
Reply to author
Forward
0 new messages