Google表格脚本-图表图例文本标签 [英] Google sheets script - chart legend text labels
问题描述
如何通过Google表格中的脚本为图表中的图例设置文本标签?我环顾四周,发现所有这些都是 setOption
属性, series
和 legend
,但是它们似乎仅在图表编辑器中设置了相应的设置.(GUI).我知道更改图例文本的唯一方法是在图表中双击它.
How do you set the text label for legends in a chart via scripts in google sheets? I've looked around and all I have found are these setOption
properties series
and legend
but they seem to only set the corresponding settings in chart editor (GUI). The only way I know to change the legend text is by double clicking on it in the chart.
这是我尝试插入的图表
var chart = sheet1.newChart()
.setChartType(Charts.ChartType.LINE)
.addRange(sheet2.getRange('A1:D12'))
.setPosition(5, 5, 0, 0)
.build();
sheet1.insertChart(chart);
这些是我尝试过运气不佳的选项:
These are the options I've tried different settings for with no luck:
.setOption('series',
{
position: 'right',
textStyle: {color: 'orange', fontSize: 16}
})
.setOption('series',
{
0: {dataLabel: 'value'},
1: {dataLabel: 'value'},
2: {datalabel: 'value'}
})
.setOption('legend',
{
position: 'top',
textStyle: {color: 'blue', fontSize: 16}
})
推荐答案
我相信您的目标如下.
- 使用Google Apps脚本创建图表时,您要将范围的第一行设置为标题行.
在这种情况下,如何在类EmbeddedTableChartBuilder中使用 setNumHeaders
?修改脚本后,其内容将如下所示.
In this case, how about using setNumHeaders
in Class EmbeddedTableChartBuilder? When your script is modified it becomes as follows.
var sheet1 = SpreadsheetApp.getActiveSheet();
var chart = sheet1.newChart()
.setChartType(Charts.ChartType.LINE)
.addRange(sheet2.getRange('A1:D12'))
.setPosition(5, 5, 0, 0)
.setNumHeaders(1) // <--- Added
.build();
sheet1.insertChart(chart);
- 通过添加
setNumHeaders(1)
作为上述修改,使用第1行作为标题"被选中. - By adding
setNumHeaders(1)
as above modification, "Use row 1 as headers" is checked.
这篇关于Google表格脚本-图表图例文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!