【问题标题】:Can't access JSON data from google sheets API link无法从谷歌表格 API 链接访问 JSON 数据
【发布时间】:2021-09-27 08:09:04
【问题描述】:

谷歌表格 API v3 已被弃用。该链接不再有效https://spreadsheets.google.com/feeds/list/<spreadsheet_id>/od6/public/values?alt=json。它显示此错误“Sheets v3 API 已被拒绝。更多信息可在以下位置找到:https://clo”ud.google.com/blog/products/g-suite/migrate-your-apps-使用最新表格 API" 谁能帮我更新 v4 的链接格式来访问 JSON 数据?

【问题讨论】:

    标签: json api google-sheets


    【解决方案1】:

    新的网址是

    var url = 'https://docs.google.com/spreadsheets/d/'+id+'/gviz/tq?tqx=out:json&tq&gid='+gid;
    

    这里是解析的例子

    var id = '______your_speadsheet_id________';
    var gid = '0';
    var url = 'https://docs.google.com/spreadsheets/d/'+id+'/gviz/tq?tqx=out:json&tq&gid='+gid;
    fetch(url)
      .then(response => response.text())
      .then(data => document.getElementById("json").innerHTML=myItems(data.substring(47).slice(0, -2))  
      );
    function myItems(jsonString){
      var json = JSON.parse(jsonString);
      var table = '<table><tr>'
      json.table.cols.forEach(colonne => table += '<th>' + colonne.label + '</th>')
      table += '</tr>'
      json.table.rows.forEach(ligne => {
        table += '<tr>'
        ligne.c.forEach(cellule => {
            try{var valeur = cellule.f ? cellule.f : cellule.v}
            catch(e){var valeur = ''}
            table += '<td>' + valeur + '</td>'
          }
        )
        table += '</tr>'
        }
      )
      table += '</table>'
      return table
    }
    

    在此处查看公告https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api

    一个完整的html应用程序

    <html>
    <title>Google Sheets json endpoint V4</title>
    <author>Mike Steelson</author>
    <style>
    table {border-collapse: collapse;}
    th,td{border: 1px solid black;}
    </style>
    <body>
    <div id="json">json here</div>
    <script>
    var id = '1n-rjSYb63Z2jySS3-M0BQ78vu8DTPOjG-SZM4i8IxXI';
    var gid = '0';
    var url = 'https://docs.google.com/spreadsheets/d/'+id+'/gviz/tq?tqx=out:json&tq&gid='+gid;
    fetch(url)
      .then(response => response.text())
      .then(data => document.getElementById("json").innerHTML=myItems(data.substring(47).slice(0, -2))  
      );
    function myItems(jsonString){
      var json = JSON.parse(jsonString);
      var table = '<table><tr>'
      json.table.cols.forEach(colonne => table += '<th>' + colonne.label + '</th>')
      table += '</tr>'
      json.table.rows.forEach(ligne => {
        table += '<tr>'
        ligne.c.forEach(cellule => {
            try{var valeur = cellule.f ? cellule.f : cellule.v}
            catch(e){var valeur = ''}
            table += '<td>' + valeur + '</td>'
          }
        )
        table += '</tr>'
        }
      )
      table += '</table>'
      return table
    }           
    </script>
    </body></html>
    

    【讨论】:

    • 它正在工作,但它正在下载文本文件如何以 JSON 格式显示在浏览器中?
    • 我已经完成了我的答案...只得到 json .substring(47).slice(0, -2)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多