我正在尝试找到我现在手动执行的操作的替代方法,这非常令人筋疲力尽(109 个电子表格,每个电子表格包含许多选项卡),那就是允许编辑特定编辑器的特定选项卡(整个电子表格与他们一起共享)使用脚本。
我有一个带有选项卡 (A,B,C,D,....) 的电子表格,这些选项卡包含许多受保护的范围(例如 A!1:2),除了我自己之外的所有人都应该受到保护。但是,每个选项卡还有一个(整个)工作表保护,我将在其中选择(手动)允许在该选项卡内进行编辑的用户(这是我在此处找到的一个技巧,以防止同一电子表格的许多编辑者能够编辑受保护的选项卡)。
使用脚本:是否有可能有一个工作表设置,我将按特定顺序输入当前编辑器的电子邮件(例如,包含选项卡名称的列,旁边是被允许的人的电子邮件)编辑它们)?
添加了以下脚本
function SetProtection() {
var ss = SpreadsheetApp.getActive();
//removes sheet protection
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if (protection.canEdit()) {
protection.remove();
}
}
var sh7 = ss.getSheetByName("Sheet1");
var protection = sh7.protect().setDescription('Whole Sheet Protected');
//protects whole sheet
protection.addEditors(['[email protected]']);
}
非常感谢
在很多帮助下,我得到了它的正常工作,这是主要代码:
特别感谢 RENO BLAIR 的巨大帮助,以及其他任何试图提供帮助的人 (@Tanaike ++)
我分享了代码,也许对某些人有用:
笔记:
代码.gs
var environment = {
protectionConfigSheetName: "Setup_Protection",
};
// Script fires when the Setup_Protection SHEET is edited
function onEdit(e) {
if (e.range.getSheet().getName() === environment.protectionConfigSheetName) resetSpreadsheetProtections();
}
function removeSpreadsheetProtections(spreadsheet) {
[
SpreadsheetApp.ProtectionType.SHEET,
//SpreadsheetApp.ProtectionType.RANGE, // I don't want to remove the Range Protections that I will set up in each tab
].forEach(function (type) {
return spreadsheet.getProtections(type).forEach(function (protection) { return protection.remove(); });
});
}
function getProtectionConfig(spreadsheet) {
var protectionConfigSheetName = "Setup_Protection";
var sheet = spreadsheet.getSheetByName(environment.protectionConfigSheetName);
var values = sheet.getDataRange().getValues();
var protectionConfig = values
.slice(1)
.reduce(function (protectionConfig, _a) {
var targetSheetName = _a[0], emailAddress = _a[1];
var config = protectionConfig.find(function (_a) {
var sheetName = _a.sheetName;
return sheetName === targetSheetName;
});
var editors = emailAddress.split(",");
if (config)
config.editors = config.editors.concat(editors);
else
protectionConfig.push({
sheetName: targetSheetName,
editors: editors.slice()
});
return protectionConfig;
}, []);
return protectionConfig;
}
function setSpreadsheetProtections(spreadsheet, protectionConfig) {
spreadsheet.getSheets().forEach(function (sheet) {
var protection = sheet.protect();
protection.removeEditors(protection.getEditors().map(function(editor) {
return editor.getEmail();
}));
var currentSheetName = sheet.getName();
var config = protectionConfig.find(function (_a) {
var sheetName = _a.sheetName;
return sheetName === currentSheetName;
});
if (config)
protection.addEditors(config.editors);
});
}
function resetSpreadsheetProtections() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var protectionConfig = getProtectionConfig(spreadsheet);
removeSpreadsheetProtections(spreadsheet);
setSpreadsheetProtections(spreadsheet, protectionConfig);
}
还有另一个名为 Polyfill 的文件(也是需要的):
Polyfill.gs
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, "find", {
value: function(predicate) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== "function") {
throw new TypeError("predicate must be a function");
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];
// 5. Let k be 0.
var k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
configurable: true,
writable: true,
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句