I've set up a for loop to extract sales data from an array (salesLog) and transfer it to a designated sheet (targetSheet) in columns. The sales data is spread across multiple columns in the array. The loop adds up the columns between columnStart and columnEnd (unit), matches them with the corresponding row in the source sheet, and places them in the correct row in the targetSheet. While this loop does the job, it's proving to be slow and potentially inefficient with larger datasets. I'm in search of a solution to expedite this loop process. Any suggestions?
var length = POlistTarget.length;
for (var i=0; i <= length; i++){
//locate Row
var row = POlistSource.indexOf(POlistTarget[i]);
//identify Columns
var columnStart = periodArr[0]+1
var columnEnd = periodArr.length
var unitArr =
salesLog.getRange(row+3,columnStart,1,columnEnd).getValues().flat().filter(row=>row!="");
//sum up units into an array
var unit = unitArr.reduce(function(a,b){return a+b;},0);
//execute
targetSheet.getRange(i+4,7,1,1).setValue(unit);
}