URL creation for extracting Google Sheets data

67 views
Skip to first unread message

Laurie Bennett

unread,
Apr 10, 2024, 2:53:46 PMApr 10
to Google Visualization API
I'm using Google Charts to create pie charts on my web site. These charts display if I hard-code the data, but I'm trying to use a query of a public Google Sheet. It's not working, and the problem appears to be in this section.

        // Callback to draw the chart
function drawChart() {
  var query = new google.visualization.Query("[Google Sheet URL]/gviz/tq?tq=[ENCODED QUERY]");
  query.send(handleQueryResponse);
}

Bracketed info is replaced by the real values. The query has been encoded using the tool on this page: https://developers.google.com/chart/interactive/docs/querylanguage.

I use double quotes around the URL because the query contains a where clause with single quotes.

Message has been deleted
Message has been deleted

Ray Thomas

unread,
Apr 10, 2024, 4:50:21 PMApr 10
to Google Visualization API
I've had better luck using the query code from https://developers.google.com/chart/interactive/docs/spreadsheets and the  encodeURIComponent function.

For example:

function drawChart() {    
var queryString = encodeURIComponent('SELECT C,A,E where E="Fun" ORDER BY C,A');
var query = new google.visualization.Query('google_sheet_url/gviz/tq?gid=1011451446&headers=1&tq=' + queryString);
query.send(handleQueryResponse);
}

which comes from a live page.
Reply all
Reply to author
Forward
0 new messages