【问题标题】:404 while trying to fetch sheet data as json尝试以 json 格式获取工作表数据时出现 404
【发布时间】:2021-08-16 01:27:03
【问题描述】:

我正在尝试通过将工作表内容调用为 json 端点来加载它。

fetch("https://spreadsheets.google.com/feeds/list/MYSHEETID/1/public/values?alt=json")
                
                .then(res => res.json())
                .then(json => {
                    const data = [] /* this array will eventually be populated with the contents of the spreadsheet's rows */

我正在使用https://benborgers.com/posts/google-sheets-json中描述的方法

它在大约 30% 的时间里工作,而我在另外 70% 的时间里得到 404。我已经为此苦苦挣扎了好几天,不知道为什么会失败。

【问题讨论】:

  • 我今天也多次收到此错误...我已将控制台日志记录添加到应用程序以跟踪和输出fetch 的结果,并注意到它有时有效,有时无效。也许是谷歌的问题? (我现在无法为此构建调试测试,因此无法确认)
  • 能否提供完整的代码?

标签: google-sheets-api


【解决方案1】:

我更改为 API V4,但我注意到您提供的链接的作者更新了代码。

https://benborgers.com/posts/google-sheets-json

const spreadsheetId = '...'
fetch(`https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:json`)
    .then(res => res.text())
    .then(text => {
        const json = JSON.parse(text.substr(47).slice(0, -2))
    })

【讨论】:

    【解决方案2】:

    您需要共享您的电子表格,以便至少让所有人都能阅读。那么

    function getJson(id,gid){
      var txt = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:json&tq&gid=${gid}`).getContentText();
      var jsonString = txt.match(/(?<="table":).*(?=}\);)/g)[0]
      var json = JSON.parse(jsonString)
      return(json)
    }
    
    • 带有电子表格的 id 和工作表的 gid(可能为 0)
    • 请注意,第一行专用于标签,然后 rows[0] 对应于第 2 行,c[0] 对应于 A 列
    • 如果要检索 B1(第 2 列第 1 行):json.cols[1].label
    • 如果要检索 B2(第 2 列第 2 行):json.rows[0].c[1].v

    【讨论】:

      【解决方案3】:

      json feed 是旧版 Sheets API,它已被关闭。在此处查看 Google 的公告https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api

      【讨论】:

        猜你喜欢
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 2020-01-21
        • 2016-11-23
        • 2021-12-13
        • 2022-09-24
        • 2018-01-25
        • 1970-01-01
        相关资源
        最近更新 更多