Cell color scripting

i have a power table, in one column Name is ‘SerNum’
in this picture we can see there are 3 row have the same data , I want to use a different color to highlight it(such as yellow)
i don’t know how to scripting it.

This will highlight the row, but it’s inefficient, as it has to extract the column for every cell.

	config = {'foreground': 'black', 'background' : 'white'}
	data = self.data
	testColName  = 'ColumnName'
	testColIndex = data.getColumnIndex(testColName)
	testColList  = data.getColumnAsList(testColIndex)
	testValue    = data.getValueAt(rowIndex, testColName)
	if testColList.count(testValue) > 1:
		config['background'] = 'yellow'
	return config

I’d recommend limiting the highlight it to the specific column, as then you don’t have to run the check for every cell:

	config = {'foreground': 'black', 'background' : 'white'}

	if colName == 'String Column':
		data = self.data
		testColList  = data.getColumnAsList(colIndex)
		if testColList.count(value) > 1:
			config['background'] = 'yellow'

	return config

You could also do this on a propertyChange event. If your table data is coming from a query, add select 0 as duplicate to your query. Then in the property change event

if event.propertyName == 'data':
	table = system.dataset.toPyDataSet(event.source.data)
	uniqueData = []
	for i,row in enumerate(table):
		value = table.getValueAt(i,'yourColumnToTest')
		if value in uniqueData:
			isDuplicate = 1
                        isDuplicate = 0
		table = system.dataset.setValue(table,i,'duplicate',isDuplicate)
	event.source.data = table

This will only update when the table data changes. Then in your configureCell function,

	data = self.data
	if data.getValueAt(rowIndex,'duplicate'):
		return {'background':'yellow'}

You can hide the duplicate column in table customizer. This code only highlights the duplicate values, and not the first instance of the duplicate values


it works.
I found the data.getColumnAslist(ColIndex) don’t have explain in the manual ,
i use print know how it works finally

i understand, i add one column in the sql for duplicate as signal
but this will base on the property change to active, thanks for your help give me another angle to think the issue

The .getColumnAsList() method is particular to the BasicDataset class and its subclasses, not part of the Dataset interface. Fortunately, most datasets in Ignition inherit from BasicDataset, so you aren’t likely to find one where it doesn’t exist.

1 Like

Dear Jordan:
previous you give me feedback on how to check one column whether there have the same data, now the situation is a little difference
you can see the picture, there are 2 lines, my request is if the two column have the same data , the color will become yellow, how to scripting it, thanks a lot.

here is current code:

config = {'foreground': 'black', 'background' : 'white'}
if colName == 'SerNum':	
	data = self.data
	ColName  = 'SerNum'
	ColIndex = data.getColumnIndex(ColName)
	ColList  = data.getColumnAsList(ColIndex)
	Value    = data.getValueAt(rowIndex, ColName)

	if ColList.count(Value) > 1:
		config['background'] = 'yellow'
return config
	config = {'foreground': 'black', 'background' : 'white'}
	data = self.data
	if data.getValueAt(rowIndex, 'yourColOneName') == data.getValueAt(rowIndex, 'yourColTwoName'):
		config['background'] = 'yellow'
	return config

Check for duplicate rows using data in two columns:

	config = {'foreground': 'black', 'background' : 'white'}
	# Column name of primary value
	colName1 = 'ColumnName1'
	# Column name of secondary value
	colName2 = 'ColumnName2'
	if colName == colName1:
		data = self.data

		# Get column indexes
		colIndex1 = data.getColumnIndex(colName1)
		colIndex2 = data.getColumnIndex(colName2)
		# Get column lists
		colList1 = data.getColumnAsList(colIndex1)
		colList2 = data.getColumnAsList(colIndex2)
		if colList1.count(value) > 1:
			# Get all indexes of primary value
			indexList = [index for index, item in enumerate(colList1) if item == value]
			# Create list of secondary values using indexes we just created
			compareList = [colList2[x] for x in indexList]
			# get secondary value
			compareItem = data.getValueAt(rowIndex, colIndex2)
			# Check for duplicates of secondary value
			if compareList.count(compareItem) > 1:
				config['background'] = 'yellow'
	return config
1 Like