在谷歌应用脚​​本中使用setoption()格式化趋势线

时间:2013-09-23 07:46:30

标签: google-apps-script google-visualization google-spreadsheet-api

我正在使用Google Apps脚本创建一个包含趋势线的散点图。 I have taken an idea from Google Charts API on how to create the trendline,这已经成功了。但是,格式化趋势线的方法不适用于Charts API描述。我已经使用setOption(“trendlines”,“0:{}”)来创建我的趋势线,但是我在大括号之间放置并不重要,我添加的格式似乎不起作用。我希望趋势线的点数小于散点图的实际绘制点,我希望趋势线系列为黑色。如果我能够成功地格式化趋势线,那么这将解决许多高中科学教师的问题,他们试图在课堂上使用谷歌硬盘作为Excel的真正替代品。

这是我的代码:

function scatterMe(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var chart = sheet.newChart().asScatterChart()
  .addRange(sheet.getRange("A1:B500"))
  .setPosition(5, 5, 0, 0)

  //this is the code that builds the trendline... the example CSS code at 
  //https://developers.google.com/chart/interactive/docs/gallery/trendlines
  //does not seem to have any effect when I add it in between the curly braces.
  .setOption('trendlines', '0: {}')

  .build();

  sheet.insertChart(chart);
}

1 个答案:

答案 0 :(得分:3)

使用以下代码(示例数据),我可以进行一些更改,例如颜色,线宽等。

function scatterMe() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[2];

  var trendlinesopt = {
    0: {
      color: 'black',
      lineWidth: 6,
      opacity: 0.2,
      labelInLegend: 'Test data',
      visibleInLegend: true
    }
  };
  var chart = sheet.newChart().asScatterChart()
  .addRange(sheet.getRange("A1:B12"))
  .setPosition(3, 4, 0, 0)

  //this is the code that builds the trendline... the example CSS code at 
  //https://developers.google.com/chart/interactive/docs/gallery/trendlines
  //does not seem to have any effect when I add it in between the curly braces.
  .setOption('trendlines', trendlinesopt)

  .build();

  sheet.insertChart(chart);
}

enter image description here