【问题标题】:Google Sheets: Add editors via script in Protected Sheet and RangesGoogle 表格:通过受保护的表格和范围中的脚本添加编辑器
【发布时间】:2020-12-02 17:33:54
【问题描述】:
请问如何通过脚本将编辑器添加到受保护的工作表和范围中的权限?我尝试使用protection.addEditors();,它需要枚举所有的电子邮件地址,对吧?但是,我有没有一种简单的或另一种方法可以做到这一点?如果没有其他办法,我有90张纸要一一修改。每个文件的电子邮件地址都不相同。
我正在考虑在 Google 云端硬盘的与人员和群组共享选项中自动获取所有编辑器。这可以吗?
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
问题:
您希望以编程方式检索一系列电子表格的编辑器列表,以便将它们添加为 Protected 工作表或范围的编辑器(不确定为什么要添加此保护,因为默认情况下文件编辑器将是唯一能够编辑电子表格的人,但只要适合您)。
解决方案 - File.getEditors():
您可以通过DriveApp.getFileById(id) 获取File 来检索此列表,并且对于每个File,通过File.getEditors() 获取编辑器列表。
代码sn-p:
function addEditors() {
const spreadsheetIDs = ["spreadsheet_ID_1", "spreadsheet_ID_2", ... etc.];
spreadsheetIDs.forEach(spreadsheetID => {
const file = DriveApp.getFileById(spreadsheetID);
const editors = file.getEditors();
const spreadsheet = SpreadsheetApp.openById(spreadsheetID);
spreadsheet.addEditors(editors);
});
}