是否可通过脚本为Google Sheet多个工作表批量设置编辑权限?
问题答复
该需求完全可以通过Google Apps Script实现,核心是调用Google Sheets的保护范围API完成批量权限配置,具体实现逻辑和参考代码如下:
实现逻辑
- 先对X、Y、Z三个工作表设置全局保护,仅保留所有者的编辑权限,移除所有其他用户的编辑权限
- 再单独给每个工作表的B6:Q50区域设置权限,将60名目标用户添加为该区域的可编辑者
- 所有配置可通过脚本批量完成,不需要手动逐个调整权限
参考代码
打开目标Google Sheet,点击顶部菜单栏「扩展程序」-「Apps脚本」,替换默认代码后运行即可:
function batchSetSheetPermissions() { // 可根据实际需求修改以下配置项 const targetUserEmails = [ "user01@example.com", "user02@example.com", // 依次补全剩余58个用户邮箱即可 ]; const targetSheetNames = ["X", "Y", "Z"]; const editableRangeA1Notation = "B6:Q50"; const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); targetSheetNames.forEach(sheetName => { const currentSheet = spreadsheet.getSheetByName(sheetName); // 跳过不存在的工作表 if (!currentSheet) return; // 保护整个工作表,仅所有者可编辑 const fullSheetProtection = currentSheet.protect().setDescription(`全表锁定_${sheetName}`); // 移除所有者之外的所有编辑者 fullSheetProtection.getEditors().forEach(editor => { if (!editor.isOwner()) fullSheetProtection.removeEditor(editor); }); // 配置指定区域的编辑权限 const editableRange = currentSheet.getRange(editableRangeA1Notation); const rangeProtection = editableRange.protect().setDescription(`可编辑区域_${sheetName}`); // 先清理区域默认的多余权限 rangeProtection.getEditors().forEach(editor => { if (!editor.isOwner()) rangeProtection.removeEditor(editor); }); // 批量添加目标用户为区域编辑者 rangeProtection.addEditors(targetUserEmails); }); }
注意事项
- 运行脚本的账号必须是该Google Sheet的所有者,否则没有权限修改保护规则
- 首次运行脚本需要按提示完成权限授权,授权时选择你当前的表格所有者账号即可
- 运行前建议先备份表格,避免配置错误影响原有内容
内容的提问来源于stack exchange,提问作者Matheus Felipe Bernardes
相关产品推荐
相关产品推荐

