Hello, Looking for some help with Table Scripting.
The table has 2 date columns (Need by Date and First Dispatch). I would like to add a conditioning scripting with adding background color to entire row
if First Dispatch <= Need by date - 4Days : Color to Yellow.
You could use a power table, then under scripting you could write code to set the background color in the configureCell script.
Otherwise if you want/need to use a regular table, it looks like you could add a column that is a boolean value which has the value of First Dispatch <= Need by date - 4Days. Then you could use the values in that column to map the colors of each row. You can find these settings under Customizers > Table Customizer > Background Color Mapping. If you use this route, you would then want to go into Customizers > Table Customizer > Column Configuration and set the visibility of this new boolean column to false, so it doesnât show in your table.
I donât know how you are getting the data, but if it is coming from a database, it would be pretty easy to calculate the boolean value in your query, then use that to map the background color. Otherwise, the power table solution may be the easier route.
The indentation of that function didnât paste correctly. Since the code you put into the editor here is within the function configureCell that is defined at the top of the window, you need the if to be indented once to the right in order to follow pythonâs whitespace rules.
this is the indentation that you want:
The following script is still not working:
if self.data.getValueAt(rowIndex, âFirst Dispatchâ) <= system.date.addDays(self.data.getValueAt(rowIndex, âFirst Delivery Need byâ), -40):
return {âbackgroundâ:âYellowâ}
else:
return {âbackgroundâ:âwhiteâ}
The first rowâs background should fill âyellowâ
the error on output console is: 14:09:50.340 [AWT-EventQueue-2] ERROR Vision.Components.AdvancedTable - Error invoking extension method. org.python.core.PyException: Traceback (most recent call last):
*** File ââ, line 34, in configureCell*** TypeError: addDays(): 1st arg canât be coerced to java.util.Date
From the error message I can see that the part of your code that is getting the date values from the tableâs data property is not returned a datetime object. If I had to guess, that is either giving you the date as a integer (milliseconds since unix epoch) or maybe the string representation of the date. What you need to do is pull those values from the dataset before your if statement, then convert them to datetime objects.
If it is a string, then you can use the function system.date.parse to turn it into a datetime obejct, and if it is an integer representing milliseconds since epoch you can use the system.date.fromMillis function to convert it.
This is just a guess, so I very well could be wrong. If you store those values into variables above your if statement and print out their types and values it may give you more insight on what the solution is.
Just a note that there is no need to import the datetime package. Ignitionâs built-in system.date.* functions will do everything you need, and be a bit more readable IMHO.
#Define dates
firstdispatch = self.data.getValueAt(rowIndex, 'First Dispatch')
firstdelneedby = self.data.getValueAt(rowIndex, 'First Delivery Need by')
firdisdate = system.date.parse(firstdispatch, 'MM/dd/yyyy')
firdelneedby = system.date.parse(firstdelneedby,'MM/dd/yyyy')
#Subtract 10 days from need by
needby = system.date.addDays(firdelneedby,-10)
if system.date.isAfter(needby,firdisdate):
return {'background':'yellow'}
return {'background':'red'}
I would also recommend making your datasetâs columns actual Date types and only formatting them for presentation. Parsing strings into dates is a relatively expensive operation (formatting is as well, but youâre doing more parsing than formatting in this use case).