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'}
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
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
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
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.
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.
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.
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.