我不太同意 user1795832 的回答,因为它不使用批量写入,而是在每次条件为真时将数据写入工作表 2……那么为什么要使用数组来做到这一点?
在循环完成后尝试简单地批量写入工作表 2,它应该可以按预期工作。
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][12] == "Associate") {
dest.push(data[i]);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
编辑:在您的评论之后选择您复制的列的其他可能性
function runReportAllMemCos1() {
var sheet1 = sskey.getSheetByName('Businesses');
var sheet2 = sskey.getSheetByName('tempsheet');
var data = sheet1.getRange(1,1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][12] == "Associate") {
var destRow = []; // initialise intermediate array
destRow.push(data[i][1],data[i][2],data[i][12]);// choose here the columns you want to add (here col2, 3 & 13)
dest.push(destRow);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
sheet2.getRange(sheet2.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}