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

539
Visualizações
Google sheets script - Unable to get background color from cells that are part of pivot table

I have written the following code which replicates a Google Sheets pivot table in HTML along with the formatting to send out in an email.

issue: getBackgrounds() does not return the colours of the cells of the pivot table (it always returns #FFFFF). I verified the range to be correct, because the table is rendered with the correct amount of rows and columns along with the data values. However without styling. Further, if the colour of a cell is changed manually in the Sheets UI before running the script, then getBackgrounds() will fetch that colour. How can I retrieve the formatting of the pivot table?

code:

function getData(){
  let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TCD").getRange(5,1).getDataRegion();
  let background = ss.getBackgrounds();
  let val = ss.getDisplayValues();
  let fontColor = ss.getFontColors();
  let fontStyles = ss.getFontStyles();
  let fontWeight = ss.getFontWeights();
  let fontSize = ss.getFontSizes();
  return [val,background,fontColor,fontStyles,fontWeight,fontSize];
}

function setupEmail(attachment, sendBool) {
  let ss_data = getData();
  let data = ss_data[0];
  let background = ss_data[1];
  let fontColor = ss_data[2];
  let fontStyles = ss_data[3];
  let fontWeight = ss_data[4];
  let fontSize = ss_data[5];

  html += "<table border='1' style=\"background-color:#000000;\">"; 
  for (let i = 0; i < data.length; i++) {
      html += "<tr>" 
      for (let j = 0; j < data[i].length; j++) { 
          html += "<td style=\"height:20px;background-color:" + background[i][j] + "; color: " + fontColor[i][j] + "; font-style: " + fontStyles[i][j] + "; font-weight: " + fontWeight[i][j] + "; font-size: " + (fontSize[i][j] + 6) + "px; text-align: center;\">" + data[i][j] + "</td>";
          Logger.log(background[i][j]);
      }
      html += "</tr>";
  }
  html + "</table>"

EDIT: I have worked around this by hardcoding the background-color. If anyone knows why the background colors of a pivot table cannot be returned, please do contribute.

about 4 years ago · Juan Pablo Isaza
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