【发布时间】:2019-10-16 18:41:00
【问题描述】:
我想使用 Google SpreadsheetApp 将二维数组写入工作表。尝试
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/sassafras 的一部分(supporting_code 中的函数appendData_)。)
【问题讨论】:
-
这只是表示
getRange()中的行数不等于setValues()中存在的行数。所以array2d.length= 3 行,但由于某种原因,array2d有 4 行——这看起来很奇怪。更具体的唯一方法是共享电子表格的副本(当然,隐私或机密数据较少)。然后,我们可以看到实际数据,并将其与创建/创建array2d时的代码进行比较。 -
您可以通过将多语句行分成两部分并记录中间状态来帮助自己(和我们)。记录数组长度(和数组元素)的值,记录范围维度,等等。 Stackdriver logging 是你的朋友,即
console.log({message:"the data", arraylen: array2d.length, .....}) -
谢谢大家!按照@tehhowch 的建议,我记录了数组的各个行,发现并非所有行的长度都与第一行相同。所以 carlesgg97 的回答很到位
标签: javascript arrays google-apps-script google-sheets