Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

77
Views
How can we read file from google sheets url and convert its data into json in vanillaJS/jsquery?

I am working on a project where I need to read a file from google sheets url and then convert its content in json. Then I will use that json object for showing content in the frontend. But I just want to know how can we read an excel file from google sheets url and then are there any built in functions or external library which converts the data in json?

Thanks in advance.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

To read a spreadsheet via app script and get the json, try (you need to know the id of the spreadsheet and the gid of the sheet)

const readSheet2json = () => {
  var id = '#######';
  var gid = '######';
  var url = `https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:json&tq&gid=${gid}`;
  var txt = UrlFetchApp.fetch(url).getContentText();
  var jsonString = txt.slice(47, -2)
  console.log(jsonString)
}

to read via html, try this script (define an element which id is "json")

var id = '#######';
var gid = '######';
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.slice(47, -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
}
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.