1

我目前正在爲我的公司開發一個新的電子表格,以使我們每週執行的任務之一更容易 - 它的工作方式是將數據導入到此電子表格中,然後運行腳本來生成我們使用的報告。下面是我爲編制電子表格的一部分而編寫的一個腳本示例。Google Apps腳本 - 超出最長執行時間

手動導入到電子表格中的數據最多可以包含3000個信息單元,因此用於檢查單元格的循環 - 但是無論何時運行此腳本,它都將啓動到第1617行,然後向我提供超出最大錯誤執行時間 - 是否有避免這個問題,因爲我有幾個腳本需要後來運行,以幫助生成我們使用的報告。

有6張紙,每張紙都有數據輸入,取決於已導入數據中的單元格值。

function WorkoutTotals() { 
// ----------------------------------------------------------------------------- 
// This function is used for working out the totals of each centre 
// ----------------------------------------------------------------------------- 
var value; 
for (var j = 2; j < abignumber; j++) { 
    var rawcentres = rawdata.getRange(j, 1); 
    var rawcategory = rawdata.getRange(j, 6); 
    switch (rawcentres.getValue()) { 
    case centres[0]: 
     centresheet = ss.getSheetByName(sheets[0]); 
     switch (rawcategory.getValue()) { 
     case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
    case centres[1]: 
     centresheet = ss.getSheetByName(sheets[1]); 
     switch (rawcategory.getValue()) { 
     case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
    case centres[2]: 
     centresheet = ss.getSheetByName(sheets[2]); 
     switch (rawcategory.getValue()) { 
     case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
    case centres[3]: 
     centresheet = ss.getSheetByName(sheets[3]); 
     switch (rawcategory.getValue()) { 
        case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
    case centres[4]: 
     centresheet = ss.getSheetByName(sheets[4]); 
     switch (rawcategory.getValue()) { 
        case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
    case centres[5]: 
     centresheet = ss.getSheetByName(sheets[5]); 
     switch (rawcategory.getValue()) { 
        case "a1": 
      range = centresheet.getRange(cramrow, 1) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "A2": 
      range = centresheet.getRange(cramrow, 2) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a3": 
      range = centresheet.getRange(cramrow, 3) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a4": 
      range = centresheet.getRange(cramrow, 4) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a5": 
      range = centresheet.getRange(cramrow, 5) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a6": 
      range = centresheet.getRange(cramrow, 6) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a7": 
      range = centresheet.getRange(cramrow, 7) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a8": 
      range = centresheet.getRange(cramrow, 8) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a9": 
      range = centresheet.getRange(cramrow, 9) 
      range.setValue(range.getValue() + 1) 
      break; 
     case "a10": 
      range = centresheet.getRange(cramrow, 10) 
      range.setValue(range.getValue() + 1) 
      break; 
     } 
     break; 
     } 
    break; 
    } 
} 
} 

回答

1

這裏的主要問題是腳本獲取/設置每個迭代的值。相反,您可以獲取整個範圍,計算並更新範圍以縮短執行時間。這是來自Developers網站的similar example

+0

即使對於很多數據,您可能需要重寫腳本,以便使用時間觸發塊進行處理,並將其當前狀態保存在script中PropertyProperties –

+0

您是否偶然有一個我可以使用的示例?我很少使用JavaScript代碼,因此我的代碼很差= /。 – Andy

+0

@Chris是正確的,批量讀/寫應該解決這個問題。還有一些關於閱讀/書寫電子表格的教程作爲對象https://developers.google.com/apps-script/guides/sheets。如果仍然需要分塊腳本,則最近的自定義腳本庫已經發布,以執行此操作。http://patt0.blogspot.in/2014/01/using-scriptapp-to-process-batches-in.html – mhawksey

相關問題