【发布时间】:2022-01-24 08:00:54
【问题描述】:
具有多个标签的电子表格有 10 个编辑者/访问权限。我想锁定其中 8 个编辑器在每个选项卡中的一些范围。
当我运行下面编写的 google app 脚本时,它还锁定了 8 个编辑器,无法编辑选项卡名称甚至选项卡颜色。可以告诉我哪里出错了吗?我希望他们仍然能够更改选项卡的名称和颜色。
function addClassProtectionFor_Current(){ //Main function to run
var currentclasstab = SpreadsheetApp.getActiveSheet();
// Remove all range protections in the spreadsheet
var protections = currentclasstab.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
protection.remove();
}
var protection = currentclasstab.protect();
//restrict editors to owner
protection.getRange().getA1Notation();
var eds = protection.getEditors();
protection.removeEditors(eds);
//Add Editors to give access to the protected ranges
protection.addEditors(["me.gmail.com","you@gmail.com"]);
//set unprotected ranges
var ranges = protection.getUnprotectedRanges();
//Ranges to leave unlocked
var data = ["A5:V19","B23:V26","B29:V35","B39:V45"];
data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
ranges.push(currentclasstab.getRange(res));
protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
});
}
【问题讨论】:
-
我必须为我糟糕的英语水平道歉。不幸的是,我无法理解你的问题。您想从用户那里锁定您的 Google 电子表格中的哪些内容?
-
我想保护除 A5:V19,B23:V26,B29:V35,B39:V45 范围之外的所有单元格,来自 8 个用户。其他 2 个用户可以访问所有内容。我的谷歌脚本就是这样做的,但它也阻止了 8 个用户更改选项卡名称和颜色。如何保护工作表(某些单元格除外)但仍允许 8 个用户更改选项卡名称或更改选项卡颜色。
-
感谢您的回复。我不得不再次为我糟糕的英语水平道歉。对于您的回复,我想确认我的理解。在您的目标中,您希望锁定用户的单元格
A5:V19,B23:V26,B29:V35,B39:V45,并且您希望锁定以更改用户的选项卡名称和选项卡颜色。我的理解正确吗? -
锁定:所有单元格。解锁:A5:V19,B23:V26,B29:V35,B39:V45 并允许所有用户编辑标签名称和颜色。
-
感谢您的回复。根据您的回复,我提出了一个修改后的脚本作为答案。你能确认一下吗?如果我误解了您的问题并且没有用,我深表歉意。
标签: google-apps-script google-sheets protected