Google sheet script, times out. Need a new way or flip it upside down

前端 未结 1 1935
一生所求
一生所求 2021-01-27 16:52

noob here, so the script works great if there are less than 800 rows on a sheet, however this time I have a sheet of almost 1500 rows and the script times out.

Basically

相关标签:
1条回答
  • 2021-01-27 17:12

    Your problem is row:

    s.getRange("H"+i).getValue()
    

    This code takes data from spreadsheet, this is very slow process when you use it inside loop. You may use this conctruction:

      var data = s.getDataRange().getValues();
    
      for (var i=0; i < data.length; i++) {
          var status = data[i][7]; // takes column H          
          // other code goes here...          
      }
    

    This way you read data from the spreadsheet only once using getDataRange(), and convert it into array with getValues(). Then loop should work way more faster.

    When hiding rows, remember to add 1 because array starts from 0, heres code for hiding rows inside loop:

       if (status == "") { 
         s.hideRows(i + 1); // adding 1
      }
    
    0 讨论(0)
提交回复
热议问题