Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

88
Visualizações
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. (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). 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). I can't figure out how to reference what the labels are to set the colors though.

My code to update the chart and set the colors is:

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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); 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda