Using setValues()

444 views
Skip to first unread message

Dirk

unread,
Mar 2, 2021, 11:48:19 AM3/2/21
to Google Apps Script Community
Hi,
I have some trouble with writing an array to a range of cells.

function go(){
var xy = [];
for(i=0; i<12; i++){
xy[i] = i;
}
Geodata.getRange("Z2:Z13").setValues(xy);
}

Error: Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.

Can you help?


Bennett, Scott

unread,
Mar 2, 2021, 11:54:59 AM3/2/21
to google-apps-sc...@googlegroups.com
I think you need to define xy as
xy=[[]]

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/99c24477-e936-4f7d-b82d-5d6d685f9476n%40googlegroups.com.



Dirk

unread,
Mar 2, 2021, 11:56:47 AM3/2/21
to Google Apps Script Community
No, this is not the issue.

CBMServices Web

unread,
Mar 2, 2021, 11:59:36 AM3/2/21
to google-apps-sc...@googlegroups.com
Set Values is expecting a 2 dimensional array but you xy array is one dimensional.

Try this:

function go(){
var xy = [];
for(i=0; i<12; i++){
xy[i][0]= i;
}
Geodata.getRange("Z2:Z13").setValues(xy);
}



Dirk

unread,
Mar 2, 2021, 12:02:38 PM3/2/21
to Google Apps Script Community
This leads to the error: TypeError: Cannot set property '0' of undefined

Bennett, Scott

unread,
Mar 2, 2021, 12:04:56 PM3/2/21
to google-apps-sc...@googlegroups.com
Is this what  you want for the array?
function go(){
var xy = [];
for(i=0i<12i++){
xy.push([i]);
}
Logger.log(xy)
}



--
Scott Bennett
Data and Assessment Coordinator/Math Teacher
Bradley-Bourbonnais Community High School

CBMServices Web

unread,
Mar 2, 2021, 12:06:49 PM3/2/21
to google-apps-sc...@googlegroups.com
I never liked the way arrays are defined in javascript.

Try this:


function go(){
var xy = [];
for(i=0; i<12; i++){
xy[i]=[ i];
}
Geodata.getRange("Z2:Z13").setValues(xy);
}



Dirk

unread,
Mar 2, 2021, 12:09:38 PM3/2/21
to Google Apps Script Community
This is working! Great! Thank you!

Dirk

unread,
Mar 2, 2021, 12:10:05 PM3/2/21
to Google Apps Script Community
This is also working! Thank you!
Reply all
Reply to author
Forward
0 new messages