目前,我有一個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 range
爲sheet.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]);
}
}
}
我感謝所有的幫助!我會研究這些改進,並讓你知道結果! –