Google脚本 - setValues问题 [英] Google Script - setValues issue

查看:95
本文介绍了Google脚本 - setValues问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为gDoc Spreadsheet编写一个脚本,并且在setValues方法中遇到了一些问题。



代码非常基本,但仍然不能正常工作



首先有一个构建的数组:

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

到目前为止没有问题(我相信),然后尝试将结果写入工作表:

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

这会带来以下错误消息:无法将(类)@ 7fb23794转换为对象



以下工作可以实现:

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

欢迎任何帮助。
Thanks。

解决方案

Range.setValues()是期待一个数组数组(二维数组),您将为它提供一组对象。另一方面,对于单个单元格, Range.setValue()需要一个对象(字符串,数字或日期)。



试试这个,它会产生一行4列的数组(匹配你的范围的维数):

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

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


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.

解决方案

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

这篇关于Google脚本 - setValues问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆