Google Script - Internal Error after 15 seconds

前端 未结 1 1274
忘掉有多难
忘掉有多难 2021-01-28 00:16

So I am writing a script that we give the sum of all the data that has a specific tag in the same row.

Col 1  |  Col 2
-------+---------
grp1   |   2
grp1   |           


        
1条回答
  •  伪装坚强ぢ
    2021-01-28 00:40

    Your problem is most likely due to the fact that you do so many calls to getRange and getValue.
    You are probably hitting your quota limit of calls.

    instead of doing that do one big call to get all the data and then work with that:

    function collectgrpdata2(group, startrow) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
      var currentcell = sheet.getActiveCell();
      var col = currentcell.getColumn();
      var range = sheet
          .getRange(startrow, 
                    col, 
                    sheet.getLastRow() - startrow + 1, 
                    sheet.getLastColumn() - col + 1)
    
      var data = range.getValues();
    
      return data
          .filter(function(row) {return row[0] === group;})
          .map(function(row) {return row[1];})
          .concat(0)
          .reduce(function(x,y) {return x+y;});
    }
    

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