【发布时间】:2018-06-04 19:08:34
【问题描述】:
我正在尝试遍历大量单元格来测试 A 列中的值,如果返回 true,那么我想将相邻的 3 个单元格(B-D 列)存储在一个数组中,并最终将该数组粘贴到单独的工作表上.当前代码找到所有正确的值,但它将它们全部写入一行,而不是像原始数据源中的多行和 3 列。
var dataset = [],
month,
i,
j,
x = 0,
targetmonth = ss.getSheetByName("BOH").getRange("B2").getValue(),
location = ss.getSheetByName(output).getRange("D3").getValue();
for ( i = 7; i < 3000; i++){
month = ss.getSheetByName(location).getRange(i,1).getValue();
if (month == targetmonth){
for (j = 2; j<5; j++){
dataset [x] = [ss.getSheetByName(location).getRange(i,j).getValues()];
x = x + 1;
}
}
}
//I've changed the range size in line below to 360x3 which is what it should be
//but this line currently only runs when set to 1x360
ss.getSheetByName(output).getRange(8,3,360,3).setValues([dataset]);
【问题讨论】:
-
不喜欢只在 JavaScript 数组中工作?你的脚本会快很多。
-
你能告诉我一些关于这方面的资源吗? JavaScript 相对较新。
-
您选择的 JavaScript 参考和无数的网络教程都将包含大量关于
Array对象的信息。我更喜欢 Mozilla 开发者网络,但更喜欢他们自己的。至于在 Apps 脚本中使用它们,请阅读 Apps 脚本文档以了解Spreadsheet类中的最佳实践。
标签: arrays for-loop google-apps-script google-sheets