Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos