I've created a for loop that pulls sales data in columns from an array (salesLog) and places them into a sheet (targetSheet). The sales data is kept in multiple columns. The columns between columnStart and columnEnd are added (unit), matched with the correct row in the source sheet, and placed in the correct row in the targetSheet. This loop works fine except it's too slow, and a fear unscalable with more data. I'm looking for a way to run this loop faster. Any help?
var length = POlistTarget.length;
for (var i=0; i <= length; i++){
//find Row
var row = POlistSource.indexOf(POlistTarget[i]);
//findColumns
var columnStart = periodArr[0]+1
var columnEnd = periodArr.length
var unitArr =
salesLog.getRange(row+3,columnStart,1,columnEnd).getValues().flat().filter(row=>row!="");
//add units in an array
var unit = unitArr.reduce(function(a,b){return a+b;},0);
//execute
targetSheet.getRange(i+4,7,1,1).setValue(unit);
}
getValues()andsetValuein the loop. In this case, the process cost will be high. How about modifying them? If my understanding is correct, Best Practices and stackoverflow.com/tags/google-apps-script/info might be useful. If I misunderstood your script, I apologize.filterand go withreducedirectly.This loop works finehow is that? you have anelsestatement without anifstatement there. Please provide the full code.