Home > OS >  Can't access JSON data from google sheets API link
Can't access JSON data from google sheets API link

Time:09-27

v3 of google sheets API has been deprecated. The link does not work anymore https://spreadsheets.google.com/feeds/list/<spreadsheet_id>/od6/public/values?alt=json. It shows this error "The Sheets v3 API has been turned down. Further information can be found at: https://clo"ud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api" Can anyone help me with the updated link format of v4 to access the JSON data?

CodePudding user response:

The new url is

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

here is an example of parsing

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
}

see the announcement here https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api

a complete application in 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>
  • Related