1
votes

I have a Google Sheets spreadsheet with a chart. I'd like to customize the chart colors beyond what is available in the default color palette. I believe this should be possible with Apps Scripts. But I cannot get the following to work for a Line Chart:

function chartColors() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getRange("A1:I100")
 var chart = sheet.getCharts()[0];
 chart = chart.modify()
     .setOption('colors', ['red', 'blue', 'green', 'black'])
     .build();
 sheet.updateChart(chart); 
};

Here is the example I am trying to follow: https://developers.google.com/apps-script/reference/spreadsheet/embedded-chart

I was able to Update Title, but the colors aren't working for some reason.

Suggestions?

1
This worked as is, how many charts do you have in your sheets? Does it give any error when you run it? What do the View>Execution transcript show? - Jack Brown
Sigh, you're right, I had deleted a prior chart so the index may have been referring to that one. It's working now - thanks for commenting here. - misanuk
For some reason, I see no effect when I use .setOption('colors', ['red', 'blue', 'green', 'black']) but it works if I change it to .setOption('slices', ['red', 'blue', 'green', 'black']) - Aaron Dunigan AtLee

1 Answers

1
votes

The above script works. My question was based on an implementation error on my end (wrong index value). Thanks to Jack Brown for commenting and confirming.

Leaving this questions here in case the above helps others add custom chart coloring to Google Sheets Charts.