我正在尝试将我的项目从一个阶段转移到另一个阶段,到目前为止,我已经取得了一些不错的进展。
当编辑名为Setup_Protections的工作表时,我有以下脚本运行:它删除所有工作表保护,然后将它们与“设置”工作表中指定的电子邮件一起重新添加(即,将这些电子邮件添加为受保护工作表的编辑者)
但是问题在于电子表格需要事先共享,这样他们才能首先访问它。有没有一种方法可以与“设置”表中输入的电子邮件同时共享文档? (无需使用需要启用Sheets API的方法,因为我将多次复制文档)
谢谢您的帮助
我的脚本:`
var environment = {
protectionConfigSheetName: "Setup_Protection",
};
// Script fires when Setup_Protection is edited
function onEdit(e) {
if (e.range.getSheet().getName() === environment.protectionConfigSheetName)
resetSpreadsheetProtections();
}
function removeSpreadsheetProtections(spreadsheet) {
[
SpreadsheetApp.ProtectionType.SHEET,
].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.gs
答案 0 :(得分:0)
最后它现在可以工作了:
在上面的代码中添加以下内容:
function addEditorsToSpreadsheetFromProtectionConfig(spreadsheet, protectionConfig) {
var editors = protectionConfig.reduce(function (accumulator, _a) {
var editors = _a.editors;
return accumulator.concat(editors);
}, []);
spreadsheet.addEditors(editors);
}
然后将以下行添加到resetSpreadsheetProtections()
:
addEditorsToSpreadsheetFromProtectionConfig(spreadsheet, protectionConfig);