I have the following chart:
And a script attached to the button:
function updateChart() {
const sheet = SpreadsheetApp.getActiveSheet()
const chart = sheet.getCharts()[0];
SpreadsheetApp.getUi().alert(String(chart.getOptions().get('title')));
SpreadsheetApp.getUi().alert(String(chart.getOptions().get('series.0.color')));
sheet.updateChart(chart.modify().build());
}
The first time I'm pressing this button it does nothing (as expected). The second time it resets line color to blue.
series.0.color
is always null, although title
is there, so I'm not even sure where to get that color to preserve it in between .modify()
and .build()
calls.
How do I prevent updateChart
from resetting the color?
I've tried to inspect options in the published chart as suggested in comments (thanks @TheMaster), unfortunately it still doesn't help with the color issue.
The options are:
{
"hAxis": {
"title": "x"
},
"vAxes": {
"0": {
"title": "10x"
}
},
"series": {
"0": {
"color": "#008000"
}
},
"useFirstColumnAsDomain": true,
"width": 600,
"title": "10x vs \"x\"",
"height": 371
}
And I'm using the following code to get them:
function printOption(opts, name) {
console.log(`${name}: ${opts.get(name)}`)
}
function inspectChart(chart) {
const options = chart.getOptions();
printOption(options, 'title');
printOption(options, 'hAxis.title');
printOption(options, 'vAxes.0.title');
printOption(options, 'series.0.color');
}
Output is:
title: 10x vs "x"
hAxis.title: x
vAxes.0.title: 10x
series.0.color: null
As you can see all properties (including nested within array) are there except color
Another interesting point is that once you try to update the color:
sheet.updateChart(sheet.getCharts()[0].modify().setOption('series', {0: {color: 'green'}}).build());
It applies, but from now you're unable to read that property at all:
sheet.getCharts()[0].getOptions().get('series.0.color')
The line above fails with Unexpected error while getting the method or property get on object Charts.ChartOptions.
Also it looks like no matter what color I choose it is always #008000
in the published chart, which makes me think I should look for the color somewhere else, but I don't know where