Why won't append row work with array?

2019-07-28 02:13发布

I am trying to insert a row to the bottom of a sheet, but instead of my values I see text similar to what happens when you try to print an array in Java. I checked to see if the array is made correctly with logger and it has the values I want.

var name = e.range.getRow() + SpreadsheetApp.getActiveSpreadsheet().getName();
var array = e.range.getValues().concat(name);
Logger.log(array.toString());
masterSheet.appendRow(array);

array contains a timestamp, string1, string2, and finally the name I concatenated. Instead I get something like [Ljava.lang.Object;@7dch7145

2条回答
爷的心禁止访问
2楼-- · 2019-07-28 02:39

It looks like a problem with your use of getValues() which returns a two-dimensional array and needs to be accessed as such.

GAS API Reference: getValues()

Return

Object[][] — a two-dimensional array of values

I believe this edit to setting your var array should do the trick, assuming your data range is for a single row (index 0 will be the first row, otherwise just change the index to the row you want):

var array = e.range.getValues()[0].concat(name);
查看更多
ら.Afraid
3楼-- · 2019-07-28 02:50

This is because appendRow() is looking for array[] not array[][].

If you attempt to append:

var array = [[1,2,3],"some string"]  

It will show up as the following as it is trying to get the entire contents of the first position of the array in a single cell. It does this by returning a string of the array object which turns out to be the native code identifier.

[Ljava.lang.Object;@32dba1e2 |  some string

You can append the contents of array by appending its individual members such as

ss.appendRow(array[0])

Would append

1 | 2 | 3
查看更多
登录 后发表回答