'App Script Array Push() in loop Optimization
I'm looking to improve my array push inside loops but I noticed that every time my function is being called (in a loop manner) and executing this lines of code, it gets slower and slower. It is fast when I run this function once, but it gets longer when ran multiple times via loop.
My goal is to get the values from spreadsheet and assign them to an array with a specific arrangement so I can retrieve those data accordingly.
I need to have the spreadsheet call in the loop as I need to extract the address from the array parameter.
function testing(dataCollected){
var i=0;
var finalArray=[];
while(i <=dataCollected.length){
projCode = dataCollected[i][0];
unitNo = sheet.getRange(dataCollected[i][1]).getValues();
hours = sheet.getRange(dataCollected[i][2]).getValues();
for (var j=0; j<=81; j++){
finalArray.push([projCode,unitNo[j][0],hours[j][0]);
}
i++;
}
}
Does using push() create an overhead in this situation as it gets larger as I append row in it? Can someone advise what is the best approach to do in this setup. Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
