Freakishly
Freakishly

Reputation: 1571

How can I set the color for a single data point in a series in Google Sheets script

I decided to create a bar chart inside my Google Sheet, read it in using the Script Editor tool & modify it in place, as I felt this would be easier than creating one from scratch & then adding it to the Sheet. Turns out, using the UI to create the Chart has been much easier than scripting it.

Now, I would like to set the color of only 1 of the data points in a single series to be red, when it's over a certain threshold. So far, I have found that the following snippet can set the color of the entire series, but I can't find anything to set the color of just one data point:

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

If I try to replace setOption with setColors (.setColors(['green', 'red'])), I get an error saying

TypeError: Cannot find function setColors in object EmbeddedChartBuilder.

According to this reference, setColors can only be used when creating a new chart.

There are numerous articles online about how to change the color of a single data point in a bar chart from the UI. Any pointers on how to change the color programmatically would be much appreciated.

Upvotes: 2

Views: 1883

Answers (1)

IMTheNachoMan
IMTheNachoMan

Reputation: 5821

This is how you set the color of a specific series:

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

Upvotes: 2

Related Questions