Google图表对象错误 [英] Google charts object error

查看:80
本文介绍了Google图表对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单电子表格的图形化表示,但是我收到一个错误,提示'没有对象类型与列的类型匹配'。

函数doGet(){

var ss = SpreadsheetApp.openById(xyz);

var data = ss.getDataRange();

var segFilter = Charts.newStringFilter()。setFilterColumnIndex(6).build();
var execFilter = Charts.newCategoryFilter()。setFilterColumnIndex(7).build();

var tableChart = Charts.newTableChart()
.setDataViewDefinition(Charts.newDataViewDefinition()。setColumns([1,2,3,4,5,6,7,8,9, 10,11,12,13]))
.build();

var dashboard = Charts.newDashboardPanel()。setDataTable(data)
.bind([segFilter,execFilter],[tableChart])
.build();

var app = UiApp.createApplication();
var filterPanel = app.createVerticalPanel();
var chartPanel = app.createHorizo​​ntalPanel();
filterPanel.add(segFilter).add(execFilter).setSpacing(10);
chartPanel.add(tableChart).setSpacing(10);

dashboard.add(app.createVerticalPanel().add(filterPanel).add(chartPanel));
app.add(dashboard);
返回应用程序;
}

如何解决这个问题?




每个列都分配了一个数据类型...
每列中的所有数据必须具有相同的数据类型。

这里的问题是前导空格在你的数据表中。当您创建 DataTable 时,列的类型从第一行数据中推断出来,然后假定以下所有行都是相同的类型。在电子表格中,G2是空白的,导致列类型为 string 。但是,G列中的其余值的类型是 number





在这个例子中,删除空白行2后仍然存在问题。请参阅A3是字符串类型,但A4是类型编号 - 任何试图使用此范围的可视化文件都会抱怨类型不匹配。



因此,请确保您没有使用空白值开始任何列,并确保您的类型在列中保持一致,并且您应该没问题。 / b>




评论的其他提示:


  • 您可以使用电子表格中的格式控件将单元格的数据类型更改为纯文本,或者使用 setNumberFormat(@ STRING @)以编程方式更改单元格的数据类型。在您不打算将其视为数字的列中,这样会很好 - 例如,列L包含文本和数字的混合。但是,如果您正在考虑要绘制的列(例如在折线图中),那么转换为纯文本将是一个坏主意 - 它会阻止您执行可视化。


I am trying to create a graphical representation of a simple spreadsheet, but I am receiving an error that says 'no object type matches the type of column'.

function doGet() {

  var ss = SpreadsheetApp.openById("xyz");

  var data = ss.getDataRange();

  var segFilter = Charts.newStringFilter().setFilterColumnIndex(6).build();
  var execFilter = Charts.newCategoryFilter().setFilterColumnIndex(7).build();

  var tableChart = Charts.newTableChart()
    .setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,2,3,4,5,6,7,8,9,10,11,12,13]))
    .build();

  var dashboard = Charts.newDashboardPanel().setDataTable(data)
    .bind([segFilter, execFilter], [tableChart])
    .build();

  var app = UiApp.createApplication();
  var filterPanel = app.createVerticalPanel();
  var chartPanel = app.createHorizontalPanel();
  filterPanel.add(segFilter).add(execFilter).setSpacing(10);
  chartPanel.add(tableChart).setSpacing(10);

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

How can I fix this?

解决方案

From the Google Visualization API Reference:

Each column is assigned a data type... All data in each column must have the same data type.

The problem here is the leading blanks in your data table. When you create a DataTable, the type for the column is surmised from the first row of data, and it's then assumed that all the following rows are the same type. In your spreadsheet, G2 is blank, and that results in the column type being string. However, the remaining values in column G are of type number.

In this example, there would still be problems after removing the blank row 2. See that A3 is type string, yet A4 is type number - any visualization trying to use this range will complain about the type mismatch.

So make sure you don't start any column with blank values, and make sure your types are consistent within the column, and you should be fine.


Additional tips from comments:

  • You can change the data type for cells to be Plain Text, either using format controls in the spreadsheet, or programmatically using setNumberFormat("@STRING@"). This would be fine in columns that you do not intend to visualize as numbers - for example, your column L that contains a mixture of text and numbers. However, if you are thinking about a column that you want to chart (for example in a line chart), then conversion to Plain Text would be a bad idea - it would stop you from performing the visualization.

这篇关于Google图表对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆