How to Get a particular value of a query?

150 views
Skip to first unread message

samuel.ma...@bbva.com

unread,
Sep 25, 2015, 2:08:40 PM9/25/15
to Google Visualization API
So I do a query to a Google Sheet as Below but I get the whole column.

How can I get a particular value of that query?

Let's said that I'm queering this table:

Name    Last Name    Age
John      Smith           26

After I execute the query SELECT B, C WHERE A = John

I would like to introduce in a variable the values  Smith and 26.

something like:

var LastName = query [1,1]
var Age =  = query [1,2]

This is what I tried so fat and didn't work:

var Area = query[0].value;
var Area = query[0];
var Area = query[1,1].value;
var Area = query[1,1];

Code below.

     var dataSourceUrl = 'https://docs.google.com/spreadsheets/d/1xfb9trifQA5KDPc9Nh5hBL4MJ290Mxcc1Uod2VTPzYI';
     
var magicIncantation = '/gviz/tq?sheet=Sheet1&headers=1&tq=';


     
var queryStringInfo = encodeURIComponent("SELECT C WHERE A = 'Alabama'");
      query
= new google.visualization.Query(dataSourceUrl + magicIncantation + queryStringInfo);
     
var Area = query[0].value;
     
var Area2 = query[1].value;
     
var Area3 = query[1,1].value;

THANK YOU SO MUCH!!!!

Sergey Grabkovsky

unread,
Sep 25, 2015, 2:22:06 PM9/25/15
to Google Visualization API
Hi Samuel,

You can't use a Query the way you're using it.

Firstly, the query must be sent first, since it's a request to Sheets. It doesn't immediately send when you construct it. You have to do so explicitly via query.send().
Secondly, queries are asynchronous, which means that you have to provide a callback for the result, since it won't be available immediately after a query was sent, but some indeterminate amount of time later.
Thirdly, query[1, 1] is not valid JavaScript.
And finally, a query will return a JSON object that may be an error or a DataTable, so you'll have to check the response.

We have fairly extensive documentation for Query, which you can find here. I think you'll have better luck following the documentation examples than trying random things.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/572d7d5f-8bba-43f9-b2f9-9d5d5ec70e60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

samuel.ma...@bbva.com

unread,
Sep 25, 2015, 2:56:20 PM9/25/15
to Google Visualization API
Hi Sergey,

Thanks for your reply. I tried with the code below but still nothing.

What I'm trying to do is to being able to store in a variable the value from a particular cell in a Google Sheet. Let's say that I want to read cell A2 from the table:

Name    Last Name    Age
John      Smith           26

Where I should get value for A2 = Smith so I store that value in a variable and use it.

How can I do that?

Thank you very much

After getting the Data through 

    var data = response.getDataTable();

I tried to do     var info = data[0,1];

I know that's not the way it works but that's the concept I'm trying to do and I'm not able to find documentation on how to do that.

THANK YOU VERY MUCH AGAIN

function drawSheetName() {
    var queryString = encodeURIComponent('SELECT A, B, C, D');
    var magicIncantation = '/gviz/tq?gid=0&headers=1&tq=';

    var query = new google.visualization.Query(
        magicIncantation + queryString);
    query.send(handleSampleDataQueryResponse);

}


function handleSampleDataQueryResponse(response) {
    if (response.isError()) {
        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
    }

    var data = response.getDataTable();
    var info = data[0,1];

       var chart = new google.visualization.ColumnChart(document.getElementById('Columnchart_div'));
    chart.draw(data, optionsColumnChart);

}
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

Sergey Grabkovsky

unread,
Sep 25, 2015, 3:00:36 PM9/25/15
to Google Visualization API
response.getDataTable() returns a google.visualization.DataTable instance. You can find the documentation for that here, and the full API reference here.

Seems like the method you're looking for is data.getValue(row, column);

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.

samuel.ma...@bbva.com

unread,
Sep 25, 2015, 3:57:49 PM9/25/15
to Google Visualization API
Thank you again Sergey,

I tried that, please see here:  http://jsfiddle.net/samuvk/42ddL04w/

It is obviously getting the data because it is drawing the graph, however when I'm trying to use the data.getValue(row, column); function to get the value in a particular cell I get the value: "undefined"

Thank you very much

To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.

samuel.ma...@bbva.com

unread,
Sep 25, 2015, 4:40:27 PM9/25/15
to Google Visualization API
Sorry, I figure it out. I need () instead of []

Thanks


On Friday, September 25, 2015 at 2:00:36 PM UTC-5, Sergey wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages