You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法为受保护Google Sheets添加编辑权限,如何运行脚本?

无法修改受保护Google Sheets权限以运行脚本的问题

我需要在群组驱动器文件夹中的一组Google Sheets上运行脚本,这些工作簿已经设置了保护,我拥有工作表的编辑权限,但没有修改保护设置的权限。

我清楚无法直接在受保护的工作表上运行脚本,但逐个请求工作表所有者授予我修改保护的权限太耗时。于是我尝试编写脚本,将自己添加为受保护工作表的编辑者,以此来正常运行脚本,但尝试失败了。

我的实现代码如下:

function addEditAccess_(sheet) {
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
  for (var i = 0; i < protections.length; i++) {
    protections[i].addEditor('my email.');
  }
}

该函数接收sheet作为参数,我在以下代码片段中调用它:

while (files.hasNext()) {
  const file = files.next();
  const spreadsheet = SpreadsheetApp.open(file);
   
  const sheets = spreadsheet.getSheets();
   sheets.forEach(sheet => {
  addEditAccess_(sheet);

//rest of the code

运行后抛出错误:Exception: You do not have permission to perform that action.

我尝试过以下调整,但都无效:

  • 将SpreadsheetApp.ProtectionType.SHEET改为SpreadsheetApp.ProtectionType.RANGE
  • 使用remove()方法(在测试工作表中操作无问题,但目标表不行)

我想知道这种方式是否可行?还是必须寻找替代解决方案?


内容的提问来源于stack exchange,提问作者Serrot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:26:58