I'm trying to use the HTML visualization to show charts, similar to how the UiApp does (which is now deprecated). I've found this example:
.gs
function doGet() {
return HtmlService.createTemplateFromFile('html')
.evaluate()
.setTitle('My WebApp')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
};
function doSomething() {
return SpreadsheetApp.openById("ABC123").getActiveSheet.getRange(1, 1).getValue;
};
.html
<html>
<script>
function onSuccess(A1) {
var value = A1
}
google.script.run.withSuccessHandler(onSuccess)
.doSomething();
</script>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart', 'controls']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var dashboard = new google.visualization.Dashboard(
document.getElementById('programmatic_dashboard_div'));
// We omit "var" so that programmaticSlider is visible to changeRange.
programmaticSlider = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'programmatic_control_div',
'options': {
'filterColumnLabel': 'Donuts eaten',
'ui': {'labelStacking': 'vertical'}
}
});
programmaticChart = new google.visualization.ChartWrapper({
'chartType': 'PieChart',
'containerId': 'programmatic_chart_div',
'options': {
'width': 300,
'height': 300,
'legend': 'none',
'chartArea': {'left': 15, 'top': 15, 'right': 0, 'bottom': 0},
'pieSliceText': 'value'
}
});
var data = google.visualization.arrayToDataTable([
['Name', 'Donuts eaten', 'Gender'],
['Michael' , value, 'Male'],
['Elisa', 7, 'Female'],
['Robert', 3, 'Male'],
['John', 2, 'Male'],
['Jessica', 6, 'Female'],
['Aaron', 1, 'Male'],
['Margareth', 8, 'Female']
]);
dashboard.bind(programmaticSlider, programmaticChart);
dashboard.draw(data);
}
</script>
</head>
<body>
<div id="programmatic_dashboard_div" style="border: 1px solid #ccc">
<table class="columns">
<tr>
<td>
<div id="programmatic_control_div" style="padding-left: 2em; min-width: 250px"></div>
<div>
<button style="margin: 1em 1em 1em 2em" onclick="changeRange();">
Select range [2, 5]
</button><br />
<button style="margin: 1em 1em 1em 2em" onclick="changeOptions();">
Make the pie chart 3D
</button>
</div>
<script type="text/javascript">
function changeRange() {
programmaticSlider.setState({'lowValue': 2, 'highValue': 5});
programmaticSlider.draw();
}
function changeOptions() {
programmaticChart.setOption('is3D', true);
programmaticChart.draw();
}
</script>
</td>
<td>
<div id="programmatic_chart_div"></div>
</td>
</tr>
</table>
</div>
This works perfectly fine, but rather than entering the data myself, I want to extract the data from a spreadsheet. For example:
SpreadsheetApp.openById("ABC123").getActiveSheet.getRange(1, 1).getValue;
I've tried placing that bit of code at the start of the script, in the middle, pretty much everywhere, but it just doesn't seem to work. Any ideas how to achieve this?
Thanks in advance