google-apps-script - 脚本在 SpreadsheetApp.openById 上失败 - 需要权限

标签 google-apps-script oauth google-api triggers

我有一个 onOpen 函数,可以在电子表格中创建一个自定义菜单。它已经运行了一年多,但几天前它停止工作。

当我查看执行记录时,我得到:

“执行失败:您无权调用 SpreadsheetApp.openById。所需权限:https://www.googleapis.com/auth/spreadsheets

这失败/与以下代码行有关:

var ss = SpreadsheetApp.openById("myID")

我检查了启用了哪些 API,并启用了 Sheets,这反射(reflect)在 .json 文件中。我在脚本中的任何地方都没有 @OnlyCurrentDoc。我已经重置了此脚本帐户的权限,再次运行该功能并允许权限,但菜单仍然不会在打开时创建。

我的 .json 文件:
{
"timeZone": "Europe/London",
"dependencies": {
"enabledAdvancedServices": [{
  "userSymbol": "Sheets",
  "serviceId": "sheets",
  "version": "v4"
 }]
},
"exceptionLogging": "STACKDRIVER"
}

最佳答案

出现此错误是因为代码没有足够的权限来运行该方法。 openById方法需要 this 中的一个或多个范围。您的 list .JSON 应如下所示:

{
  "timeZone": "Europe/London",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Sheets",
      "serviceId": "sheets",
      "version": "v4"
    }]
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets"
  ]
}

请注意我如何在文件末尾添加范围。请不要犹豫,要求对此答案进行更多说明。

关于google-apps-script - 脚本在 SpreadsheetApp.openById 上失败 - 需要权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58025650/

相关文章:

google-chrome-extension - 从 Chrome 扩展程序运行 Apps 脚本需要授权

google-apps-script - 谷歌电子表格 : Array formula if/and to return "No" if 3 previous cells are blank

python - GAE : python code to check if i'm on dev_appserver or deployed to appspot

google-maps - 如何保护我的 api key ?

google-apps-script - 将电子表格本地下载到我的电脑的脚本

google-apps-script - 谷歌应用程序脚本: a select drop down list in google sheet

asp.net - 两个不同的 OWIN 应用程序能否授权一个 OAuth2 不记名访问 token ?

java - 证书中的主机名在 OAuth 请求中不匹配

android - 发布版本 apk 上的 400 错误请求

c# - JSON.NET:获取子值?