Why won't append row work with array?

十年热恋 提交于 2020-02-05 04:19:05

问题


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


回答1:


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);



回答2:


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


来源:https://stackoverflow.com/questions/42447359/why-wont-append-row-work-with-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!