2016-03-30 56 views
0

我已經構建了一個腳本(onEdit),它對變化進行了大量檢查,以指示何時以及由誰進行更改,以及將行移動到特定更改的歸檔,但速度很慢,有時會在3MB左右的工作簿上崩潰。加速Google腳本中的編輯功能 - 有時無響應

請參見下面的代碼,讓我知道如果你能幫助:

function onEdit(event) { 
    var timezone = "GMT"; 
    var timestamp_format = "dd/MM/yyyy 'at' h:mm a"; // Timestamp Format.dd-MM-yyyy hh:mm:ss 
    var updateColName = "JL Comments and Actions "; 
    var timeStampColName = "Last Modified"; 
    var sheet = event.source.getSheetByName('LOG'); //Name of the sheet where you want to run this script. 
    var actRng = event.source.getActiveRange(); 
    var editColumn = actRng.getColumn(); 
    var index = actRng.getRowIndex(); 
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); 
    var dateCol = headers[0].indexOf(timeStampColName); 
    var updateCol = headers[0].indexOf(updateColName); 
    updateCol = updateCol+1; 

    if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! 
     var cell = sheet.getRange(index, dateCol + 1); 
     var date = Utilities.formatDate(new Date(), timezone, timestamp_format); 
     cell.setValue(date); 
    } 

    var updateColName1 = "JL Comments and Actions "; 
    var Acdifference ="Next Action Required by"; 
    var sheetN = event.source.getSheetByName('LOG'); 
    var actRng2 = event.source.getActiveRange(); 
    var editColumn = actRng2.getColumn(); 
    var index = actRng2.getRowIndex(); 
    var headers = sheetN.getRange(1, 1, 1, sheetN.getLastColumn()).getValues(); 
    var diffCol = headers[0].indexOf(Acdifference); 
    var updateCol1 = headers[0].indexOf(updateColName1); updateCol1 = updateCol1+1; 

    if (diffCol > -1 && index > 1 && editColumn == updateCol1) { 
     var cell = sheetN.getRange(index, diffCol + 1); 
     cell.setValue ("if"); 
    } 

    var timezone = "GMT"; 
    var timestamp_format = "dd/MM/yyyy 'at' h:mm a"; // Timestamp Format.dd-MM-yyyy hh:mm:ss 
    var updateColName = "if Comments/Actions "; 
    var timeStampColName = "Last modified by if"; 
    var sheet = event.source.getSheetByName('LOG'); //Name of the sheet where you want to run this script. 
    var actRng3 = event.source.getActiveRange(); 
    var editColumn = actRng3.getColumn(); 
    var index = actRng3.getRowIndex(); 
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); 
    var dateCol = headers[0].indexOf(timeStampColName); 
    var updateCol = headers[0].indexOf(updateColName); 
    updateCol = updateCol+1; 

    if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! 
     var cell = sheet.getRange(index, dateCol + 1); 
     var date = Utilities.formatDate(new Date(), timezone, timestamp_format); 
     cell.setValue(date); 
    } 


    var updateColName1 = "if Comments/Actions "; 
    var Aidifference ="Next Action Required by"; 
    var sheetN = event.source.getSheetByName('LOG'); 
    var actRng4 = event.source.getActiveRange(); 
    var editColumn = actRng4.getColumn(); 
    var index = actRng4.getRowIndex(); 
    var headers = sheetN.getRange(1, 1, 1, sheetN.getLastColumn()).getValues(); 
    var diffCol = headers[0].indexOf(Aidifference); 
    var updateCol1 = headers[0].indexOf(updateColName1); updateCol1 = updateCol1+1; 

    if (diffCol > -1 && index > 1 && editColumn == updateCol1) { 
     var cell = sheetN.getRange(index, diffCol + 1); 
     cell.setValue ("JLP"); 
    } 

    var timezone = "GMT"; 
    var timestamp_format = "dd/MM/yyyy 'at' h:mm a"; // Timestamp Format.dd-MM-yyyy hh:mm:ss 
    var updateColName = "Lu Comments"; 
    var timeStampColName = "Last modified by Lu"; 
    var sheet = event.source.getSheetByName('LOG'); //Name of the sheet where you want to run this script. 
    var actRng = event.source.getActiveRange(); 
    var editColumn = actRng.getColumn(); 
    var index = actRng.getRowIndex(); 
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); 
    var dateCol = headers[0].indexOf(timeStampColName); 
    var updateCol = headers[0].indexOf(updateColName); 
    updateCol = updateCol+1; 

    if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself! 
     var cell = sheet.getRange(index, dateCol + 1); 
     var date = Utilities.formatDate(new Date(), timezone, timestamp_format); 
     cell.setValue(date); 
    } 

    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var s = event.source.getActiveSheet(); 
    var r = event.source.getActiveRange(); 

    if(s.getName() == "LOG" && r.getColumn() == 21 && r.getValue() == "y") { 
     var row = r.getRow(); 
     var numColumns = s.getLastColumn(); 
     var targetSheet = ss.getSheetByName("Archived"); 
     var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); 
     s.getRange(row, 1, 1, numColumns).moveTo(target); 
     s.deleteRow(row); 
    } 
} 
+2

歡迎來到[so]。請參加[旅遊]和結帳[問]。 –

回答

1

陣列的外觀起坐每一次使用它們時採取收費上的程序的速度性能。嘗試在Google表格here中尋找優化代碼的提示。 也如here所述,爲了加速腳本,使用一個命令將所有數據讀入一個數組中,對數組中的數據執行任何操作,並用一個命令寫入數據。