我希望从三列中选择数据并写入另一张表。
我的代码出了什么问题?
function values() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getSheets()[0],
t = ss.getSheetByName('o'),
range = sheet.getDataRange(),
values = range.getValues(),
x = 2;
for (var r = 1; r < values.length; r++) {
var row = values[r],
n1 = row[1],
n2 = row[5],
n3 = row[9],
array = [n1,n2,n3]
for (var y = 0; y < 2; y++) {
var u = x + y;
t.getRange(u, 1).setValue(array[y]);
}
}
}
&#13;
答案 0 :(得分:0)
最好将值写入数组,然后在一次调用中将数组写入工作表。另见:here.
所以要优化脚本,你可以尝试
function values() {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
sheet = ss.getSheets()[0],
t = ss.getSheetByName('o'),
range = sheet.getDataRange(),
values = range.getValues(),
x = 2,
array =[ ];
for (var r = 1; r < values.length; r++) {
var row = values[r],
n1 = row[1],
n2 = row[5],
n3 = row[9];
array.push([n1,n2,n3]);
}
t.getRange(2, 1, array.length, array[0].length).setValues(array);
}