How to add yellow color to the background cell colors when exporting to Excel and to empty cells ?

How to add yellow color to the background cell colors when exporting to Excel and to empty cells ?

smarcinsmarcin Posts: 2Questions: 1Answers: 0
edited October 2023 in Free community support

Description of problem: I have been following many examples and tutorials how to style rows in excel sheet when exporting a table. I cannot find a way to add yellow color or any other color the background color. There must be a way. And also I have a problem, empty cells are not formatted they stay white.

I used this code.

buttons: [
{
    extend: 'excelHtml5',
    customize: function ( xlsx ) {
      var sheet = xlsx.xl.worksheets['sheet1.xml'];
      jQuery('row', sheet).each( function (x) {
          var colour = jQuery(table.row(':eq('+x+')').node()).css("background-color");            
          if (colour === 'rgb(241, 136, 147)' || colour === 'rgb(241 136 147) !important' || colour === 'red') {
            jQuery('row:nth-child('+(x+3)+') c', sheet).attr( 's', '35' );
          }
          else if (colour === 'rgb(255, 238, 186)' || colour === 'rgb(255, 238, 186) !important' || colour === 'yellow') {
            jQuery('row:nth-child('+(x+3)+') c', sheet).attr( 's', '41' );
          }
          else if (colour === 'rgb(195, 230, 203)' || colour === 'rgb(195, 230, 203) !important' || colour === 'green') {
            jQuery('row:nth-child('+(x+3)+') c', sheet).attr( 's', '40' );
          }
      });
    }
}
]

Answers

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    This example works. If it doesn't help then please provide a link to your page or a test case (update the working case) replicating the issue so we can see what you have to help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • smarcinsmarcin Posts: 2Questions: 1Answers: 0

    I know it works, but my problem is, empty cells are not filled with color. And also there is no yellow color.

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    Its hard to debug code snippets without seeing the context of your table data and configuration.

    I would start by using the browser's debugger to see what the variable colour is within the code. Your problem description suggests that the none of the if statements are true for the cases of an empty cell or the color yellow.

    If you still need help then please provide a test case replicating the issues so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955
    edited October 2023

    It doesn't look like there is a Yellow background color in the Default Styles. You can add to the styles. See this thread for some techniques to add your own custom styles. Unfortunately its not simple to do.

    For the empty cells we will need an example showing the issue to understand why the background color is not set as you expect.

    Kevin

Sign In or Register to comment.