我正在尝试寻找一种替代我目前正在手动执行的操作的方法,这非常累人(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(['test@gmail.com']);
}
非常感谢
答案 0 :(得分:0)
在很多帮助下,我可以按需运行它,这是主要代码:
特别感谢RENO BLAIR的巨大帮助,以及其他也想提供帮助的人(@Tanaike ++)
我共享代码,也许对某些人可能有益:
注释:
CODE.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,
});
}