简体   繁体   English

写入Google表格中的单元格时出现错误的范围错误

[英]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.

相关问题 扩展 RegexMatch 以针对整个单元格范围进行测试 - Google Sheets - Expand RegexMatch To Test Against An Entire Range Of Cells - Google Sheets 使用Google表格中某个范围内的map()发出阅读单元格的问题 - Issues reading cells using map() in a range in Google Sheets Google Sheets - 根据相应单元格范围内的数值连接单元格范围内文本的公式 - Google Sheets - Formula to Concatenate Text in Range of Cells based on Numeric Value in Corresponding Range of Cells 是否有一个谷歌表格公式可以采用一系列单元格,并按字母顺序在一个单独的列中列出所有单元格? - Is there a Google Sheets formula that can take a range of cells, and list all the cells in one separate column, alphabetically? 仅从 Google 表格中的函数返回一系列单元格(在所需的索引范围内) - Return only a range of cells (within desired index range) from a function in Google Sheets 如何编辑嵌套数组并将更改设置为 Google 表格中的一系列单元格? - How do I edit a nested array and set changes to a range of cells in Google Sheets? Google脚本错误:使用setValues时“范围宽度不正确” - Google Script Errror: “Incorrect range width” when using setValues 检查数字是否在范围内 - Google 表格 - Check if number in range - Google sheets Google表格中一行中的数字范围 - Range of numbers in a row in Google Sheets 基于 Google 表格中的下拉列表填充单元格 - Populating Cells Based on Dropdowns in Google Sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM