1
votes

Trying to build a Pie Chart with Google Apps script from a spreadsheet and so far nothing's working.

Here's the code I have right now:

Code:

function doGet() {

var ss   =   SpreadsheetApp.openById('1lmmpJs2Bz3EfQWExB4KXq_uJWoLlq1PMCahy6w4ipcE');
var data = ss.getDataRange();

/* Build filters to analyze charts */
var neighborhoodFilter   = Charts.newCategoryFilter().setFilterColumnIndex(2).build();
var attendanceFilter     = Charts.newCategoryFilter().setFilterColumnIndex(3).build();

var pieChart   =  Charts.newPieChart()
                 .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([2,3]))
               .build();


var dashboard  =  Charts.newDashboardPanel().setDataTable(data)
               .bind([neighborhoodFilter, attendanceFilter], [pieChart])
               .build();

var app         = UiApp.createApplication();
var filterPanel = app.createVerticalPanel();
var chartPanel  = app.createHorizontalPanel();
filterPanel.add(neighborhoodFilter).add(attendanceFilter).setSpacing(10);
chartPanel.add(pieChart).setSpacing(10);


dashboard.add(app.createVerticalPanel().add(filterPanel).add(chartPanel));
app.add(dashboard);
return app;
}

It's been deployed as a web app, which can be seen here: https://script.google.com/macros/s/AKfycbyQMz3dSYqNLBEusCs4-_lB8u9wvy-P9Q_HXKn-oCH8b3gqgb4E/exec

I run it through debugger and everything works fine.

Also, for additional clarity, this is a simplified snippet from a larger dashboard I'm trying to create that populates two charts.

If it's useful here's the entire piece of code that I'm trying to use (the table chart populates as expected in this example):

Code:

function doGet() {

var ss   = SpreadsheetApp.openById('1lmmpJs2Bz3EfQWExB4KXq_uJWoLlq1PMCahy6w4ipcE');
var data = ss.getDataRange();

/* Build filters to analyze charts */
var nameFilter           = Charts.newStringFilter().setFilterColumnIndex(1).build();
var neighborhoodFilter   = Charts.newCategoryFilter().setFilterColumnIndex(2).build();
var attendanceFilter     = Charts.newCategoryFilter().setFilterColumnIndex(3).build();
var transportationFilter = Charts.newCategoryFilter().setFilterColumnIndex(4).build();
var travelFilter         = Charts.newCategoryFilter().setFilterColumnIndex(5).build();
var companyFilter        = Charts.newNumberRangeFilter().setFilterColumnIndex(6).build();

/* Build charts and dashboard object */
var tableChart =  Charts.newTableChart()
               .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,2,3,4,5,6]))
               .build();


var pieChart   =  Charts.newPieChart()
               .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([2,3]))
               .build();

var dashboard  =  Charts.newDashboardPanel().setDataTable(data)
               .bind([neighborhoodFilter, attendanceFilter, nameFilter, transportationFilter, travelFilter, companyFilter], [tableChart, pieChart])
               .build();

var app         = UiApp.createApplication();
var filterPanel = app.createVerticalPanel();
var chartPanel  = app.createHorizontalPanel();


 filterPanel.add(neighborhoodFilter).add(attendanceFilter).add(nameFilter).add(transportationFilter).add(travelFilter).add(companyFilter).setSpacing(10);
chartPanel.add(pieChart).add(tableChart).setSpacing(10);

dashboard.add(app.createVerticalPanel().add(filterPanel).add(chartPanel));
app.add(dashboard);
return app;
}

Link can be seen here with the working table chart: https://script.google.com/macros/s/AKfycbwn66EMbZsrCNyuRCYMk6ERyXhKGNt3_m1i5VIj_ITzWxAnb1vw/exec

Any help would be appreciated. Thank you.

1

1 Answers

0
votes

You might want to take note that:

The UI service was deprecated on December 11, 2014. To create user interfaces, use the HTML service instead.

Fortunately, there is a tutorial "Converting from UiApp + Chart Service to Html Service + Google Visualization API".

For users of Google Apps Script, there are two alternatives for visualizing data:

  1. Continue using Charts + UiApp. While deprecated and not supported for auto-completion in the editor, UiApp still works. There has been no announcement that it will actually be shut down… yet.
  2. Follow Google’s recommendation, and use HtmlService instead. A feature request to have Charts supported by the HtmlService has been declined. So this seemingly-simple recommendation entails the additional requirement that you use Google’s other Charts offering, also known as the Google Visualization API, or GViz.

Hope this helps