Hello there,
I been trying to change the style (background and text color) of individual cells based on their value in a Revit area schedule with Python. I want the values lower than -2.0 in a red color, the values between -2.0 and 2.0 in a green color and the rest regular black.
Although I managed to get the value per cell and the style settings, adjusting the style settings changed the whole table to the new color and not just the cells I wanted to.
Does anybody know how to fix this?
Dynamo script:
Python script:
import sys
import clr
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
schedule_view = UnwrapElement(IN[0])
color_white = Autodesk.Revit.DB.Color(255,255,255)
color_black = Autodesk.Revit.DB.Color(0,0,0)
color_red = Autodesk.Revit.DB.Color(255,0,0)
color_blue = Autodesk.Revit.DB.Color(0,0,255)
color_green = Autodesk.Revit.DB.Color(0,255,0)
color_yellow = Autodesk.Revit.DB.Color(255,255,0)
# Get table data
table = schedule_view.GetTableData()
section = table.GetSectionData(SectionType.Body)
rows = section.NumberOfRows
columns = section.NumberOfColumns
output = []
def styleoptions(style, color_bg, color_tx):
new_style = style
override = new_style.GetCellStyleOverrideOptions()
new_override = override
new_override.BackgroundColor = True
new_override.FontColor = True
new_style.BackgroundColor = color_bg
new_style.TextColor = color_tx
new_style.SetCellStyleOverrideOptions(new_override)
return new_style
for row in range(1,rows):
cell_text = section.GetCellText(row,5) # Get Cell value
cell_text = cell_text[:-3].replace(",",".") # Convert value to float
try:
cell_text = float(cell_text)
TransactionManager.Instance.EnsureInTransaction(doc)
style = section.GetTableCellStyle(row,5)
if cell_text < -2.0:
style_new = styleoptions(style, color_white, color_red)
section.SetCellStyle(style_new)
elif cell_text <= 2.0:
style_new = styleoptions(style, color_white, color_green)
section.SetCellStyle(style_new)
else:
style_new = styleoptions(style, color_white, color_black)
section.SetCellStyle(style_new)
TransactionManager.Instance.TransactionTaskDone()
except: cell_text = None
output.append(cell_text)
OUT = output