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.
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.
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.
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.
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
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.