【发布时间】:2014-01-14 18:30:09
【问题描述】:
我正在尝试从谷歌电子表格创建条形图。我不想手动添加每一列和每一行,所以这个例子似乎应该工作并使用 for 循环自动化该过程。
下面的例子来自https://sites.google.com/site/appsscripttutorial/chart-services/bar-chart
错误显示“行的单元格过多。最多应为列数。”
function doGet(){
//Get the data from spreadsheet
ssID ='SpreadsheetID';//Change it to yours
var SS = SpreadsheetApp.openById('ssID');
var sheet = SS.getSheets()[0];
var data = sheet.getRange('A1:G7').getValues();
//Build data table
var dataTable = Charts.newDataTable();
//Add Column types
dataTable.addColumn(Charts.ColumnType.STRING, data[0][0]);
for(var i=1; i<data[0].length-1; i++){
dataTable.addColumn(Charts.ColumnType.NUMBER, data[0][i]);
}
//Add rows
for(var j=1; j<data.length; j++){
dataTable.addRow(data[j]);
Logger.log(data[j])
}
//Create and build chart
var chart = Charts.newBarChart()
.setDataTable(dataTable)
.setTitle("Sales by store")
.build();
var app = UiApp.createApplication().setTitle("AST Chart");
app.add(chart)
return app;
}
【问题讨论】:
标签: javascript google-apps-script google-sheets google-visualization