Create Google Chart on web page with data from private Google Sheet

458 views
Skip to first unread message

Tim Hill

unread,
Dec 12, 2016, 1:56:51 PM12/12/16
to Google Visualization API
I'm able to create a Google Chart using data in a Google Sheet identical to this one: https://docs.google.com/spreadsheets/d/1QO2m5oLoLDclkNWCqVO7HdjJqsXp4fgaIzPFdngmIhg/edit?usp=sharing using this code:

<html>
 
<head>
 
<title>Graph from sheet</title>
   
<meta charset="utf-8">
   
<meta http-equiv="X-UA-Compatible" content="IE=edge">
   
<meta name="viewport" content="width=device-width, initial-scale=1">

   
<script src="demo.js" type="text/javascript"></script>
   
<script src="https://apis.google.com/js/auth.js?onload=init"></script>

   
<!--Load the AJAX API-->
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">

     
// Load the Visualization API and the corechart package.
      google
.charts.load('current', {'packages':['line', 'corechart']});

     
// Set a callback to run when the Google Visualization API is loaded.
      google
.charts.setOnLoadCallback(initialize);

     
// Callback that creates and populates a data table,
     
// instantiates the pie chart, passes in the data and
     
// draws it.
function initialize() {
       
var opts = {sendMethod: 'auto'};
       
// Replace the data source URL on next line with your data source URL.
       
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1mF-0U2Nh3YsZOvHBaDaBZTOS-J3IQNAbshHdBQYhJts/gviz/tq?sheet=SEO&headers=1&tq=', opts);        

       
// Send the query with a callback function.
        query
.send(handleQueryResponse);
}

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

   
var data = response.getDataTable();

 
var options = {
        title
: 'Organic Search Performance',
        width
: 900,
        height
: 500,
       
// Gives each series an axis that matches the vAxes number below.
        series
: {
         
0: {targetAxisIndex: 0},
         
1: {targetAxisIndex: 1},
         
2: {targetAxisIndex: 0},
         
3: {targetAxisIndex: 1}        
       
},
        vAxes
: {
         
// Adds titles to each axis.
         
0: {title: 'Impressions'},
         
1: {title: 'Click Through Rate'}
       
},
        explorer
: {
        axis
: 'horizontal',
        maxZoomOut
:2,
        keepInBounds
: true
   
}

 
};

 
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart
.draw(data, options);
}

   
</script>  
 
</head>

 
<body>
   
<button id="authorize-button" style="visibility: hidden">Authorize</button>  
   
<!--Div that will hold the pie chart-->
   
<div id="chart_div"></div>
 
</body>
</html>

But I don't want to share the link to the sheets publicly. However when I set the sheets to private the chart no longer works.


I've followed the instructions here: https://developers.google.com/chart/interactive/docs/spreadsheets#authorization adding in the demo.js file with my clientID.


The result is if I'm signed out of Google I need to login and get asked if I'm OK for the page to access my Google Sheets but I don't want to get the page to ask for permission to access the Sheets of the visiting user, I want it to access the sheets in my account so I think this is a different type of Auth???


Basically when my page loads I want it to try and fetch a specific sheet. Google Sheets will say "woah, that's private" and my script will say "Its OK I have credentials"

I've looked high and low for a way to do this but can't even seem to find any discussions about it. Is this actually even possible?



Pierre-Hedzer Marchi

unread,
Jun 4, 2017, 10:52:51 PM6/4/17
to Google Visualization API
Hi Tim

I am going over the same issue but I can't even manage to have the demo.js example working with a private google sheet (can't manage to have the authentication passed to the chart declaration). I can't find anywhere a full example of that process from the authentication to the display of a chart based on private google sheet data.

In my case it would be perfectly fine if the user needs to be signed in to Google to be be able to access the charts without being prompted for authorization.

Could you please help me by showing your example above with the demo.js included (except the clientID :-) ) ? 

Thanks
Pierre
Reply all
Reply to author
Forward
0 new messages