使用以下脚本:
function chartCreation() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet1");
var chart = sheet.newChart().asColumnChart()
.setTitle('Test Chart')
.addRange(sheet.getRange(1, 1, sheet.getLastRow(), 4))
.setPosition(1,1,0,0)
sheet.insertChart(chart.build());
}
Produces a chart like this one, in which the legend is empty (i.e. the data series are unlabeled).
我读到这可能是由于我的标题行的格式所致,该格式应该是文本(我的是,但似乎没有使用)。如果我尝试在 Google Sheets 中构建它,通过图表构建器对话框,它工作得很好并且包含数据系列标签。
如何使用 Google Apps 脚本设置“使用第 1 行作为标题”选项?
这个问题 https://stackoverflow.com/questions/14611197/google-apps-script-chart-use-row-1-as-headers类似,但没有关于非文本格式的答案。
Note:这篇文章是基于这个SO Post https://stackoverflow.com/questions/49176895/google-script-use-row-x-as-headers甚至适用于柱形图。尽管柱状图配置选项 https://developers.google.com/chart/interactive/docs/gallery/columnchart#configuration-options不要记录它。
图例值可以使用设置labelInLegend
in the series
如上所述的配置选项here https://developers.google.com/chart/interactive/docs/gallery/scatterchart#configuration-options.(导航至选项系列)
您的代码将如下所示:
function chartCreation() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet1");
var headerRow = sheet.getRange(1,1,1,4).getValues()
var chart = sheet.newChart().asColumnChart()
.setTitle('Test Chart')
.addRange(sheet.getRange(1, 1, sheet.getLastRow(), 4))
.setPosition(1,1,0,0)
.setOption('series',
{0:{labelInLegend:headerRow[0][1]},
1:{labelInLegend:headerRow[0][2]},
2:{labelInLegend:headerRow[0][3]}})
sheet.insertChart(chart.build());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)