我有一个 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/