Jump to content
  • 0

Changing Background Cell Colours in a Pivot Table


Caron

Question

2 answers to this question

Recommended Posts

  • 0

Hi @Caron,

I created a test Pivot table in my account that has the same layout as on your screenshot.

This is the output:
ccz0yzg.png

 

I used this code in the Footer section of the 'Configure Pivot Table Fields' page:
 

<script>
 document.addEventListener('DataPageReady', colorCellsHandler);
    
 function colorCellsHandler() {
     
    //phases field (field used for a condition to color cells)
    const phases = document.querySelectorAll('td:nth-child(2)');
    
    //cells with  phases; add more variables if there are more phases
    let concept = [];
    let schematic = [];
    let development = [];
    let construction = [];
    let services = [];
    
    //looping through the phases field to populate the array with elements 
    phases.forEach(phase => {

    const selectCells = (array) => {

    //exclude Grand Total field by filtering the elements
      let siblings = Array.from(phase.parentNode.children).filter(child => child !== phase.parentNode.lastChild);
      array.push(siblings);
    }

   
        if(phase.innerText == '02_Concept'){
         selectCells(concept);
            }
         else if(phase.innerText == '03_Schematic Design'){          
            selectCells(schematic);
            }
         else if(phase.innerText == '04_Design Development'){ 
            selectCells(development);
            }
         else if(phase.innerText == '05_Construction Documentation'){         
           selectCells(construction);
            }
        else if(phase.innerText == '06_Site Services'){     
           selectCells(services)
            } 
    })

    //appling color for cells with each phase, empty cells are excluded
    const colorCells = (cells, color) => {
      cells.forEach(cell => {
         cell.forEach(el => {
              if(el.innerText !== "") {
                 el.style.backgroundColor = color;
              }
           })
      })
   }

     //replace color codes for each phase
      colorCells(concept, '#e8aab5');
      colorCells(schematic, '#d5aae8');
      colorCells(development, '#a0aef1');
      colorCells(construction, '#e8ca7d');
      colorCells(services, '#e87d95');

document.removeEventListener('DataPageReady', colorCellsHandler);
}

</script>

You may provide all possible phases and colors you want to apply, so I can modify the code.

Is it the result you wanted to achieve? 

It is possible to modify the code above and to style the Pivot table like this. But the code for this layout is less productive from the performance point of view.
OLxqyGQ.png

Feel free to update this thread in case you have any questions. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...