Power table background color change based on another columns value

How do you change the background color of a column based on another columns value. I can change the same column but I don’t know how to get it to change another ones background. The script below just changes that certain column.

if colName == 'ReqReading' and value == 0:
			return {'background' : 'black'}

Has anyone done this before?

	data = self.data
	
	returnValue = {}
	
	if colName == "Col1":
		if value > 10:
			returnValue["background"] = "red"
	elif colName == "Col2":
		if data.getValueAt(rowIndex,"Col1" )>10 and value > 10:
				returnValue["background"] = "blue"
	
	return returnValue
1 Like

that worked great. I was able to modify it to work the way I need it.

I want to add text to the certain row and column through the same script. Is there a way to do this? This is what I have for the solution for the color. but I am not for sure how to add text to the same column. The text I want to add is N/A in white letters.

data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==1:
							returnValue["background"] = "green"
	
	if colName =="Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==0:
								returnValue["background"] = "black"
								
	return returnValue

I have tried this but it takes the black color away.

data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==1:
							returnValue["background"] = "green"
	
	if colName =="Reading":
					if data.getValueAt(rowIndex,"ReqReading" )==0:
								returnValue["background"] = "black"
								textValue["N/A"]="white"
	return returnValue
data = self.data		
	returnValue = {}		
	
	if colName == "Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==1:
			returnValue["background"] = "green"
	
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"

	return returnValue

What happened with your code was it had an exception at the textValue[“N/A”]=“white” line - textValue wasn’t defined so it threw an error, which means it never got to return a value at all. This should work.

My next issue is turning the comments column background red only if the status column texted is Maint Required or Getting Moisture but it turns the whole column red not just the certain row.

data = self.data		
	returnValue = {}		
		
	if colName == "Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==1:
			returnValue["background"] = "green"
		
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"
	
	if colName == 'Status' and value == "Maint Required":
		returnValue ['background' ] = 'red'
	
	if colName =="Comments":
				if data.getValueAt(rowIndex,"Status" )=="Maint Required" or "Getting Mositure":
					returnValue["background"] = "red"
					
	return returnValue

It’s this line:

if data.getValueAt(rowIndex,"Status" )=="Maint Required" or "Getting Mositure"

The right side of the ‘or’ is just a string, which evaluates to true. So it’s ALWAYS true for that column. Try something like this:

if data.getValueAt(rowIndex,"Status" ) in ["Maint Required", "Getting Mositure"]:

It gets the value of the Status column at that row index, and checks to see if it’s in an array with just those two strings in it. If its either one of those, this evaluates to True.

this one worked before I added the one below

if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"

this one below doesn’t seem to work, any ideas?


	
	if data.getValueAt(rowIndex,"Reading" ) < 200:
				return {'background' : 'red'}

any Ideas on the question above, is it over righting it?

The latter operation is a pure return of a fixed dictionary (just for a red background). A return instruction exits the function at that point, yielding just what it’s given. You don’t show enough of your script to be sure, but consider just assigning:

	if data.getValueAt(rowIndex,"Reading" ) < 200:
				returnvalue['background'] = 'red'

Assuming there’s an unconditional return returnvalue later in the script.

That didn’t work either, here is what I have . I have all tried changing it to a elif but still the same result

# reference column name Question
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"
	
	if data.getValueAt(rowIndex,"Reading" ) < 200:
		returnValue["background"] = "red"
	```

Please post your entire script. I’m also now confused as to what each column’s behavior should be.

If the ReqReading column values is equal to 0 then, the Reading column rows goes black and puts a 0 in that row only. ReqReading is a hidden column.The ones left white are for data entry. If the value is greater the the listed value in the question column then I want to change the color of that row.

data = self.data		
	returnValue = {}		
	
	# reference column name Question and Descr
	if colName =="Question":
			
			if data.getValueAt(rowIndex,"Descr" )== "Air Compressor #1 Quincy 750":
				returnValue["background"] = "D5D5D5"	
	
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Thermotech #1 FEVF-13412MA":
				returnValue["background"] = "D5D5D5"	
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Dryer ICP IDHE-800 #1":
				returnValue["background"] = "D5D5D5"
				
			elif data.getValueAt(rowIndex,"Descr" )== "Air Compressor #2 Quincy 750":
				returnValue["background"] = "D5D5D5"		
	
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Thermotech #2 FEVF-13412MA":
				returnValue["background"] = "D5D5D5"		
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Dryer ICP IDHE-1000 for ELGI Air Compressor #1":
				returnValue["background"] = "D5D5D5"
				
			elif data.getValueAt(rowIndex,"Descr" )== "Air Compressor Ingersoll Rand #1 R90i-W145":
				returnValue["background"] = "D5D5D5"
			
			elif data.getValueAt(rowIndex,"Descr" )== "Air Cooling Tower Gunter #1 S-GFW 1/4 for Ingersoll Rand Air  Comp 1 & 2":
				returnValue["background"] = "D5D5D5"
	# reference column name Question
	if colName =="Reading":
		if data.getValueAt(rowIndex,"ReqReading" )==0:
			returnValue["background"] = "black"
			returnValue["text"]="0"
			returnValue["foreground"] = "white"
	
		elif data.getValueAt(rowIndex,"Reading" ) < 200:
			returnValue["background"] = "red"
	
	
	
	if colName == "Status" and value == "Maint Required":
		returnValue ["background"] = "FF4747"
	elif colName == "Status" and value == "Getting Moisture":
				returnValue ["background" ] = "FF4747"
	elif colName=="Status" and value == "OK":
				returnValue ["background" ] = "8AFF8A"
	
	elif colName =="Comments":
		if data.getValueAt(rowIndex,"Status" )=="Maint Required":
			returnValue["background"] = "FF4747"
		if data.getValueAt(rowIndex,"Status" )=="Getting Moisture":
			returnValue["background"] = "FF4747"
		if data.getValueAt(rowIndex,"Status" )=="OK":
			returnValue["background"] = "black"
			returnValue["text"]="N/A"
			returnValue["foreground"]="white"		
	return returnValue

First, none of your returnvalue[] assignments are cumulative. That is, at any branch where you are setting return values, only that branch sets values. So you can use actual return statements for faster execution and better readability. Also, consider adding another hidden column, boolean, to identify the questions that need to be grey. GreyQ, perhaps. Also, consider only using .getValueAt() once per value retrieved, into a temporary variable. Assuming ReqReading is the listed value from the question, I’d have a routine that looks like this:

	ds = self.data		

	if colName == "Question":
		if ds.getValueAt(rowIndex, 'GreyQ'):
			returnValue["background"] = "D5D5D5"	

	elif colName == "Reading":
		ReqReading = ds.getValueAt(rowIndex, "ReqReading")
		if ReqReading:
			if value < ReqReading:
				return {'background': 'red'}
		else:
			return {'background': 'black', 'text': '0', 'foreground': 'white'}

	elif colName == "Status":
		if value in ("Maint Required", "Getting Moisture"):
			return {'background': 'FF4747'}
		if value == "OK":
			return {'background': '8AFF8A'}
	
	elif colName == "Comments":
		Status = ds.getValueAt(rowIndex, "Status")
		if Status in ("Maint Required", "Getting Moisture"):
			return {'background': 'FF4747'}
		if Status == "OK":
			return {'background': 'black', 'text': 'N/A', 'foreground': 'white'}

I would also consider encoding more of this information in hidden columns.