2016-01-19 80 views
0

目前,我有一個Google腳本,我必須在一張工作表中爲多個工作表中的多個人員保護多個範圍。保護多個範圍時Google Apps腳本運行時間非常緩慢

這裏是我現在所擁有的代碼:

function setPermissions() { 

for (var number = 1; number < 30; number++) { 

    var n = number.toString(); 

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(n); 

    var protectionDescription = 'Initial Sheet Protection - Script' 

    var protectedRangesArray = [ 
     'A1:F5', 
     'G1:G4', 
     'H1:K5', 
     'L1:L2', 
     'M1:N5', 
     'A6:P8', 
     'A7:B61', 
     'A62:P62', 
     'O9:O61', 
     'F9:F11', 
     'A1:P2' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.removeEditors(protection.getEditors()); 

    }; 

    // = Everything else is repeated from here so you really only need to look at the above code. 
    var protectedRangesArray = [ 
     'C9:E61', 
     'F12:F61', 
     'G9:H61', 
     'P9:P61', 
     'O3:P5' 

    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      saEmail, 
      amEmail, 
      bmEmail, 
      meEmail 
     ]); 

     protection.removeEditors([ 
      brEmail 
     ]); 
    }; 

    // ===== 
    var protectedRangesArray = [ 
     'K9:N61' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      bmEmail, 
      brEmail 
     ]); 

     protection.removeEditors([ 
      saEmail, 
      amEmail, 
      meEmail 
     ]); 
    }; 

    // ===== 
    var protectedRangesArray = [ 
     'G5:G5' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      amEmail, 
      bmEmail, 
      meEmail 
     ]); 

     protection.removeEditors([ 
      saEmail, 
      brEmail 
     ]); 
    }; 

    // ===== 
    var protectedRangesArray = [ 
     'L3:L3' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      amEmail, 
      bmEmail, 
      meEmail 
     ]); 

     protection.removeEditors([ 
      saEmail, 
      brEmail 
     ]); 
    }; 

    // ===== 
    var protectedRangesArray = [ 
     'L4:L4' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      bmEmail, 
      meEmail 
     ]); 

     protection.removeEditors([ 
      saEmail, 
      amEmail, 
      brEmail 
     ]); 
    }; 

    // ===== 
    var protectedRangesArray = [ 
     'L5:L5', 
     'I9:J61' 
    ]; 

    for (var i = 0; i < protectedRangesArray.length; i++) { 
     var range = sheet.getRange(protectedRangesArray[i]); 
     var protection = range.protect().setDescription(protectionDescription); 

     protection.addEditors([ 
      meEmail 
     ]); 

     protection.removeEditors([ 
      saEmail, 
      amEmail, 
      bmEmail, 
      brEmail 
     ]); 
    }; 

    }; 
}; 

理解,代碼需要長的時間。

我想弄清楚的是如何減少我在整個腳本中調用的getRange()調用的數量。據我所知,這大大減緩了腳本的速度。

我試過var protection = range[0][0].protect().setDescription(protectionDescription);,定義var rangesheet.getRange(1,1,62,16)後,但它給出了錯誤無法從不確定讀取屬性「0」。

無論如何加快這個功能?現在,我一次只做一張紙(每張紙約需5分鐘)。

編輯:這裏是更新(和更快的代碼),任何人都關心(感謝BMcV):

function setPermissions() { 

    var worksheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var protectionDescription = 'Initial Sheet Protection - Script'; 
    var protectedRangesArray = []; 
    var addEditorsArray = []; 
    var removeEditorsArray = []; 

    for (var number = 0; number < 30; number++) { 
     var sheet = worksheet.getSheetByName(number.toString()); 

     protectedRangesArray = [ 
      [//0 
       'A1:F5', 
       'G1:G4', 
       'H1:K5', 
       'L1:L2', 
       'M1:N5', 
       'A6:P8', 
       'A7:B61', 
       'A62:P62', 
       'O9:O61', 
       'F9:F11', 
       'A1:P2'], 
      [//1 
       'C9:E61', 
       'F12:F61', 
       'G9:H61', 
       'P9:P61', 
       'O3:P5'], 
      [//2 
       'K9:N61'], 
      [//3 
       'G5:G5'], 
      [//4 
       'L3:L3'], 
      [//5 
       'L4:L4'], 
      [//6 
       'L5:L5', 
       'I9:J61'] 

     ]; 

     addEditorsArray = [ 
      [], //0 
      [saEmail, amEmail, bmEmail, meEmail], //1 
      [bmEmail, brEmail], //2 
      [amEmail, bmEmail, meEmail], //3 
      [amEmail, bmEmail, meEmail], //4 
      [bmEmail, meEmail], //5 
      [meEmail] //6 
     ]; 

     removeEditorsArray = [ 
      [saEmail, amEmail, bmEmail, brEmail, meEmail], //0 
      [brEmail], //1 
      [saEmail, amEmail, meEmail], //2 
      [saEmail, brEmail], //3 
      [saEmail, brEmail], //4 
      [saEmail, amEmail, brEmail], //5 
      [saEmail, amEmail, bmEmail, brEmail] //6 
     ]; 

     protectRanges(sheet, protectionDescription, protectedRangesArray, addEditorsArray, removeEditorsArray) 

    }; 
}; 

function protectRanges(sheet, protectionDescription, protectedRangesArray, addEditorsArray, removeEditorsArray) { 
    var i = 0, n, 
    len = protectedRangesArray.length, 
    range, protection; 
    for (i; i < len; i++) { 
     n = 0 
     for (n; n < protectedRangesArray[i].length; n++) { 
      range = sheet.getRange(protectedRangesArray[i][n]); 
      protection = range.protect().setDescription(protectionDescription); 
      protection.addEditors(addEditorsArray[i]); 
      protection.removeEditors(removeEditorsArray[i]); 
     } 
    } 
} 

回答

1

有幾件事情可以幫助使劇本更加高效,最有可能的,速度更快。

利用hoisting。在這個腳本的很多領域中,變量被定義了多次。最好這樣做:

var number = 1; // Some code here involving number 
number = 10; // notice no var at the start! 

這個函數也應該分成幾個輔助函數。這將有助於使您的代碼更易讀,更易於維護。

function protectRanges(sheet, protectionDescription, protectedRangesArray) { 
    var i = 0, 
    len = protectedRangesArray.length, 
    range, protection; 
    for (i; i < len; i++) { 
     range = sheet.getRange(protectedRangesArray[i]); 
     protection = range.protect().setDescription(protectionDescription); 
     protection.removeEditors(protection.getEditors()); 
    } 
} 

這樣,至少你不必一次又一次地寫代碼。這個概念被稱爲幹(不要重複自己)。只要有可能,將重複的代碼移到單獨的函數中。現在,找到提高性能的方法會更容易。

有一件事可能會幫助將電子表格定義移到循環之外。目前,SpreadsheetApp.getActiveSpreadsheet()被稱爲30次,只需要被稱爲1次。

最主要的是簡化功能。

+0

我感謝所有的幫助!我會研究這些改進,並讓你知道結果! –