Solution:
Methods that access the sheet like getRange()
, getValue()
and setValue()
have a lot of execution time, thus they are not recommended to be executed inside loops.
The best practice is to get all the values you need from a range to an array, manipulate that array in a loop, then write the array back to the range.
Sample Code:
function fillBlanks() {
var sheet = SpreadsheetApp.getActiveSheet();
var sheetLR = sheet.getLastRow();
var range = sheet.getRange(2,2,sheetLR-1,7);
var values = range.getValues();
for (var r = 1; r < sheetLR-1; r++) {
for (var c = 0; c < 7; c++) {
if(values[r][c] == "") {
values[r][c] = values[r-1][c];
}
}
}
range.setValues(values);
}
This should do the same thing but with only two executions on the range regardless of size.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…