Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Last active September 19, 2018 07:58
Show Gist options
  • Save bennettscience/c314a9251cb4f4661adac95c517d31c5 to your computer and use it in GitHub Desktop.
Save bennettscience/c314a9251cb4f4661adac95c517d31c5 to your computer and use it in GitHub Desktop.
Code to display Google Sheets data as a web-based dashboard
// Pie Chart visualization client-side scripting
// Initialize the script and grab the data from the spreadsheet.
google.script.run.withSuccessHandler(drawChart).getSpreadsheetData();
google.load("visualization", "1", {packages:["corechart"]});
// Using the returned data from the spreadsheet, draw the chart.
// ssData is a 2D array of the candidate name and their electoral vote totals from the spreadsheet.
function drawChart(ssData) {
// Build the data table for the visuzlization API to read from.
var data = new google.visualization.DataTable();
data.addColumn('string','Candidate');
data.addColumn('number','Electoral Votes');
data.addRows(ssData);
// Set the options for the visualization once it's drawn to the page.
var options = {
'title': 'Electoral Vote Totals',
'pieSliceText': 'value',
'height':'550',
'pieHole':'0.5',
'slices': {
0: {color:'red'},
1: {color:'blue'},
2: {color:'orange'}
},
'titleTextStyle': {
fontSize: 24
}
};
// Grab the appropriate `div1 and draw.
var chart = new google.visualization.PieChart(document.getElementById("chart"));
chart.draw(data, options);
}
// This is the server-side script that gathers the spreadsheet
// when requested by the client. This is only the portion of the
// code that relates to the chart. Add this snippet to the server-
// side code used to generate the webapp.
// Gather the vote data from the Google Sheet
function getSpreadsheetData() {
var sheet = ss.getSheets()[2];
var ssData = sheet.getRange("F1:G3").getValues();
return ssData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment