# Cell Color in Perspective Table

**URL:** <https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 14, 2021, 6:04pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966 "2021-09-14T18:04:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [September 14, 2021, 6:04pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/1 "2021-09-14T18:04:50Z")

</div>

Hello everyone,

For an application, I am trying to change the color of individual cells in the “names” column based on its results.

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

I am not able to find a good way to do it and would appreciate your kind help.

Thank you!

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 14, 2021, 6:28pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/2 "2021-09-14T18:28:47Z")

</div>

> [@Change style of cell (component Table) via script - Perspective](https://forum.inductiveautomation.com/t/change-style-of-cell-component-table-via-script-perspective/44573/2):
>
> Hi @truonghaibk94 this may be best done in a script transform of the database binding. You can loop through the dataset and form an array of dictionaries for each element like this: data [ { "colTitle1": { "value": "SomeValue", "style": { "backgroundColor": "#F7901D", "classes": "SomeClass" } }, "colTitle2": { "value": "SomeValue2", "style": { "backgroundColor": "#FFFFFF", "classes": "SomeClass" } } } { …

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [September 15, 2021, 6:39am UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/3 "2021-09-15T06:39:34Z")

</div>

you'd have to put a transform script like this in the data prop

> [@Perspective table cell color](https://forum.inductiveautomation.com/t/perspective-table-cell-color/50047/5):
>
> Thanks for your help. That has cleaned up the code a little. I’ll work on sorting the key order in the json string later. output\_json = [] style\_red = {"backgroundColor": "red"} style\_green = {"backgroundColor": "green"} style\_gray = {"backgroundColor": "gray"} for row in range(value.getRowCount()): row\_object = {} for col in value.getColumnNames(): cell\_object = {} cell\_style = {} # Add a key/value pair for the cell cell\_object['value']=value.getValueAt(ro…

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [September 15, 2021, 3:41pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/4 "2021-09-15T15:41:18Z")

</div>

Thank you very much, @victordcq and @cmallonee!  
I followed the code below. It worked for the whole row, however, it did not work for the individual cell:

```auto
	output_json = []
	
	style_red = {"backgroundColor": "#FF0000"}
	style_green = {"backgroundColor": "#00AA00", "fontWeight": "bold", "fontSize": 35}
	style_yellow = {"backgroundColor": "#FFFF00"}
	
	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.getValueAt(row, col) == value.getValueAt(4, 0):
				row_style = style_green
			row_object['style'] = row_style
		output_json.append(row_object)
	return output_json

```

This is my table:

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

I tried the following scripts, but none of them worked for individual cells:

The 1st version:

```auto
	output_json = []
	style_red = {"backgroundColor": "red"}
	style_green = {"backgroundColor": "green"}
	
	for row in range(value.getRowCount()):
		row_object = {}
		for col in range(value.getColumnCount()):    
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if value.getColumnName(col) == 'names':
				if value.getValueAt(row, col)=='Flash'
					cell_style= style_red
				elif value.getValueAt(row, col)=='pass'
					cell_style= style_green
			cell_object['style']=cell_style
			row_object[value.getColumnName(col)]=cell_object				
		output_json.append(row_object)		
	return output_json

```

The 2nd version:

```auto
	output_json = []
	style_red = {"backgroundColor": "red"}
	style_green = {"backgroundColor": "green"}
	
	for row in range(value.getRowCount()):
		row_object = {}
		for col in value.getColumnNames():   
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if col == 'names':
				if value.getValueAt(row, col)=='Flash'
					cell_style= style_red
				elif value.getValueAt(row, col)=='pass'
					cell_style= style_green
			cell_object['style']=cell_style
			row_object[col]=cell_object				
		output_json.append(row_object)		
	return output_json

```

Am I doing something wrong here?

**I get configuration error "parse error in script transform"**

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [September 15, 2021, 5:21pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/5 "2021-09-15T17:21:41Z")

</div>

You are missing an ':' after the if checking Flash and pass

> [@pkhoshroo](#):
>
> ```auto
> col) == 'names':
> if value.getValueAt(row, col)=='Flash'
> cell_style= style_red
> elif value.getValueAt(row, col)=='pass'
> cell_style= style_green
> 
> ```

=='Flash':  
...  
=='pass':

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [September 15, 2021, 5:25pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/6 "2021-09-15T17:25:44Z")

</div>

You are correct! Nice catch!  
I have not even seen that!!!  
However, I cannot see any colors in my table now. I tried both scripts.  
It should be red on “Flash” cell and green on the “pass” cell.

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

I tried the following code for “ref\_vac\_result\_id” column and it worked, but same code does not work for the “names” column:

```auto
	output_json = []
	style_red = {"backgroundColor": "#FF0000"}
	style_green = {"backgroundColor": "#00AA00", "fontWeight": "bold", "fontSize": 35}
	
	for row in range(value.getRowCount()):
		row_object = {}
		for col in value.getColumnNames():   
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if col == 'ref_vac_result_id':
				if value.getValueAt(row, col)==2:
					cell_style= style_red
				elif value.getValueAt(row, col)==4:
					cell_style= style_green
			cell_object['style']=cell_style
			row_object[col]=cell_object				
		output_json.append(row_object)		
	return output_json

```

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

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [September 15, 2021, 5:37pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/7 "2021-09-15T17:37:49Z")

</div>

I realized the problem.

The text “Flash” in the table was “Flash…spaces…” with a lot of spaces in front of it. That is why I could not see any colors.

Thank you for your help AS ALWAYS, @victordcq!

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [September 15, 2021, 7:03pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/8 "2021-09-15T19:03:12Z")

</div>

No problem:)  
You could use .strip() to remove leading and trailing spaces value.getValueAt(row,col).strip()  
It might be a good idea to do that before you save the values into the database too  
or wherever you get those values from

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [September 16, 2021, 12:06pm UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/9 "2021-09-16T12:06:27Z")

</div>

Great information! Thank you very much, Victor! 🙂

---

<div class="post-metadata">

**Author:** ![vnayal](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@vnayal](https://forum.inductiveautomation.com/u/vnayal)\
**Post date:** [July 4, 2024, 11:34am UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/10 "2024-07-04T11:34:06Z")

</div>

I tried the same code for one of the table but it is not working as shown below:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/8/3898474586738e17ae8442a8ed704d9517716847.png)  
The script change done and is shown as below:

```python
  output_json = []
	`Preformatted text`style_red = {"backgroundColor": "#FF0000"}
	style_green = {"backgroundColor": "#00AA00"}
	
	for row in range(value.getRowCount()):
		row_object = {}
		for col in value.getColumnNames():   
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if col == 'Bin1PalletFillStatus':
				if value.getValueAt(row, col)>0:
					cell_style= style_red
				elif value.getValueAt(row, col)==0:
					cell_style= style_green
			cell_object['style']=cell_style
			row_object[col]=cell_object				
		output_json.append(row_object)		
	return output_json

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [July 7, 2024, 2:26am UTC](https://forum.inductiveautomation.com/t/cell-color-in-perspective-table/50966/11 "2024-07-07T02:26:40Z")

</div>

Could you copy `props.data` and either paste it here or send it to me as a private message? Do you have any styling applied to the column as a whole? Examine `props.columns[2]` for any styling.
