SpreadsheetApp:getRange和setValues

时间:2019-10-16 18:41:00

标签: javascript arrays google-apps-script google-sheets

我想使用Google SpreadsheetApp将2D数组写入工作表。尝试

sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);

导致以下错误:

"The number of rows in the data does not match the number of rows in the range. The data has 4 but the range has 3."

如何从这一行代码中得到这样的错误?我使用数组的维数来指定电子表格中的范围,那么怎么可能它们不匹配?

(该行是https://github.com/alpatania/sassafrasappendData_中的函数supporting_code的一部分)。

2 个答案:

答案 0 :(得分:1)

打电话时

sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);

您只是在检查2D数组第一行的长度(使用array2d[0].length)。但是,不同的行也可能具有不同的长度。您可以在下面看到复制您的问题的一小段代码:

Issue reproduction

为了继续解决您的问题,您可能会找到以下相关的超链接:

答案 1 :(得分:0)

function insert2DArrayIntoSheet() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var array=[[1,2,3],[4,5,6],[7,8,9]];
  var rg=sh.getRange(1,1,array.length,array[0].length);
  rg.setValues(array);
}