Jump to content
  • 0

Changing Background Cell Colours in a Pivot Table



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:


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

 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);

        if(phase.innerText == '02_Concept'){
         else if(phase.innerText == '03_Schematic Design'){          
         else if(phase.innerText == '04_Design Development'){ 
         else if(phase.innerText == '05_Construction Documentation'){         
        else if(phase.innerText == '06_Site 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);


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.

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.

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.

  • Create New...