I want to create a dynamic chart that changes the color based on a cell value. I used the example in this answer but it only uses the first color I declared in the legend as seen in this image:
function modifyChart(sheet, newCssColor) {
// Assume there is only one chart on this sheet.
var sheet = SpreadsheetApp.getActive().getActiveSheet();
const charts = sheet.getCharts();
var array = [];
var colorValues = sheet.getRange("G4:G6").getValues();
for(var i = 0; i < colorValues.length; i++){
array.push(colorValues[i][0]);
}
Logger.log(colorValues);
const barBuilder = charts[0].modify().asColumnChart().setColors(array);
sheet.updateChart(barBuilder.build());
}
But here's exactly want to do:
If score <= 49 set bar color to red
Else if score >= 50 and score <= 89 set bar color to orange
else set bar color to green
Just like how the cell background changes because I set rules to it using Conditional Formatting.
Edit: Change the the cell range to match the sample
