Main question: why is the script below not returning anything? When given a version and deployed, the script returns 'The script completed but did not return anything.'
(Bonus question: can I run two charts on two different datatables within the same dashboard and within the same script?)
The input is a Google Spreadsheet with four columns and a header row: Year|Month|Group|Count, all numerics except Group (single letter).
The objective is to create a line chart of total counts by yearmo (201001, 201002...). Ideally I'd like to generate another line chart of total counts by year-group within the same script, but one step at a time...
Since the data isn't rolled up (and I'm new to google apps script and can't find any aggregation outside pivoting the spreadsheet itself), I resort to hashes to sum counts over year-month. I'll do the same for year-groups.
It may not be elegant (I happen to be new to js too), but it makes sense to me. Too bad it doesn't work. Thanks for pointers.
EDIT 2013-04-25
Tried to get too much done in a single script and lost my footing. So I've taken the aggregation outside of the script, created a new spreadsheet, and ran the code found here https://sites.google.com/site/appsscripttutorial/chart-services/line-chart: got my line chart.
function doGet(){
// counts by yearmo
var ss = SpreadsheetApp.openById('0Atd6tVDA1d3UOI-jfkdlRHbk85Y19BcU9BNFdPNXBlVlE');
var data = ss.getDataRange().getValues();
var yr = [];
var mo = [];
for (var i = 1; i < data.length; i++) {
yr[data[i][0]] = 0;
mo[data[i][1]] = 0;
}
Logger.log( yrLvls = Object.keys(yr) );
Logger.log( moLvls = Object.keys(mo) );
// initialize hash
var yearmo = [];
for (var i = 0; i < yrLvls.length; i++){
for (var j = 0; j < moLvls.length; j++){
var key = yrLvls[i] + '-' + moLvls[j]
yearmo[ key ] = 0;
}
}
// aggregation
for (var i = 1; i < data.length; i++) {
yearmo[ data[i][0] + '-' + data[i][1] ] += data[i][3];
}
// check
var values = Object.keys(yearmo).map(function(key){
return yearmo[key];
});
Logger.log( values );
// Year Mo
Logger.log( "--------------" );
var data_yearmo = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, 'YearMo')
.addColumn(Charts.ColumnType.NUMBER, 'Count');
Logger.log( Object.keys(yearmo) );
for ( key in Object.keys(yearmo) ) {
Logger.log( key );
data_yearmo.addRow( [ key , yearmo[key] ] );
}
data_yearmo.build()
var lineChart_yearmo = Charts.newLineChart()
.setTitle('Counts')
.setXAxisTitle('YearMo')
.setYAxisTitle('Counts')
.setCurveStyle(Charts.CurveStyle.SMOOTH)
.setPointStyle(Charts.PointStyle.MEDIUM)
.setDataTable(data_yearmo);
var uiApp = UiApp.createApplication().setTitle('Yearmo');
uiApp.add(lineChart_yearmo);
return uiApp;
}