Not able to edit perspective table cells after applying the color transform script

Hello everyone,

I have a perspective table with an event script configured on “onEditCellCommit”.

	#GET EDITED DATA DETAILS
	rowIndex=event.row									# row number of the commit
	colName=event.column								# column name of the commit
	newValue=event.value
	data = self.props.data							# Dataset [86R ? 8C]
	pds = system.dataset.toPyDataSet(data)			# <PyDataset rows:86 cols:8>
	newData = system.dataset.setValue(data, rowIndex,colName,newValue)
	# update property that is binded to the table
	self.view.custom.part.spec.data = newData
	

The table data is also bound to a dataset and I wrote the script below for cell coloring transformation:

	output_json = []
	style_red = {"classes": "CellColor/AboveHigh"}
	style_green = {"classes": "CellColor/Between"}
	style_yellow = {"classes": "CellColor/BelowLow"}
		
	for row in range(value.getRowCount()):
		row_object = {}
		for col in value.getColumnNames():   
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if col == 'process_value':
				if value.getValueAt(row,col)>=value.getValueAt(row, 'low_limit') and value.getValueAt(row,col)<= value.getValueAt(row, 'high_limit'):
					cell_style= style_green
				elif value.getValueAt(row,col)>value.getValueAt(row, 'high_limit'):
					cell_style= style_red
				elif value.getValueAt(row,col)<value.getValueAt(row, 'low_limit'):
					cell_style= style_yellow						
			cell_object['style']=cell_style
			row_object[col]=cell_object				
		output_json.append(row_object)		
	return output_json

The problem is that I am not able to edit the cell once I have the color script on it. It is working without cell coloring but not with that. I get an error:

TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductive automation.ignition.common.Dataset

I am thinking the problem is with the color script as it does not capture the new data to do the transformation on that but not sure how to fix this and would appreciate your help/suggestions in this regard.

Is that the whole thing ?
The only place where you use toPyDataSet() is in the first part:
pds = system.dataset.toPyDataSet(data).
But you don’t seem to be using that pds variable you initialized…

My suggestion to you: comment out that line and see what’s up.

1 Like

You are correct. I am not using pds anywhere else in “onEditCellCommit”. Commented that sentence but I still get that error.

line 12, in runAction
TypeError: setValue(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset

Line 12: newData = system.dataset.setValue(data, rowIndex,colName,newValue)

After trying myself with a dataset, I get the same error. It appears the dataset is actually of type class com.inductiveautomation.ignition.common.JsonDataset.
converting to a pyDataSet amd back with system.dataset.toPyDataSet() and system.dataset.toDataSet() still gives a JsonDataset… don’t ask me why.

BUT, If you convert to a PyDataSet, you can then use setValue() on it, which does return a BasicDataSet.

This is all very confusing. I fail to see why my dataset was a legal argument for toPyDataSet but not for setValue().

Can you try to log/print the result of type(data) ?

edit:
Wait, I tried again, and was actually able to use setValue() on my inital dataset - no idea what I messed up the first time.

1 Like

I tried to set up something that looks like what I think you have:
Made a view with a table, bound it’s props.data property to a query with dataset as return type, then added that script on its onClick event:

data = self.props.data	
data = system.dataset.setValue(data, 0, 'col_name', 21)
self.props.data = data	
self.getSibling("Label").props.text = type(data)

This works as expected. The initial type of the dataset is BasicStreamingDataSet, which works with setValue(), which in turn returns a BasicDataSet.

1 Like

Thank you very much, Pascal. Appreciate the valuable time and information you provided.
Did you use cell coloring after your script? If yes, was it working?

I am able to edit the cell without color, but as soon as I add color transform I get the error when I want to edit the cell. I am thinking the problem is with the cell color transform script not on the edit cell script.

Just for reference, I am writing them here again:

OnEdittCellCommit Script:

	#GET EDITED DATA DETAILS
	#######################################################
	rowIndex=event.row									# row number of the commit
	colName=event.column								# column name of the commit
	newValue=event.value
	data = self.props.data							# Dataset [86R ? 8C]
	pds = system.dataset.toPyDataSet(data)			# <PyDataset rows:86 cols:8>
	newData = system.dataset.setValue(pds, rowIndex,colName,newValue)
	# update property that is binded to the table
	self.view.custom.part.spec.data = newData

CellColorTransform Script on property binding:

	data=[]
	style_red = {"classes": "CellColor/AboveHigh"}
	style_green = {"classes": "CellColor/Between"}
	style_yellow = {"classes": "CellColor/BelowLow"}
		
	for row in range(value.getRowCount()):
		row_object = {}
		for col in value.getColumnNames():   
			cell_object={}
			cell_style={}
			cell_object['value']=value.getValueAt(row,col)
			if col == 'process_value':
				if value.getValueAt(row,col)>=value.getValueAt(row, 'low_limit') and value.getValueAt(row,col)<= value.getValueAt(row, 'high_limit'):
					cell_style= style_green
				elif value.getValueAt(row,col)>value.getValueAt(row, 'high_limit'):
					cell_style= style_red
				elif value.getValueAt(row,col)<value.getValueAt(row, 'low_limit'):
					cell_style= style_yellow						
			cell_object['style']=cell_style
			row_object[col]=cell_object				
		data.append(row_object)		
	return data

No, I only checked the types of the datasets I got at different stages, and which ones I could pass to setValue().
I don’t think the coloring script has anything to do with your issue.
Did you try to get the type of data ?
You can print type(data) to the console with ‘system.perspective.print()’, send it to a custom property of your table, or display it in a label.
This might show you why it’s not working.

1 Like

data = self.props.data

is class .JsonDataset

and

pds = system.dataset.toPyDataSet(data)

is class.script.builtin.DatasetUtilities$PyDataSet

Edit:

I print this again but I got different results for data!!!

	data = self.props.data							# Dataset [86R ? 8C]
	system.perspective.print(type(data))
	
	pds = system.dataset.toPyDataSet(data)			# <PyDataset rows:86 cols:8>
	system.perspective.print(type(pds))
	
	pdsds=system.dataset.toDataSet(pds)
	system.perspective.print(type(pdsds))
  • class com.inductiveautomation.ignition.common.BasicDataset
  • class com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet
  • class com.inductiveautomation.ignition.common.BasicDataset

Those should be legal first arg to setValue()

Can you try trimming your script to the bare minimum ?
Maybe add a button with an onclick event script as such:

data = self.getSibling("table").props.data   #replace table with the name of your actual table
system.dataset.setValue(data, 0, 'name_of_a_column', 42)

Then try clicking it in preview mode ?

1 Like

I do not get any errors, however, I do not see the changes on the table as well!

	data=self.getSibling("specTable").props.data
	system.dataset.setValue(data, 5, 'process_value', 5)
	system.perspective.print('This Works')

That’s normal, datasets are immutable. setValue() returns a new dataset with the modified value.
If you want the change to be reflected on the table, you need to do

self.props.data = system.dataset.setValue(self.props.data, 5, 'process_value', 5)

If you do not get any error there, then there’s something else going on.
Try going back to onEditCellCommit event, start with the bare minimum, and add from that. When you start getting errors, you’ve reached your problem.

1 Like

So @cmallonee has replied to my question in another thread so I am posting his solution here for further refrences:

That solved the issue :slight_smile: Thank you @cmallonee !

2 Likes