简体   繁体   English

appendRow 返回 [Ljava.lang.Object;@ 而不是值

[英]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, thejava.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:解释:

  • The approach of using 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.也就是说,如果您想从第 4 列开始粘贴,则必须执行[null,null,null,...row[0]] ,这在我看来不是正确的方法。
  • I would advice you to get rid of appendRow and null since you want to paste the data from the second column onwards.我建议你去掉appendRownull因为你想从第二列开始粘贴数据。 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.

相关问题 消息说 [Ljava.lang.Object] 被传递代替值 - Message saying [Ljava.lang.Object] being delivered in place of values “[Ljava.lang.Object;@”是什么意思? - What does "[Ljava.lang.Object;@" mean? 如何使用sheets.AppendRow? 我一直在所有单元格中收到错误 [Ljava.lang.Object;@29080d11 - How do I use sheets.AppendRow? I keep receiving an error [Ljava.lang.Object;@29080d11 in all the cells Google Sheet 导入 Ljava.lang.Object 错误 - Google Sheet Import Ljava.lang.Object error 在 Apps 脚本中接收 [Ljava.lang.Object(将附件名称从 Gmail 带到表格) - Receiving [Ljava.lang.Object in Apps Script (bringing attachment name from Gmail to Sheets) JDBC 错误:AbstractMethodError:com.microsoft.sqlserver.jdbc.SQLServerConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array - JDBC ERROR : AbstractMethodError: com.microsoft.sqlserver.jdbc.SQLServerConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array java.lang.NoSuchMethodError: jdk.nashorn.internal.runtime.ECMAException.getEcmaError()Ljava/lang/Object; - java.lang.NoSuchMethodError: jdk.nashorn.internal.runtime.ECMAException.getEcmaError()Ljava/lang/Object; Javascript console.log返回c()而不是对象的值 - Javascript console.log returns c() instead of the values of the object 函数返回函数而不是对象 - Function returns function instead of object $。 template返回一个对象而不是一个函数 - $. template returns an object instead of a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM