简体   繁体   English

谷歌表格脚本获取图表的标签?

[英]Google Sheets Script get a chart's labels?

So, I have a sheet that covers a year of data, and a bar chart that updates on a trigger to show each month's data specifically.所以,我有一张涵盖一年数据的表格,以及一个在触发器上更新以具体显示每个月的数据的条形图。 It also sets a color for each of the bars when it updates.它还会在更新时为每个条设置颜色。 The problem is that the way I did the color changing, it always has the same color in each position regardless of what the label for the bar is.问题是我进行颜色更改的方式,它在每个 position 中始终具有相同的颜色,无论条形的 label 是什么。 (ie. if it's sorted by descending value, if X has the lowest number it's blue, and Y is second and red, etc. Then the next month Y has less so it's first and blue, and X is red). (即,如果它按降序排序,如果 X 的数字最小,它是蓝色的,Y 是第二个和红色的,等等。然后下个月 Y 的数量较少,所以它是第一个和蓝色,X 是红色)。 I need to set the colors based on the label (so X is blue regardless of if it has more or less than Y in a given month).我需要基于 label 设置 colors (所以 X 是蓝色的,无论它在给定月份是否大于或小于 Y)。 I can't figure out how to reference what the labels are to set the colors though.我不知道如何引用标签来设置 colors。

My code to update the chart and set the colors is:我更新图表并设置 colors 的代码是:

function updateRangeBar(sheet, range, chart)
{
  
  chart = chart.modify()
    .clearRanges()
    .addRange(range)
    .setOption('series.0.items.0.color', "red")
    .setOption('series.0.items.1.color', "blue")
    .setOption('series.0.items.2.color', "yellow")
    .setOption('series.0.items.3.color', "green")
    .setOption('series.0.items.4.color', "orange")
    .setOption('series.0.items.5.color', "black")
    .build()
  sheet.updateChart(chart); 
}

What I'm imagining the code to look like would be like我想象的代码看起来像什么

function updateRangeBar(sheet, range, chart)
{
  var redIndex, blueIndex, yellowIndex, greenIndex, orangeIndex, blackIndex
  for(i = 0; i < [chartBarItemsLength]; i++)
  {
    if([chartBarItemLabel] == "DataHeaderForRed")
    {
       redIndex = i
    }
    elseIf([chartBarItemLabel] == "DataHeaderForBlue")
    {
       blueIndex = i
    }
  }
  
  chart = chart.modify()
    .clearRanges()
    .addRange(range)
    .setOption('series.0.items.'+redIndex+'.color', "red")
    .setOption('series.0.items.'+blueIndex'+.color', "blue")
    .setOption('series.0.items.+yellowIndex'+.color', "yellow")
    .setOption('series.0.items.+greenIndex'+.color', "green")
    .setOption('series.0.items.+orangeIndex'+.color', "orange")
    .setOption('series.0.items.+blackIndex'+.color', "black")
    .build()
  sheet.updateChart(chart); 
}

Where [chartBarItemLabel] and [chartBarItemsLength] are what I need help finding a way to get.其中 [chartBarItemLabel] 和 [chartBarItemsLength] 是我需要帮助找到的方法。

Figured out an answer, though better options would always be nice.想出了一个答案,尽管更好的选择总是很好。

function updateRangeBar(sheet, range, chart)
{
 
  var subRange = range.getSheet().getRange(range.getRow(), range.getColumn(), range.getHeight(), 1)
  chart = chart.modify()
    .clearRanges()
    .addRange(range)
    .build()
  for(i = 0; i < subRange.getHeight(); i++)
  {
    var val = range.getSheet().getRange((subRange.getRow()+(i+1)), subRange.getColumn()).getValue()
      
      if(val.toString() == BLUE_TYPE)
      {
        chart = chart.modify().setOption('series.0.items.'+i+'.color', BLUE_RGB).build()
      }
      else if(val.toString() == RED_TYPE)
      {
        chart = chart.modify().setOption('series.0.items.'+i+'.color', RED_RGB).build()
      }
      else if(
      ...
  }
  sheet.updateChart(chart); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM