为什么我无法通过 ID 打开已安装我的附加组件的文件?

Why can't I open a file by ID that I have installed my add-on in?

根据 Apps 脚本 documentation:

openById(id)

Opens the spreadsheet with the given ID. ...

...

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

我想将 openByIdspreadsheets.currentonly 范围一起使用,因为它适合我的需要。但是,即使在将范围添加到 appsscript.json 清单之后,以下代码对我来说仍然失败:

const sheet_id = SpreadsheetApp.getActiveSpreadsheet().getId();
SpreadsheetApp.openById(sheet_id);

Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets

我知道我只能使用 https://www.googleapis.com/auth/spreadsheets 范围,但由于文档说我应该能够使用 spreadsheets.currentonly,这就是我想要做的。我在这里做错了什么?


更新:

是相关的,但建议的解决方法对我不起作用。我上面分享的代码片段是在脚本编辑器中执行的。我也不打算创建自定义函数。我打算 运行 在网络应用程序脚本的 doPost(e) 函数中使用此代码。

OAuth 范围 ID 有时可以访问 link,它可以提供有关范围的有用信息。

导航到 https://www.googleapis.com/auth/spreadsheets.currentonly 后我收到以下消息:

You are receiving this error either because your input OAuth2 scope name is invalid or it refers to a newer scope that is outside the domain of this legacy API.

This API was built at a time when the scope name format was not yet standardized. This is no longer the case and all valid scope names (both old and new) are catalogued at https://developers.google.com/identity/protocols/oauth2/scopes. Use that webpage to lookup (manually) the scope name associated with the API you are trying to call and use it to craft your OAuth2 request.

最终,我 here 列出了工作表的以下范围 API:

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/spreadsheets
  • https://www.googleapis.com/auth/spreadsheets.readonly

https://www.googleapis.com/auth/spreadsheets.currentonly 范围似乎不再有效。我怀疑这是由于表格的第 3 版 API 在 2021 年 8 月 2 日(几周前撰写本文时)达到了它的日落日期而发生的。也许范围没有转移到工作表的第 4 版 API。

这个问题当然值得进一步调查。我建议您使用 Google 的问题跟踪器使用以下 link:

报告问题

https://issuetracker.google.com/components/191608.