使用 google 应用脚本在图表上设置角色

Setting roles on charts with google app script

我正在 spread sheet 应用程序脚本中编写代码。

我正在尝试创建一个 ColumnTable 并为不同的列添加注释或颜色。

Google's Visualization 对此非常清楚,但是关于将其与应用程序脚本一起使用的文档很少。

我要执行的代码类似于:

var data = google.visualization.arrayToDataTable([
     ['Element', 'Density', { role: 'style' }, { role: 'annotation'}],
     ['Copper', 8.94, '#b87333', 'Cu' ],
     ['Silver', 10.49, 'silver', 'Ag' ],
     ['Gold', 19.30, 'gold', 'Au' ],
     ['Platinum', 21.45, 'color: #e5e4e2', 'Pt' ]
  ]);

但是我无法执行 ['Element', 'Density', { role: 'style' }, { role: 'annotation' } 行,因为没有像 'role'.

这样的 Charts.ColumnType

有人可以帮助我吗?

我想你在找 setColumns(columns) as it also makes a reference to Visualization Chart's DataTable Roles

setColumns(columns)

Sets the indexes of the columns to include in the data view as well as specifying role-column information. This subset of column indexes refer to the columns of the data source that the data view will be derived from.

A column role describes the purpose of the data in that column: for example, a column might hold data describing tooltip text, data point annotations, or uncertainty indicators. For more details, see DataTable Roles in the Google Charts documentation.

function myFunction() {
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.getSlides()[0];

  var COLUMN_SPEC = [
  0, // H
  1, //

  {sourceColumn: 1, role: 'annotation'},
    2, //
    {sourceColumn: 2, role: 'annotation'}
];

  var viewSpec = Charts.newDataViewDefinition()
      .setColumns(COLUMN_SPEC)
      .build();

  var data = Charts.newDataTable()
      .addColumn(Charts.ColumnType.STRING, 'H')
      .addColumn(Charts.ColumnType.NUMBER, 'Test1')
      .addColumn(Charts.ColumnType.NUMBER, 'Test2')
      .addRow(['H1 18', 13, 33])
      .addRow(['H2 18', 10, 25])
      .addRow(['H1 19', 17, 23])
      .addRow(['H2 19', 17, 29])
      .build();

  var chartBuilder = Charts.newLineChart()
      .setXAxisTitle('H')
      .setYAxisTitle('Value')
      .setDimensions(600, 500)
  .setDataViewDefinition(viewSpec)
      //.setOption('useFirstColumnAsDomain', true)
      .setCurveStyle(Charts.CurveStyle.NORMAL)
      .setPointStyle(Charts.PointStyle.NONE)
      .setLegendPosition(Charts.Position.BOTTOM)
      .setDataTable(data);

  var chart = chartBuilder.build();  

  var areaBlob = chart.getBlob().getAs('image/png').setName("areaBlob");
  slide.insertImage(areaBlob,  3, 3, 306, 163);  
}