# Perspective Table - Invididual Cell Conditional Formatting

**URL:** <https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371>\
**Category:** Ignition\
**Tags:** perspective, scripting, ignition80\
**Created:** [October 5, 2020, 1:50pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371 "2020-10-05T13:50:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![msusi](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@msusi](https://forum.inductiveautomation.com/u/msusi)\
**Post date:** [October 5, 2020, 1:50pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/1 "2020-10-05T13:50:04Z")

</div>

Hello everyone.

I am having some difficulty with individual cell conditional formatting on the new perspective tables. I am using the script below from another post to apply formatting based upon a cell return. While this works for applying the background colour to the entire row, I need this to be cell specific. I assume I need to make my JSON return more specific/requiring a specific key/value pattern but have not been successful in doing so. Has anyone had luck with this?

Ignition Version: 8.0.5  
OS: Ubuntu LTS 18.04

 ![ConditionalFormat](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/4/a4f7befc68a7822a66394032144b7899cc69fd4d.png)

```auto
	output_json = []

	style_orange = {"backgroundColor": "#F7901D"}
	style_green = {"backgroundColor": "#00AA00"}
	
	for row in range(value.getRowCount()):
			row_object = {}
			row_value = {}
			row_style = {}
			for col in range(value.getColumnCount()):    
				row_value[value.getColumnName(col)] = value.getValueAt(row, col)
				row_object['value'] = row_value
				
				if value.getColumnName(col) == 'CC_msval':
					if value.getValueAt(row, col) < 95:
						row_style = style_orange
					elif value.getValueAt(row, col) >= 95:
						row_style = style_green
	
				row_object['style'] = row_style
				
			output_json.append(row_object)
			
	return output_json

```

---

<div class="post-metadata">

**Author:** ![msusi](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@msusi](https://forum.inductiveautomation.com/u/msusi)\
**Post date:** [October 12, 2020, 11:37pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/2 "2020-10-12T23:37:52Z")

</div>

A quick chat with support has gotten this solved! For those that come across this, see attached.

```auto
	output_json = []
	style_orange = {"backgroundColor": "#F7901D"}
	style_green = {"backgroundColor": "#00AA00"}
	for row in range(value.getRowCount()):
		row_object = {}
		row_value = {}
		row_style = {}
		for col in range(value.getColumnCount()):    
# row_value[value.getColumnName(col)] = value.getValueAt(row, col)
# row_object['value'] = row_value
			if value.getColumnName(col) == 'CC_msval':
				if value.getValueAt(row, col) < 95:
					row_object[value.getColumnName(col)] = {"value": value.getValueAt(row, col), "style": style_orange}
# row_style = style_orange
				elif value.getValueAt(row, col) >= 95:
					row_object[value.getColumnName(col)] = {"value": value.getValueAt(row, col), "style": style_green}
# row_style = style_green
			else:
				row_object[value.getColumnName(col)] = value.getValueAt(row, col)
# row_object['style'] = row_style
		output_json.append(row_object)
	return output_json

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 18, 2020, 3:50pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/3 "2020-11-18T15:50:35Z")

</div>

For anyone wondering where this code goes -

- Select the table.
- Click on the table data binding link.
- Select _Add transform_.
- Select _Script_.
- Paste in the code (and remove the existing _return value_ line at the bottom).

Thanks, msusi.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 18, 2020, 5:41pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/4 "2020-11-18T17:41:23Z")

</div>

A _maybe_ more maintainable variation, if you ever intend on adding other columns you need to style:

```python
	def cc_msval(v, r, c):
		style_orange = {"backgroundColor": "#F7901D"}
		style_green = {"backgroundColor": "#00AA00"}
		return {
			"value": v.getValueAt(r, c),
			"style": style_orange if v.getValueAt(r, c) < 95 else style_green
		}

	transforms = {
		"CC_msval": cc_msval
	}

	return [
		{
			value.getColumnName(col): transforms.get(value.getColumnName(col), lambda v, r, c: v.getValueAt(r, c))(value, row, col)
			for col in xrange(value.columnCount)		
		}
		for row in xrange(value.rowCount)
	]

```

You could just make a new function, add it to the `transforms` dictionary, keyed to your column name. The slightly weird syntax at the end is just a nested comprehension to build the required JSON structure.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 18, 2020, 5:44pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/5 "2020-11-18T17:44:23Z")

</div>

Thanks, Phil.  
How would you reference a _Perspective_ style in the code? (Adding inline style is naughty!)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 18, 2020, 5:54pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/6 "2020-11-18T17:54:08Z")

</div>

Just adjust the inner function to return another nested object:

```python
	def cc_msval(v, r, c):
		return {
			"value": v.getValueAt(r, c),
			"style": {
				"classes": "style_orange" if v.getValueAt(r, c) < 95 else "style_green"
			}
		}

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [November 18, 2020, 7:45pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/7 "2020-11-18T19:45:09Z")

</div>

Thanks, Phil. Received and understood!

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 18, 2020, 7:52pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/8 "2020-11-18T19:52:00Z")

</div>

_Pssst!_ That’s _Paul!_

---

<div class="post-metadata">

**Author:** ![suryawanshinr2011](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@suryawanshinr2011](https://forum.inductiveautomation.com/u/suryawanshinr2011)\
**Post date:** [March 31, 2021, 7:42am UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/9 "2021-03-31T07:42:46Z")

</div>

Can we add more than two colour?Actually in our case we want four colours but it is taking time to discover , anyone having solution it would be helpful

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [August 31, 2023, 10:17pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/10 "2023-08-31T22:17:27Z")

</div>

> [@Transistor](#):
>
> For anyone wondering where this code goes -
> 
> - Select the table.
> - Click on the table data binding link.
> - Select _Add transform_.
> - Select _Script_.
> - Paste in the code (and remove the existing _return value_ line at the bottom).

I want to clarify.  
Did you miss one step above, before "Add Transform".  
Bind to the datasource (a dataset in this case)?

Reason I ask is, my datasource, a dataset, comes from a script.  
I did my conversion from dataset to object on the script before writing to table.data property.

I like the idea where, the datasource and the formatting script are independent and on different places. Like the quoted above.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [August 31, 2023, 10:27pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/11 "2023-08-31T22:27:26Z")

</div>

> [@paul-griffith](#):
>
> Just adjust the inner function to return another nested object:

I wish I could follow, I will try.  
Below is sample dataset written to a table, executed on button click.

```auto
	headers = ["Month","Score"]
	rows = []
	
	rows.append(["June",98])
	rows.append(["April",90])
	rows.append(["December",95])
	
	ds = system.dataset.toDataSet(headers, rows)
	
	self.getSibling("Table_0").props.data = ds

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3b8b325385d749566f63e0ddc7545bb2acb79e1d.png)

if score is \< 95. cell background must be red, otherwise green.  
My goal is clean and fast to understand code.

Output dsObject, Should look like below

```auto
[
  {
    "Month": "June",
	"Score": {
      "value": 98,
      "style": {
        "backgroundColor": "green"
      }
    }
  },
  {
    "Month": "April",
	"Score": {
      "value": 90,
      "style": {
        "backgroundColor": "red"
      }
    }
  },
  {
    "Month": "December",
	"Score": {
      "value": 95,
      "style": {
        "backgroundColor": "green"
      }
    }
  }
]

```

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 1, 2023, 12:29am UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/12 "2023-09-01T00:29:06Z")

</div>

Alas! here it is:

```auto
#ds is the input source dataset
#dsObject is the Object Version with condition style formatting
	
dsObject= [] #list of row objects
	
styleRed = {"color": "white", "backgroundColor": "red"}
styleGreen = {"color": "blue", "backgroundColor": "green"}
	
for row in range(ds.getRowCount()):
	rowObject = {}
	for col in range(ds.getColumnCount()):
		cellObject = {} # has value, and style
		cellStyle = {} # has the style define above
		cellObject['value'] = ds.getValueAt(row,col) # value = "June"

		#< modify this part of code>
		if ds.getColumnName(col) == 'Score':
			if ds.getValueAt(row,col) < 95:
				cellStyle = styleRed
			else:
				cellStyle = styleGreen
		#</Modify>

		cellObject['style'] = cellStyle #empty for column "Month"
		rowObject[ds.getColumnName(col)] = cellObject
	dsObject.append(rowObject)
	
self.getSibling("Table_0").props.data = dsObject

```

---

<div class="post-metadata">

**Author:** ![Eric\_Meyer](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/eric_meyer/32/72323_2.png) [@Eric\_Meyer](https://forum.inductiveautomation.com/u/Eric_Meyer)\
**Post date:** [August 21, 2025, 9:31pm UTC](https://forum.inductiveautomation.com/t/perspective-table-invididual-cell-conditional-formatting/39371/13 "2025-08-21T21:31:05Z")

</div>

Nice, Eugene. Used this today! Couldn’t find a way to do this in the interface column styling option so here I am. Thanks.
