Google脚本图表默认排除标题 [英] Google Script Charts Excludes Header by Default

查看:67
本文介绍了Google脚本图表默认排除标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下脚本:

Using the following script:

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表格中构建它,虽然图表构建器对话框工作得很好,并且包含数据系列标签。

I've read that it could be due to the format of my header row, which should be text (which mine is, but it doesn't seem to be used). If I try to build it within Google Sheets, though the chart builder dialog, it works just fine and includes the data series labels.

如何设置使用行1作为标题选项与Google Apps脚本?

How do I set the "use row 1 as headers" option with Google Apps Script?

这个问题是类似的,但没有与非文本格式有关的答案。

This question is similar, but has no answer pertaining to non-text format.

推荐答案

注意:本文基于此 SO贴子,甚至可以用于柱状图。尽管列图配置选项未记录它。


系列配置选项中,可以使用 labelInLegend 来设置图例值,如下所示:在此提及 (导航至选项系列)

Note: This post is based on this SO Post and works even for column charts. Even though column charts configuration options dont document it.

Legend values can be set using a labelInLegend in the series configuration options as mentioned here.(Navigate to the option series)

您的代码如下所示:

Your code will look something like this:

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()); 
}

这篇关于Google脚本图表默认排除标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆