使用的SetOption()在谷歌的Apps脚本格式化趋势线(formatting trendlin

2019-10-18 16:29发布

我使用谷歌Apps脚本来创建一个趋势线的散点图。 我从谷歌图表API采取如何创建趋势线的想法 ,这是成功的。 但是,该方法能格式化趋势线不作为API描述图表的工作。 我用的SetOption(“趋势线”,“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);
}

Answer 1:

用下面的代码(样本数据)我可以进行一些变化,如颜色,线条宽度,等等。

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);
}



文章来源: formatting trendline using setoption() in google apps script