[英]appendRow returns [Ljava.lang.Object;@ instead of values
I want to copy form submissions over to a different sheet so that the copied data can be edited without affecting the original submissions.我想将表单提交复制到不同的工作表,以便可以编辑复制的数据而不影响原始提交。
I have the following code:我有以下代码:
function copy2(){
var responses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("from");
var tracker = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("to");
var lastrow = responses.getLastRow();
var col = responses.getLastColumn();
var row = responses.getRange(lastrow, 1, 1, col).getValues();
tracker.appendRow([null,row[0]]);
Using null
in appendRow
helps you move the info over to the next column.在
appendRow
使用null
可帮助您将信息移至下一列。 However, it doesn't quite work with the row[0]
array.但是,它不适用于
row[0]
数组。 If I remove the null
it works fine, but I want the info copied on a column different that the first one.如果我删除
null
它工作正常,但我希望将信息复制到与第一列不同的列上。
Why Ljava.lang.Object?为什么是 Ljava.lang.Object?
Because you are using the older Rhino runtime that was written in Java.因为您使用的是用 Java 编写的较旧的 Rhino 运行时。 Hence when something unexpected happens you get a glimpse of the infrastructure GAS is built upon.
因此,当发生意外情况时,您可以瞥见构建 GAS 的基础设施。 Now, the
java.lang.object
is a base class in Java from which other objects, including arrays, are derived.现在,
java.lang.object
是 Java 中的一个基类,从它派生其他对象,包括数组。
Since the appendRow
method signature's only parameter accepts a one-dimensional array of values, your row[0]
, which contains an array (see what getvalues
method returns), made it to the sheet as a string tag indicating that this was an object at runtime.由于
appendRow
方法签名的唯一参数接受一维值数组,因此包含数组(查看getvalues
方法返回的内容)的row[0]
将其作为字符串标记添加到工作表中,表明这是一个对象运行。
What to do in Rhino?在犀牛做什么?
All solutions depend on taking [ null ]
as your base array and using concat
to append the rest of the first row, something like this: [ null ].concat(row[0])
.所有解决方案都取决于将
[ null ]
作为基本数组并使用concat
附加第一行的其余部分,如下所示: [ null ].concat(row[0])
。 You can also use push
with a simple for
loop for better performance.您还可以
push
与简单的for
循环一起使用以获得更好的性能。
What to do in V8 0 ?在 V8 0 中做什么?
As the other answer mentioned, your best bet is the spread syntax .正如另一个答案所提到的,你最好的选择是传播语法。 You can also do a
push(...row[0])
to avoid concatenation of arrays (since you immediately use and discard the copy resulting from [ null, ...row[0] ]
).您还可以执行
push(...row[0])
以避免串联数组(因为您立即使用并丢弃由[ null, ...row[0] ]
产生的副本)。
0 See official docs on how to migrate to V8 to take advantage of new language features and improved speed. 0请参阅有关如何迁移到 V8 以利用新语言功能和提高速度的官方文档。
The row
variable contains an array so you should use the spread operator with appendRow
row
变量包含一个数组,因此您应该将扩展运算符与appendRow
一起appendRow
Replace:代替:
tracker.appendRow([null,row[0]]);
with:和:
tracker.appendRow([null,...row[0]]);
Make sure your project is enabled for Chrome V8 runtime.确保您的项目已启用 Chrome V8 运行时。
Explanation:解释:
null
is clearly a workaround and not a futureproof solution.null
的方法显然是一种解决方法,而不是面向未来的解决方案。 Namely, if you want to start pasting from column 4 you would have to do [null,null,null,...row[0]]
which is not the proper way to do it in my opinion.[null,null,null,...row[0]]
,这在我看来不是正确的方法。appendRow
and null
since you want to paste the data from the second column onwards.appendRow
和null
因为你想从第二列开始粘贴数据。 Therefore, use setValues()
instead.setValues()
。 Replace:代替:
tracker.appendRow([null,row[0]]);
with:和:
tracker.getRange(tracker.getLastRow()+1,2,1,row[0].length).setValues(row);
Complete Solution:完整解决方案:
function copy2(){
var responses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("from");
var tracker = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("to");
var lastrow = responses.getLastRow();
var col = responses.getLastColumn();
var row = responses.getRange(lastrow, 1, 1, col).getValues();
tracker.getRange(tracker.getLastRow()+1,2,1,row[0].length).setValues(row);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.