How can I access the contents of a (new-style) Google sheet a JSON? My aim is to access the values from JavaScript, so I need to be able to download the JSON via HTTP.

示例:如何从this sheet下载JSON格式的数据?

I tried to find the answer via a web search, but ultimately failed:

推荐答案

If you want to use the latest API (v4), you'll need to do the following:

  1. 生成Electron 表格API密钥(参见下面的说明).
  2. Make your sheet publicly accessible.
  3. Use a request of the form:

    https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID/values/RANGE?key=API_KEY
    

然后,您将得到一个干净的JSON响应:

{
  "range": "Sheet1!A1:D5",
  "majorDimension": "ROWS",
  "values": [
    ["Item", "Cost", "Stocked", "Ship Date"],
    ["Wheel", "$20.50", "4", "3/1/2016"],
    ["Door", "$15", "2", "3/15/2016"],
    ["Engine", "$100", "1", "30/20/2016"],
    ["Totals", "$135.5", "7", "3/20/2016"]
  ],
}

请注意,如果要指定页面的全部内容,则标识符(如Sheet1)是perfectly valid.

See Basic Reading for more information.


As of v4 API, all requests must be accompanied by an identifier (e.g. API key):

Requests to the Google Sheets API for public data must be accompanied by an identifier, which can be an API key or an access token.

Follow the steps in the linked document to create an API key on the credentials page.

确保:

  1. Create a new app on Google Cloud Platform.
  2. 创建一个新的API密钥.
  3. Add the Google Sheets API. (API Manager > Dashboard > Enable API)

Note that you can still access public data without forcing the user to log in:

在新版API v4中,没有明确的可见性声明.API调用是使用Electron 表格ID进行的.如果应用程序没有访问指定Electron 表格的权限,则返回错误.否则通话将继续.

Note that you do not need to publish the sheet to the web. All you need to do is make sure anyone with the link can access the sheet.

(也就是说,当你在Google Sheets API上点击Create credentials时, Select 其他非UI用户数据,它会显示"用户数据不能从没有UI的平台上访问,因为它需要用户交互才能登录."你可以放心地忽略这个信息.API密钥是您真正需要的,因为这是公共数据.)


Common error messages:

The request is missing a valid API key.

You didn't include the key= param in your call.

API key not valid. Please pass a valid API key. Google developers console

You supplied an incorrect API key. Make sure that you typed in your key correctly. If you don't have a key yet, go to the Google developers console and create one.

API Key not found. Please pass a valid API key.
Google developer console API key

您的API密钥可能是正确的,但您很可能没有添加Google Sheets权限.转到Google开发者控制台API密钥页面,添加表单权限.

呼叫方没有权限

Your sheet isn't set to be publicly accessible.

Json相关问答推荐

在Jenkins中使用ReadJSON读取json子元素

Golang返回的JSON顶级字段是可变的.如何在 struct 中使用

Flutter -控制器问题-JSON API

给定一个包含两个数组的JSON输入文件,如何使用Jolt将一个数组中的每个元素与另一个数组组合在一起?

将PNG图像保存为Python中的JSON文件

在 VS Code 中将一个正则表达式替换为另一个正则表达式

展平多个数组以保持顺序

使用 jq 工具将文本从 txt 文件转换为 json

如何从 JSON 中获取数据并通过 vb6 在网格中显示

如果 jq 数组中的字符串是对象或字符串,则获取值

在 Flutter 中将对象转换为可编码对象失败

Play Framework:如何序列化/反序列化与 JSON 的枚举

反序列化大型 json 对象的 JsonMaxLength 异常

为什么我不能在 C# 中引用 System.Runtime.Serialization.Json

如何从Typescript 中的json响应中获取日期对象

如何将 Swift 对象转换为字典

将 CoffeeScript 项目转换为 JavaScript(不缩小)?

Java循环遍历Json数组?

使用 Codable 序列化为 JSON 时的 Swift 字符串转义

用 JSON 编写 HTML 字符串