Code runs too slow

后端 未结 1 1694
感情败类
感情败类 2021-01-21 15:41

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

1条回答
  •  醉梦人生
    2021-01-21 16:05

    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

    0 讨论(0)
提交回复
热议问题