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?
A maybe more maintainable variation, if you ever intend on adding other columns you need to style:
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.
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
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.
#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