Cell Color in Perspective Table

Hello everyone,

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

image

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

Thank you!

1 Like

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

1 Like

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:

	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

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

The 1st version:

	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:

	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"

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

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

1 Like

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

I tried the following code for “ref_vac_result_id” column and it worked, but same code does not work for the “names” column:

	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

2 Likes

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!

1 Like

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

2 Likes

Great information! Thank you very much, Victor! :slight_smile:

1 Like

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


The script change done and is shown as below:

  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

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.