Google Script - setValues issue

I'm writing a script for a gDoc Spreadsheet and have a little issue with the setValues method.

The code is pretty basic but still desn't work properly.

First there is an Array that is build:

var newRow = [date, sRowValues[0][1], sRowValues[0][2], sRowValues[0][4]];

No problem so far (I believe) and then I try to write the result to a sheet:

destinationSheet.getRange(2,1,1,4).setValues(newRow);

And this brings the following error message: Cannot convert (class)@7fb23794 to Object

The following does work though:

destinationSheet.getRange(2,2,1,1).setValue("newRow")

Any help would be welcome. Thanks.


Solution 1:

Range.setValues() is expecting an array of arrays (two-dimensional array), you're feeding it an array of objects. On the other hand, Range.setValue() expects an object (string, number or date), for a single cell.

Try this, which will produce an array with 1 row of 4 columns (matching the dimensions of your range):

var newRow = [date, sRowValues[0][1], sRowValues[0][2], sRowValues[0][4] ];
var newData = [newRow];

destinationSheet.getRange(2,1,1,4).setValues(newData);