All the Line chart examples that i see online rely on building the data table manually in the app script and then constructing the line graph. Is there a means to Link the google sheet data to create line graph.
I have my sample code, but not sure where i am going wrong in developing a webscript to draw line chart using google sheet data. Below is my code.
// Code.gs
function doGet(e) {
return HtmlService
.createTemplateFromFile("index")
.evaluate()
.setTitle("Google Spreadsheet Chart")
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getSpreadsheetData() {
var ssID = "PUT_YOUR_SPREADSHEET_ID",
sheet = SpreadsheetApp.openById(ssID).getSheets()[0],
data = sheet.getDataRange().getValues();
return data;
}Below is the html file inside the script editor saved as index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://www.google.com/jsapi"></script>
</head>
<body>
<div id="main"></div>
<script>
google.load('visualization', '1', {
packages: ['corechart', 'line']
});
google.setOnLoadCallback(getSpreadsheetData);
function getSpreadsheetData() {
google.script.run.withSuccessHandler(drawChart).getSpreadsheetData();
}
function drawChart(rows) {
var options = {
title: 'Line Chart',
legend: { position: 'bottom' },
chartArea: {
width: '60%'
},
vAxis: {
textStyle: {
fontFamily: 'Arial',
fontSize: 12
}
}
};
var data = google.visualization.arrayToDataTable(rows, false),
chart = new google.visualization.LineChart(document.getElementById("curve_chart"));
chart.draw(data, options);
}
</script>
</body>
</html>