在Google Apps脚本中使用setoption()格式化趋势线 [英] formatting trendline using setoption() in google apps script

查看:92
本文介绍了在Google Apps脚本中使用setoption()格式化趋势线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本创建带有趋势线的散点图. 我从Google Charts API获得了有关如何创建趋势线的想法,并且已经成功.但是,格式化趋势线的方法无法按照Charts API的描述进行操作.我已经使用setOption("trendlines","0:{}")来创建趋势线,但是放在大括号之间的内容并不重要,我添加的格式似乎无效.我希望趋势线的点比散点图的实际绘制点要小,并且我希望趋势线系列是黑色的.如果我可以成功设置趋势线的格式,那么这将解决许多高中理科教师的问题,他们正试图在课堂上使用Google云端硬盘作为Excel的真正替代品.

I am using Google Apps Script to create a scatterplot with a trendline. I have taken an idea from Google Charts API on how to create the trendline, and this has been successful. But, the approach to formatting the trendline does not work as the Charts API describes. I have used setOption("trendlines", "0: {}") to create my trendline, but it does not matter what I put in between the curly brackets, the formatting that I add there doesn't seem to work. I would like the trendline to have smaller points than the actual, plotted points of the scatterplot, and I would like the trendline series to be black. If I can successfully format the trendline, then this would solve the problem of many high school science teachers who are trying to use Google Drive as a real alternative to Excel in their classes.

这是我的代码:

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

推荐答案

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

With the following code (sample data) I can make some changes, such as color, line width, among others.

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

这篇关于在Google Apps脚本中使用setoption()格式化趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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