【问题标题】:Google Script - Use row x as headersGoogle Script - 使用第 x 行作为标题
【发布时间】:2018-03-08 15:36:11
【问题描述】:

我有一个在谷歌表格中制作折线图和散点图的系统。自 2018 年 3 月 7 日起,图表开始将我的第一行数据作为图表的一部分,而不是列的标题。

如果我手动制作图表,那么它将使我的第一行成为图表的标题。此外,如果我编辑当前图表并勾选使用行 x 作为标题,那么我的标题将恢复到原来的样子。

我的问题是,是否有一种方法可以在谷歌脚本中编程,或者我可以使用的解决方法。

我尝试过遵循本指南: Google Apps Script Chart Use row 1 as headers

但正如其他人评论的那样,它不再有效,在我的测试中它没有。

谢谢!

编辑:这就是我构建图表的方式:

var currChart = currSheet.newChart();
currChart.setPosition (row, col, 5,5)
var builtChart = currChart.asScatterChart()
                        .addRange(currRange)
                        .setTitle(title)
                        .setYAxisTitle(axisLabel)
                        .setOption('useFirstColumnAsDomain','true')
                        .setOption('hAxis.viewWindow.min', min)
                        .setOption('hAxis.viewWindow.max', max)
                        .setOption('chartArea.left', 80)
                        .setOption('chartArea.top', 60)
                        .setOption('chartArea.width', 360)
                        .setOption('chartArea.height', 240)
                        .setOption('chartArea.backgroundColor',
                                  { stroke: "#828282", strokeWidth: 2, fill: "#fcfcfc" })
                        .setOption('series', 
                                  {0:{color: '#54B4C6', pointSize: 3, pointShape: 'circle'},
                                   1:{color: '#2F53D9', pointSize: 10, pointShape: 'diamond'},
                                   2:{color: '#F1BE00', pointSize: 4, pointShape: 'circle'},
                                   3:{color: '#D90D0C', pointSize: 11, pointShape: 'diamond'}})
                        .build();

currSheet.insertChart(builtChart);

【问题讨论】:

  • 几乎与this question完全相同
  • @Brian 您的回答与柱形图有关,并且可以将它们转置。但是,散点图不能转置。
  • @Brian 我不确定柱形图中的图例。但是,可以在散点图中定义图例,如下面的答案所述。
  • @JackBrown 感谢您的澄清。它不在柱形图选项中。为什么不同类别的选项会有所不同一直是个谜,尤其是在这种细微差别的情况下。
  • @Brian 我在柱形图上尝试了下面的代码,它似乎设置了图例!但是documentation 并没有提到它!

标签: javascript google-apps-script google-sheets


【解决方案1】:

您可以在series 配置选项中使用labelInLegend 设置图例值,如here 所述。(导航到选项系列)

所以在上面的代码中,假设您已经提取了数组中的第一行,如下所示:

 var values = currRange.getValues()
 var headerRow = []
 for (var i =0; i<values[0].length ; i++)
   headerRow[i] = values[0][i]

您可以像这样使用 setOptions 设置系列的值:

.setOption('series', 
            {0:{color: '#54B4C6', pointSize: 3, pointShape: 'circle',labelInLegend:headerRow[1]},
             1:{color: '#2F53D9', pointSize: 10, pointShape: 'diamond',labelInLegend:headerRow[2]},
             2:{color: '#F1BE00', pointSize: 4, pointShape: 'circle',labelInLegend:headerRow[3]},
             3:{color: '#D90D0C', pointSize: 11, pointShape: 'diamond',labelInLegend:headerRow[4]}})

简而言之,只需在您的系列对象中包含以下key:valuelableInLegend:'Header Value'
注意:跳过标题的第一个值,因为它与垂直轴有关。
您的最终代码将如下所示:

var values = currRange.getValues()
 var headerRow = []
 for (var i =0; i<values[0].length ; i++)
   headerRow[i] = values[0][i]
 var currChart = currSheet.newChart();
  currChart.setPosition (row, col, 5,5)
 var builtChart = currChart.asScatterChart()
                    .addRange(currRange)
                    .setTitle(title)
                    .setYAxisTitle(axisLabel)
                    .setOption('useFirstColumnAsDomain','true')
                    .setOption('hAxis.viewWindow.min', min)
                    .setOption('hAxis.viewWindow.max', max)
                    .setOption('chartArea.left', 80)
                    .setOption('chartArea.top', 60)
                    .setOption('chartArea.width', 360)
                    .setOption('chartArea.height', 240)
                    .setOption('chartArea.backgroundColor',
                              { stroke: "#828282", strokeWidth: 2, fill: "#fcfcfc" })
                        .setOption('series', 
                                   {0:{color: '#54B4C6', pointSize: 3, pointShape: 'circle',labelInLegend:headerRow[1]},
                                   1:{color: '#2F53D9', pointSize: 10, pointShape: 'diamond',labelInLegend:headerRow[2]},
                                   2:{color: '#F1BE00', pointSize: 4, pointShape: 'circle',labelInLegend:headerRow[3]},
                                   3:{color: '#D90D0C', pointSize: 11, pointShape: 'diamond',labelInLegend:headerRow[4]}})
                        .build();
currSheet.insertChart(builtChart);

【讨论】:

  • 好吧,我今天试试这个,让你知道。谢谢!
  • 当值来自一系列公式时,我如何获取它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
相关资源
最近更新 更多