Conditional formatting in Confluence
Asked by Rumceisz:

Hi All,

we need a macro or plugin which enables us to create conditional fields. Just like Excel does: based on the value of a field, another field will be set to a custom value. More specifically our management likes the green-yellow-red status macro and we'd like to implement that based on the value of a field then the status will change to green (good) or red (bad). Is it somehow poosible?

Thanks in advance,

Rumi


Answers:
author: Juliette Lallement _Elements Apps_
body: Hi [@Rumceisz](/t5/user/viewprofilepage/user-id/783224)

Thank you for sharing your use case!

Ig you're open to third party apps, I can recommend you [Elements Spreadsheet](https://marketplace.atlassian.com/apps/1212486/elements-spreadsheet-for-confluence?hosting=cloud&tab=overview&utm_source=community&utm_medium=referral&utm_campaign=question-111762), which I think will cover your need through the [Project Tables macro](https://doc.elements-apps.com/spreadsheet/project-tables-macro/getting-started-with-project-tables). It looks exactly like a Confluence table, it is very user-friendly and requires no learning curve.

Let's consider the following use case:

- I have 2 columns in my table, one for tracking the allocated budget, another for tracking the actual cost

![first step.png](https://community.atlassian.com/t5/image/serverpage/image-id/305386iF09F5845DE6781C8/image-size/large?v=v2&px=999 "first step.png")

- Now, I want to track the remaining budget status (Under the line/Budget met) for each row based on the following calculation: Allocated budget - Actual Cost. I can capture that information by configuring a calculation-type column:

![step 2.png](https://community.atlassian.com/t5/image/serverpage/image-id/305387iF7583FABEAFB169C/image-size/large?v=v2&px=999 "step 2.png")

Here is a sneak peak of the Remaining budget column formula. You can adapt the formula to anything that you'd like. You can use specific thresholds if that works best for you, e.g. IF({Allocated budget} \> 30.000, "pass condition", "fail condition").

![step 2-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/305388i6211F1657FDF8D0E/image-size/large?v=v2&px=999 "step 2-1.png")

- Now if you'd like to apply a specific color code for each of these status values (Under the line/Budget met), you can use the conditional coloring feature:

![step 3.png](https://community.atlassian.com/t5/image/serverpage/image-id/305389i894F5A5CF7A10841/image-size/large?v=v2&px=999 "step 3.png")

And that's it.

I hope this helps! Feel free to reach out if you need further assistance!

PS: I am the Product Manager of the app


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi guys,  

The [Table Filter, Charts \& Spreadsheets](https://marketplace.atlassian.com/apps/27447/table-filter-charts-spreadsheets-for-confluence?hosting=cloud&tab=overview&utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) app (that is our add-on) has been already mentioned in this thread regarding native Confluence tables: the [Table Transformer](https://docs.stiltsoft.com/tfac/cloud/advanced-table-cells-formatting-58426218.html#Advancedtablecellsformatting-Conditionalinsertingofstatuses?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro allows you to set colorful statuses based on different conditions.  

But if you prefer an Excel-like functionality, you may also use the [Table Spreadsheet](https://docs.stiltsoft.com/tfac/cloud/how-to-use-table-spreadsheet-macro-83100938.html?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro that is also provided by the app. With the help of this macro, you'll be able to set statuses automatically using cell formulas and apply familiar Excel-like conditional formatting via a user-friendly UI.  

![Fri 4-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/305495iAD6143B0F0EB42D3/image-size/large?v=v2&px=999 "Fri 4-1.png")  

Please book a [live demo](https://calendly.com/tablefilter/live-demo?month=2024-02) from our team -- we'll be happy to arrange it and discuss your cases.


author: Davin Studer
body: What do you mean by field? Is this something that is enetered by a user, is it changed automatically by some system, is it getting it from a web service? How is the "field" data changing?  

As far as the status maco is concerned you could change that on-the-fly with Javascript after page load by simply adding or removing a CSS class to the element.

```
Color        CSS Classes----------------------------------------------------------Grey         status-macro aui-lozengeRed          status-macro aui-lozenge aui-lozenge-errorYellow       status-macro aui-lozenge aui-lozenge-currentGreen        status-macro aui-lozenge aui-lozenge-successBlue         status-macro aui-lozenge aui-lozenge-complete
```

comments: author: Rumceisz
body: Hi Davin,

I mean that I would have a table in Confluence. There is a cell where I add number value. Based on this number another field would have various value: e.g. if the number cell has value of 100 then the other value gets text value: Good, if the number cell has value of 30 then the text cell gets value of Bad.

Sorra for confusing you with fields. In fact there are no fields in Confluence, it's JIRA:). I talk about a table in Confluence.

Thanks in advance,

Rumi


author: Davin Studer
body: Does the value entered need to persist over page reloads, or would it be enetered everytime the page is accessed?


author: Rumceisz
body: Hi,

the value would be updated in every 2 weeks: so in every 2 weeks this particular page would be edited. I hope that helps.


author: Davin Studer
body: So, just to be clear would you be editing the page(in edit mode) and putting data into a table. When the page is saved based on the data in the table you want to have a status macro updated to reflect a formula calculation?


author: Rumceisz
body: Hi Davin,

yes, exactly!


author: Davin Studer
body: This could be done then with a user macro and a little bit of javascript. Here is a down and dirty example. You will have to fill out the formula logic yourself.

**Macro Name:**macro_name

**Macro Title:**Macro Title

**Macro Body Processing:**Rendered

**Template:**

```
## Developed by: Davin Studer## Date created: 07/08/2014## @noparams#set( $id = $action.dateFormatter.calendar.timeInMillis )<div id="table$id">    $body    <p>        <ac:structured-macro ac:name="status">            <ac:parameter ac:name="colour">Grey</ac:parameter>            <ac:parameter ac:name="title">Grey</ac:parameter>        </ac:structured-macro>    </p></div><script type="text/javascript">AJS.toInit(function(){    var results = 0;        /////////////////////////////////////////////////////////////////////////////////    // Code here to set the results variable based on your formula.                //    /////////////////////////////////////////////////////////////////////////////////        switch (results) {        case 1:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-error'); //Red            AJS.$('#table$id span.status-macro.aui-lozenge').text('Red Text');            break;        case 2:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-current'); //Yellow            AJS.$('#table$id span.status-macro.aui-lozenge').text('Yellow Text');            break;        case 3:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-success'); //Green            AJS.$('#table$id span.status-macro.aui-lozenge').text('Green Text');            break;        case 4:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-complete'); //Blue            AJS.$('#table$id span.status-macro.aui-lozenge').text('Blue Text');            break;    }});</script>
```


author: Davin Studer
body: Does something like this meet your needs?


author: Rumceisz
body: Hi Davin,

it doesn't work at all.


author: Davin Studer
body: Well, you have to fill out the code to calculate the formula to change the macro class. Aside from that it should work.


author: Barbara Mason
body: Is it possible to convert JIRA data to colors when presented in Confluence using a similar script, or does the above script only work if you manually enter data into a table in Confluence?

Example: We have a JIRA field 'Health' = At Risk, which would be associated as a YELLOW. Our Confluence page uses JIRA Filter Results to present a status report that includes several fields, including Health. We need to show the Health as Yellow on the Confluence page without manual data entry -- just rely on the JIRA data.


author: ncoronado
body: [@Davin Studer](/t5/user/viewprofilepage/user-id/781467) , how do you add your macro to a confluence page? I'm looking for the following logic below, if a table cell contains 'PASS' then make it green, 'FAIL', then make it red etc.


author: Davin Studer
body: The above code is a start for a user macro. You have to create the user macro in Confluence Admin -\> User Macro. Also, the cloud offering of Confluence does not support user macros. So, you need to have server or data center. Once you create the user macro you can put it on a page just like any other macro. It will show up in the macro browser. Here is an example of the above code edited to look at the contents of a table. It will scan all the table cells and if it finds any that have the word fail in them then the status macro will be turned red. Otherwise it will be green.

```
## Developed by: Davin Studer## Date created: 07/08/2014## @noparams#set( $id = $action.dateFormatter.calendar.timeInMillis )<div id="table$id">    $body    <p>        <ac:structured-macro ac:name="status">            <ac:parameter ac:name="colour">Grey</ac:parameter>            <ac:parameter ac:name="title">Grey</ac:parameter>        </ac:structured-macro>    </p></div><script type="text/javascript">AJS.toInit(function(){    var results = 0;        AJS.$('#table$id td').each(function(){        if(AJS.$(this).text().toLowerCase() == "fail") {            results = 1;            return false;        }    });    switch (results) {        case 0:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-success'); //Green            AJS.$('#table$id span.status-macro.aui-lozenge').text('Green Text');            break;        case 1:            AJS.$('#table$id span.status-macro.aui-lozenge').addClass('aui-lozenge-error'); //Red            AJS.$('#table$id span.status-macro.aui-lozenge').text('Red Text');            break;    }});/script>
```

**This is what the page looks like in edit mode.**

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/80251i35269933B2417CD7/image-size/large?v=v2&px=999 "image.png")

**This is what it looks like when the page is viewed**

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/80251i35269933B2417CD7/image-size/large?v=v2&px=999 "image.png")


author: Andrey Khaneev _StiltSoft_
body: This can be done with the [Table Transformer](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?hosting=cloud&tab=overview) macro: [see examples](https://docs.stiltsoft.com/display/TFAC/Custom+Transformation+-+use+cases+with+advanced+SQL+queries).

comments: author: Michael Daoust
body: [@Andrey Khaneev _StiltSoft_](/t5/user/viewprofilepage/user-id/781696) I dont see a table transformer macro in the market place.

Your link goes to table filter and charts(which i have), and the example references a different macro all together. Please clarify.


author: Michael Daoust
body: Ah I see, I figured it out. Thanks for making such a useful addon.


Tags: confluence-administration

filter

table

table-filter

table-filter-and-charts

table-filter-charts-spreadsheets

table-spreadsheet

table-transformer