Skip to content Skip to sidebar Skip to footer

Code Runs Too Slow

I'm trying to run a code that copies values from one spreadsheet and copies them to another, however the order is not the same(hard to make it an array). In some cases it also prin

Solution 1:

The code should be optimised:

  1. You do all calculations in a loop
  2. You use getValue and setValue instead of faster functions getValues, setValues

Instead of this concentrate your loop to do a single call:

var rowInt = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow()

try to figure out how to find the first row outside the loop and then increment this value:

var rowStart = destinationSheet.getRange(destinationSheet.getLastRow()+1, 4,1,1).getRow();

for (var row = rowStart; row <= lastRow, row++)
{
  // some code...
}

Use arrays and then copy the value from arrays into ranges:

var formulas = [];

for (var row = rowStart; row <= lastRow, row++)
{
  // some code...
  formulas.push(['=Month(D'+ row + ')']);
}
var rangeToPateFormulas = destinationSheet.getRange('A' + rowStart + ':A' + lastRow);
rangeToPateFormulas.setFormulas(formulas);

And so on. See more info:

https://developers.google.com/apps-script/reference/spreadsheet/range

https://developers.google.com/apps-script/guides/support/best-practices


Post a Comment for "Code Runs Too Slow"