1

I am trying to set two seperate arrays(headers & rows) to be equal to the response of the query I am making.

let dataProp = {}
// I make the query
var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1xEz5wfWSwXbAgWhgvJxfxSDhNGf5w8hqVpAFbbAViEo/gviz/tq?tqx=out:json&sheet=Sheet2')

// I send the query to a function to be handled
query2.send(handleQueryResponse2)
function handleQueryResponse2(response) {

    // I want to inspect the response (shown below in image)
    console.log(response.getDataTable())

    data2 = response.getDataTable()
    dataProp.table2 = { rows: [...data2.fg] }

    // let's put this loop aside (this sanitizes the content in same data type.)
    for (let i = 0; i < data2.fg.length; i++) {
      let tempRow = [];
      for (let j = 0; j < data2.fg[i].c.length; j++) {
        tempRow[j] = cleanRowsContent(dataProp.table2.rows[i].c[j])
      }
      dataProp.table2.rows[i] = tempRow
    }
  }

Response of the query in console:

enter image description here

Problem : Like shown in code, I want to grab the headers and rows like shown below BUT the values "If" & "fg" changes with updates (to "Fg" & "hg" and so on...) and I constantly have to check if my code is still working... Can someone explain me how I can fix this?

A little more context : I am using Google visualisation tools (to make multiple queries) because it is 97% lighter than the traditional jquery api format available

1 Answer 1

1

first, if you want to set properties on variable --> dataProp
need to use an object ({}), instead of an array ([])

let dataProp = {};

next, rather than trying to access the data directly from the data table,
try converting the data table to json,
then grab the pieces you want...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {

  let dataProp = {};
  var query2 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1xEz5wfWSwXbAgWhgvJxfxSDhNGf5w8hqVpAFbbAViEo/gviz/tq?tqx=out:json&sheet=Sheet2');

  query2.send(handleQueryResponse2);
  function handleQueryResponse2(response) {

    // convert data table to json
    data2 = JSON.parse(response.getDataTable().toJSON());

    // get rows
    dataProp.table2 = data2.rows;

    // get columns
    dataProp.table3 = data2.cols;

    console.log(JSON.stringify(dataProp));

  }

});
Sign up to request clarification or add additional context in comments.

1 Comment

(dataProp is an object yes I made a typo, sorry) But YES this is exactly what I needed to see thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.