excelHtml5 Format column A if condition in column B is met

excelHtml5 Format column A if condition in column B is met

Lara20Lara20 Posts: 10Questions: 4Answers: 0

I am pretty new to Datatables. I now want to format the first Column of my exported Excel sheet, if another column B of that row is <0. Column B is a percentage Value. How can I achieve that?

   $('row c[r^="B"]', sheet).attr( 's', 60); // Format column B to percentage
   $('row c[r^="B"]', sheet).each( function () {

      if (parseFloat($(this).text()) < 0) {
           $(this).('row c[r^="A"]', sheet).attr( 's', '22' ); // this does not work
      }
    });

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    This example shows how to modify and test the cells. That should get you going.

    Colin

  • Lara20Lara20 Posts: 10Questions: 4Answers: 0

    Thank you Colin. Very interesting, but I think my problem is that I have a column with Percentage. I tried to set the style to a colour before, but then the Percentage changed from 15% to 0.15% so I had to use s60 to have the right percentage. I then tried to assign a second style, but I found out that this is not possible, so I thought to have Column B the Percentage style assigned s60 and column A gets a background colour of red. But I now just don't know how I can say Loop through Column B and if B is below 0% meaning its -x% then go to Column A and make background s35. I hope this makes sense even if I would prefer to have Column B defined as Percentage and Font colour red if the value is -x%, but I guess this does not work.

    // Needed to say column B displays % otherwise 15% is 
    // shown as 0.15%
    $('row c[r^="B"]', sheet).attr( 's', 60);
    // Now loop through column B and check if value is -x%
    w c[r^="B"]', sheet).each( function () {
       // If Value of column B is < 0 then it's negative
       if (parseFloat($(this).text()) < 0) {
          // Go to Column A from that specific row and assign s35
           $(this).('row c[r^="A"]', sheet).attr( 's', '22' ); // this does not work
       }
     });
    
  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.