Last active
July 5, 2022 15:54
-
-
Save jarridlima/1f138aa4a40ce49179f9c7625acacb8d to your computer and use it in GitHub Desktop.
In this example, you can format data (rows and cols) became from public Google Sheets API in JSON format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------- | |
// IMPORTANT!!! | |
// ---------------------------------------- | |
// FIRST OF ALL, REMEMBER TO TURN ON PUBLIC READ PERMISSION IN YOUR SPREADSHEET IN GOOGLE. | |
// ---------------------------------------- | |
// BASE URL TO GET SHEET DATA | |
sheetsBaseURL = "https://docs.google.com/spreadsheets/d" | |
// LOAD DATA FROM GOOGLE SHEETS SERVICE | |
getSheet(id, params='') { | |
const xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( "GET", `${this.sheetsBaseURL}/${id}/gviz/tq?tqx=out:json${params}`, false ); // false for synchronous request | |
xmlHttp.send( null ); | |
return xmlHttp.responseText; | |
} | |
// GET AND FORMAT DATA BY `ROWS` | |
getByRows(table:any){ | |
if(table.rows){ | |
const rows = [] | |
table.rows.map(r=>{ | |
if(r.c){ | |
const cols = []; | |
r.c.map((col)=>{ | |
if(col != null && col.v){ | |
cols.push(col.v) | |
} else { | |
cols.push(col) | |
} | |
}) | |
rows.push(cols) | |
} | |
}) | |
return rows | |
} | |
} | |
// GET AND FORMAT DATA BY `COLUMNS` | |
getByCols(table:any){ | |
if(table.cols){ | |
const cols = [] | |
table.cols.map((col,indexCol)=>{ | |
col.lines = [] | |
if(table.rows){ | |
table.rows.map(r=>{ | |
if(r.c){ | |
if(r.c[indexCol] != null && r.c[indexCol].v){ | |
col.lines.push(r.c[indexCol].v) | |
} else { | |
col.lines.push(r.c[indexCol]) | |
} | |
} | |
}) | |
cols.push(col) | |
} | |
}) | |
return cols | |
} | |
} | |
// GET AND FORMAT DATA BY `MATRIX` | |
getByMatrix(table:any, keyAsID=true){ | |
if(table.rows){ | |
const rows = [] | |
table.rows.map(r=>{ | |
if(r.c){ | |
const cols:any = keyAsID ? {} : [] | |
r.c.map((colR,indexCol)=>{ | |
if(colR === null) colR = {} | |
colR.id = table.cols[indexCol].id | |
colR.label = table.cols[indexCol].label | |
colR.value = colR.v ? colR.v : null | |
delete colR.v | |
if(keyAsID){ | |
cols[colR.id] = colR | |
} else { | |
cols.push(colR) | |
} | |
}) | |
rows.push(cols) | |
} | |
}) | |
return rows | |
} | |
} | |
// GETTING DATA FROM GOOGLE API... | |
let sheet:any = this.getSheet('1XRsJinlp_q86el6YTjl_Z7IISIeHdE_m4Gu0hPqiOp-s','&gid=0') | |
// REMOVING UNECESSARY STRING FROM RESPONSE | |
sheet = sheet.match(/google\.visualization\.Query\.setResponse\(([\s\S\w]+)\)/); | |
if (sheet && sheet.length == 2) { | |
const obj = JSON.parse(sheet[1]); | |
// FORMATING DATA BY MATRIX (using function getByMatrix() | |
console.log(this.getByMatrix(obj.table)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment