Excel Export: Adding background color to a cell if meets a condition

Excel Export: Adding background color to a cell if meets a condition

vemonvemon Posts: 2Questions: 1Answers: 0

Hello! How would I go about adding a background color using the customize function to add a background color if the cell in the table meets the requirements? For example, a column called **replay **has values of "Approved", "Overturned", and "Check Supervisor". Approved => Green, Overturned => Yellow, Check Supervisor => Blue.

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    This example shows how you might apply a condition in the customize function.

    Allan

  • ttse23ttse23 Posts: 10Questions: 3Answers: 0

    This should do the trick!

    $('table.data-table').DataTable({
    
    
      dom: 'Bfrtip',
      buttons: [{
        extend: 'excel',
        text: 'Save as Excel',
        customize: function(xlsx) {
          var sheet = xlsx.xl.worksheets['sheet1.xml'];
          $('row:first c', sheet).attr('s', '7');
          //color the background of the cells if found the value
          $('row c[r^="C"]',sheet).each(function(){
            if($('is t', this).text() =="Not Approved"){
                $(this).attr('s', '15');
            } 
            else if ($('is t', this).text()=="Approved"){
                $(this).attr('s', '12');
            }
            else {
                $(this).attr('s', '34');
            }
           
            
          });
    
This discussion has been closed.