如何通过脚本修改谷歌表格内嵌图表次级Y轴的文字颜色
你的代码未生效是因为谷歌表格图表配置项的拼写错误:
- 多Y轴的样式配置键名应为
vAxes,而非你所用的YAxis - 文字样式的属性名是首字母小写的
textStyle,而非大写开头的TextStyle
你只需替换原有setOption行的内容即可实现需求,示例如下:
var i = 0; /** 循环遍历所有图表 */ for (var i = 0; i < charts.length; i++) { /** 选中每个图表 */ var cht = (charts[i]); sh.getCharts().forEach(cht => { const container = cht.getContainerInfo(); if (container.getAnchorRow() == row && container.getAnchorColumn() == col) { /** 匹配到目标图表 */ var title = cht.getOptions().get('title') cht = cht.modify() .setOption('vAxes', { // 0为左侧主Y轴配置,不需要可删除 0: { textStyle: { color: 'white', fontName: 'Calibri', fontSize: 14 } }, // 1为右侧次级Y轴配置 1: { textStyle: { color: 'white', fontName: 'Calibri', fontSize: 14 } } }) .build(); sh.updateChart(cht); } }) break; }
如果需要同时修改次级Y轴的标题文字颜色,可新增titleTextStyle配置:
.setOption('vAxes', { 1: { textStyle: { color: 'white', fontName: 'Calibri', fontSize: 14 }, titleTextStyle: { color: 'white', fontName: 'Calibri', fontSize: 16 } } })
若修改后仍未生效,可先确认目标系列的targetAxisIndex确实设置为1,你此前的系列绑定逻辑无需调整。
内容的提问来源于stack exchange,提问作者John Crooks
相关产品推荐
相关产品推荐

