【发布时间】:2021-12-10 05:28:26
【问题描述】:
我仍在学习 Javascript 的基础知识,但我知道有一种方法可以简化这一点,但我就是想不通。我修改了在这里找到的脚本:Protect spreadsheet then unprotect specific cells and ranges with script
我正在与多个用户共享一张工作表,并且希望每张工作表的大部分内容都受到保护。上面的链接脚本帮助我确保我的用户需要访问的范围可以被所有人编辑,即使工作表扩展也是如此。但是,每张纸上的可编辑范围都不同,所以我最终回收了unlockCertainRanges() 部分以将其单独应用于每张纸。这使得脚本最多需要 70 秒才能运行。我很确定我可以使用一个数组和一个 for 循环来遍历每张纸,但我无法弄清楚。
这是我目前所拥有的:
function mainProtection(){ //Main function to run
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var disregard = ["NetSuite INV", "Sales", "Delivery Schedule", "TO", "APP Arch", "ACC Arch", "INV REF"]; //ADD SHEET NAMES HERE THAT YOU WANT TO BE DISREGARDED
for(var x=0; x<sheets.length; x++){
if(disregard.some(data => sheets[x].getName().includes(data))){
//E.g. Disregard any sheet names added on the "disregard" array
}else{
unlockOrderingRanges(sheets[x]);
unlockPendingTORanges(sheets[x]);
unlockAccessoryRanges(sheets[x]);
unlockApparelRanges(sheets[x]);
}
}
}
function unlockOrderingRanges(){ //Function to unlock ranges on Ordering spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Ordering");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["O5:P", "C2:E2"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
为了简化这篇文章,我省略了 unlockPendingTORanges();、unlockAccessoryRanges(); 和 unlockApparelRanges(); 脚本,因为它们与 unlockOrderingRanges() 脚本相同,它们只是更改了定义的工作表名称和范围。
非常感谢任何指导!
unlockPendingTORanges();、unlockAccessoryRanges(); 和 unlockApparelRanges(); 的 ETA 详细信息
function unlockPendingTORanges(){ //Function to unlock ranges on Pending TOs spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Pending TOs");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E6:H"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
function unlockAccessoryRanges(){ //Function to unlock ranges on Accessory INV spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Accessory INV");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E5:H"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
function unlockApparelRanges(){ //Function to unlock ranges on Apparel INV spreadshseet
var sheet = SpreadsheetApp.getActive().getSheetByName("Apparel INV");
// Remove all range protections in the spreadsheet
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = sheet.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
var data = ["E5:F"]; // ADD YOUR RANGES HERE
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(sheet.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
【问题讨论】:
-
可以问一下
they just change the defined sheet name and ranges.的详细信息吗? -
@Tanaike,我编辑了原始帖子以澄清这些。抱歉,如果不清楚,我试图缩短帖子。谢谢!
标签: javascript google-apps-script google-sheets google-sheets-api