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

如何限制Google Apps Script仅访问当前Google表格权限?

解决Google表格宏权限过度宽泛的问题

问题描述

开发供第三方使用的Google表格宏时,尝试插入以下代码后,仍无法限制权限范围,希望脚本仅请求当前表格的访问权限,避免用户因过度权限产生不适:

/**
 * @OnlyCurrentDoc
 * @NotOnlyCurrentDoc
 * @include currentonly
 * @exclude currentonly
 */
// Configuration for OAuthScopes
var CONFIG = {
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets.currentonly"
  ]
};

修正方案

  1. 清理冲突注释标签

    • 移除@NotOnlyCurrentDoc、@include currentonly、@exclude currentonly这些与@OnlyCurrentDoc冲突的标签,仅保留@OnlyCurrentDoc——这是官方指定的、用于将权限限制在当前文档的注释标签:
      /**
       * @OnlyCurrentDoc
       */
      
  2. 正确配置OAuth权限范围

    • 脚本内定义CONFIG变量的方式不生效,需直接修改项目的appsscript.json文件:
      • 在脚本编辑器顶部菜单栏点击「查看」→「显示清单文件」,打开appsscript.json
      • 修改oauthScopes字段,仅保留目标权限:
        {
          "dependencies": {},
          "exceptionLogging": "STACKDRIVER",
          "runtimeVersion": "V8",
          "oauthScopes": [
            "https://www.googleapis.com/auth/spreadsheets.currentonly"
          ]
        }
        
  3. 验证权限效果

    • 保存脚本与清单文件后重新运行宏,此时请求的权限会显示为「查看和编辑您当前打开的表格」,而非所有表格的宽泛权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:02:04