简体   繁体   English

使用Google Apps脚本和UiApp()填充饼图

[英]Populating a Pie Chart With Google Apps Script and UiApp()

Trying to build a Pie Chart with Google Apps script from a spreadsheet and so far nothing's working. 尝试使用电子表格中的Google Apps脚本构建饼图,到目前为止还没有任何效果。

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 它已部署为网络应用程序,可在此处查看: 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 可以在此处使用工作表图表查看链接: https//script.google.com/macros/s/AKfycbwn66EMbZsrCNyuRCYMk6ERyXhKGNt3_m1i5VIj_ITzWxAnb1vw/exec

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

You might want to take note that: 您可能需要注意:

The UI service was deprecated on December 11, 2014 . UI服务已于2014年12月11日弃用 To create user interfaces, use the HTML service instead. 要创建用户界面,请改用HTML服务

Fortunately, there is a tutorial " Converting from UiApp + Chart Service to Html Service + Google Visualization API ". 幸运的是,有一个教程“ 从UiApp +图表服务转换为Html服务+ Google Visualization API ”。

For users of Google Apps Script, there are two alternatives for visualizing data: 对于Google Apps脚本的用户,有两种可视化数据的方法:

  1. Continue using Charts + UiApp . 继续使用Charts + UiApp While deprecated and not supported for auto-completion in the editor, UiApp still works. 虽然在编辑器中不推荐使用并且不支持自动完成,但UiApp仍然有效。 There has been no announcement that it will actually be shut down… yet. 没有声明它实际上会被关闭......
  2. Follow Google's recommendation, and use HtmlService instead . 按照Google的建议, 改为使用HtmlService A feature request to have Charts supported by the HtmlService has been declined. HtmlService支持图表的功能请求已被拒绝。 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. 因此,这个看似简单的建议需要额外要求您使用Google的其他图表产品 ,也称为Google Visualization API或GViz。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM