这听起来可能是个愚蠢的问题,但我在网上搜索了很多,但没有找到我想要的...
我已经成功地根据不同电子表格上的数据构建了我想要显示的图表,我想将图表插入到电子表格中,而不必将数据粘贴到工作表上。这可能吗 ?
我也可以使用 UIApp 来显示它,但我想将它显示在电子表格上,而不必从菜单或按钮调用它。
谢谢你的帮助,
毛毛虫
-- 到目前为止,这是我的代码,正如我在调试器工具上看到的那样,它正在运行
function BuildMTChart(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var DataChart = MTLastMonth() // MTLastMonth() returns an array with the data I need
var dSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard");
var dataTable = Charts.newDataTable();
dataTable.addColumn(Charts.ColumnType['DATE'], 'Date');
dataTable.addColumn(Charts.ColumnType['NUMBER'], 'Maintenance');
dataTable.addColumn(Charts.ColumnType['NUMBER'], 'Troubleshooting');
for ( var i = 0; i < DataChart.length; i++) {
dataTable.addRow([DataChart[i][0], DataChart[i][1], DataChart[i][2]]);
}
dataTable.build();
var chart = Charts.newAreaChart()
.setDataTable(dataTable)
.setDimensions(800, 400)
.setYAxisTitle("Hours")
.setXAxisTitle("Date")
.build();
最佳答案
EmbeddedChartBuilder 曾经有setDataTable 方法。但我无法让它发挥作用。
据我所知,您可以从脚本向电子表格添加图表,但这些图表需要在电子表格本身中包含它们的数据:
var chart = workingSheet.newChart()
.setChartType(Charts.ChartType.COLUMN)
.setPosition(8, 2, 0, 0)
.setTitle("Sample chart")
.setXAxisTitle("...")
.setYAxisTitle("...")
.addRange(range) // This could be smth. like SpreadsheetApp.getActiveSpreadsheet().getRange("B2:C8")
.build();
SpreadsheetApp.getActiveSpreadsheet().insertChart(chart);
Here it says您可以设置一些高级图表选项。也许还有一种方法可以在那里传递数据表?
目前,我建议使用您的数据填充电子表格,然后添加基于范围的图表。如果您不希望此数据在 google 文档中可见,您可以隐藏列或将其移至新工作表。
关于google-apps-script - 将使用 google apps 脚本构建的图表插入到 google 电子表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12046489/