如何在Google表格脚本中为一系列数据中的单个数据点设置颜色

时间:2019-06-23 01:12:29

标签: google-apps-script google-sheets google-visualization embedded-resource

我决定在Google表格中创建一个条形图,使用脚本编辑器工具阅读并在适当的位置进行修改,因为我觉得这比从头开始创建一个图表然后添加到表格中要容易得多。事实证明,使用UI创建图表比编写脚本要容易得多。

现在,当某个阈值以上时,我想将单个系列中只有1个数据点的颜色设置为红色。到目前为止,我发现以下代码段可以设置整个系列的颜色,但是我找不到任何设置一个数据点颜色的东西:

  var chart = sheet.getCharts()[0];
  chart = chart.modify()
     .setOption('series.0.color', 'red')
     .build();
  sheet.updateChart(chart);

如果我尝试将setOption替换为setColors(.setColors(['green','red'])),则会出现错误提示

TypeError: Cannot find function setColors in object EmbeddedChartBuilder.

According to this reference,只能在创建新图表时使用setColors。

numerous articles online关于如何通过UI更改条形图中单个数据点的颜色。任何有关如何以编程方式更改颜色的指针将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是设置特定系列颜色的方法:

var chart = sheet.getCharts()[0];
chart = chart.modify()
    .setOption('series.0.color', 'red')
    .setOption('series.0.items.0.color', 'blue')
    .build();
sheet.updateChart(chart);