google-apps-script - 如何使用 Spreads.readonly 而不是完整的读/写身份验证范围

标签 google-apps-script google-sheets google-sheets-api

我正在使用 Google Apps 脚本为我的组织创建一个网络应用程序。应用程序的功能归结为从不同的工作表读取数据并将其显示给用户。

使用 SpreadsheetApp 服务的文档似乎要求您提供完整的读/写范围才能打开工作表;通过 ID 或 URL,即 openById() 或 openByURL() 需要:

    https://www.googleapis.com/auth/spreadsheets

但是我只想使用以下范围。

    https://www.googleapis.com/auth/spreadsheets.readonly

我觉得在 Apps 脚本中应该很容易做到这一点,但我在文档中找不到任何相关内容。

我找到了一些与我想要的相关的东西here 。然而,这似乎非常复杂,需要管理 API key 。

Google Apps 脚本是否提供“简单”只读界面,或者我需要将我的项目移植到标准 Google 云项目、启用 Sheets API、找出 API key 并了解如何应用 oauth 范围与 appsscript list 分开?

最佳答案

问题和解决方法:

遗憾的是,现阶段,使用电子表格服务(SpreadsheetApp)时,需要使用https://www.googleapis.com/auth/spreadsheets的范围。看来这是当前的规范。例如,之前使用DriveApp时,需要使用https://www.googleapis.com/auth/drive的范围。但是,通过 Google 端的更新,当使用 DriveApp.getFiles() 时,可以使用 https://www.googleapis.com/auth/drive.readonly用过的。所以,我认为电子表格服务的情况可能会在未来的更新中发生改变。

但是,在现阶段,需要使用当前的规范。因此,需要使用解决方法。在此答案中,我想提出一种在 https://www.googleapis.com/auth/spreadsheets.readonly 范围内使用电子表格的解决方法。

用法:

1。准备一个 Google Apps 脚本项目。

请创建一个新的 Google Apps 脚本项目。在这种情况下,您可以使用独立类型和容器绑定(bind)脚本类型。在这个答案中,我想使用独立类型。如果您想直接创建,请访问https://script.new 。通过此,您可以看到Google Apps脚本的脚本编辑器。请设置文件名。

2。启用表格 API。

为了使用 https://www.googleapis.com/auth/spreadsheets.readonly 范围从电子表格中检索值,需要使用 Sheets API。因此,请在高级 Google 服务中启用 Sheets API。 Ref

3。准备 list 文件。

在使用脚本之前,请设置 list 文件(appsscript.json)。因此,请显示 list 文件。 Ref

并且,请添加您的预期范围,如下所示。您可以看到 Sheets API 已包含在 enabledAdvancedServices 中。请添加 "oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.readonly"],如下所示。

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

4。准备示例脚本。

请将以下脚本复制并粘贴到脚本编辑器中,并设置 spreadsheetId,然后保存脚本。

function myFunction() {
  const spreadsheetId = "###"; // Please set your Spreadsheet ID.

  const res = Sheets.Spreadsheets.get(spreadsheetId);
  console.log(res);
}
  • 关于“方法:spreadsheets.get”,这是您的问题。

5。测试。

当您运行myFunction时,脚本就会运行。并且,返回电子表格的值。这样,您就可以使用 Sheets API 从电子表格中检索值,范围为 https://www.googleapis.com/auth/spreadsheets.readonly

注意:

  • https://www.googleapis.com/auth/spreadsheets.readonly 的范围可用于从电子表格中检索值。当您想要更新电子表格时,需要使用https://www.googleapis.com/auth/spreadsheets的范围。请注意这一点。

引用:

关于google-apps-script - 如何使用 Spreads.readonly 而不是完整的读/写身份验证范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74538238/

相关文章:

ios - 如何通过 Google Sheets API 进行基本写作?

google-apps-script - 序列化延续 Google Apps 脚本时出现意外异常

google-apps-script - 如何格式化非英语日期

javascript - 使用查询公式编写 setFormula()

javascript - 将项目添加到逗号分隔列表的自定义 Google 表格功能

javascript - Google Sheets Appscript 弹出提示响应并隐藏工作表,直到响应正确

google-sheets-api - Google Spreadsheet API v4,是否可以获取修订内容?

python - 谷歌表格 Python API : how get values in first sheet if don't know range?

javascript - 为什么我总是收到“找不到脚本函数 : sendInvoiceIfNew when I use the initializeTrigger function?”