[英]Incorrect Range Error when writing to cells in google sheets
I'm using a gfx as a button and I want it to set the values from the array to a set of rows and columns. 我使用gfx作为按钮,希望它将数组中的值设置为一组行和列。 So far this is what I have. 到目前为止,这就是我所拥有的。
function loadTrades(){
var balancesSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Balances");
var exchange = balancesSheet.getRange("D1").getValue();
var trades = Gettrades(exchange);
var arr = [];
var c = [];
for (var i=0;i < trades.length-1;i++) {
c=[];
for (var j=0; j< trades[0].length;j++){
c.push(trades[i][j]);
}
arr.push(c);
}
//var arr = json2array(trades); this does the same as above but in a funtion of its own.
var destinationRange = balancesSheet.getRange("D2");
destinationRange.setValues(arr);
};
function json2array(data){
var results = [];
var keys = [];
var values = [];
for (var i in data){
for (var Key in data[i]){
if (i == 0) keys.push(Key);
values.push(data[i][Key]);
}
if (i == 0){
results.push(keys);
keys = [];
}
results.push(values);
values = [];
}
return results;
};
There is up to 500 rows in the array, and 7 columns. 阵列中最多有500行,共有7列。
I get the error. 我得到了错误。
Incorrect range height, was 3 but should be 1. 范围高度不正确,为3,但应为1。
I think I have a miss understanding of how getRange and setValues work in google sheets. 我想我对Google表格中的getRange和setValues如何工作缺少了解。
I've read through this site and a few other examples here on stackoverflow, but can't find my solution. 我已经在Stackoverflow上阅读了本站点和其他一些示例,但是找不到我的解决方案。
As the error occurs on 由于错误发生
destinationRange.setValues(arr);
the origin of the problem is on the previous line 问题的根源在上一行
var destinationRange = balancesSheet.getRange("D2");
The above because balancesSheet.getRange("D2")
returns a single cell but arr
, according to the error, has a height of 3. 上面的原因是balancesSheet.getRange("D2")
返回单个单元格,但是根据错误arr
的高度为3。
One way to solve this is replace 解决此问题的一种方法是替换
var destinationRange = balancesSheet.getRange("D2");
by 通过
var destinationRange = balancesSheet.getRange(2,4,arr.length,arr[0].length);
Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.