Filling a table from 2 other tables

Nope…same thing.

In my script I had a space in ‘Position Table’, so make sure it matches what you have.
This one has a couple of print statements in there as well, so we can see in the console what the list values are.

table = event.source.parent.getComponent('PositionTable')
dataIn = table.data
headers = list(dataIn.getColumnNames())
positionList = dataIn.getColumnAsList(0)
print headers
print positionList
dataOut = []
for row in positionList:
	dataOut.append([row, '', 0])
table.data = system.dataset.toDataSet(headers, dataOut)

This is what I got in the output console:

[u’Position’, u’EmployeeID’, u’FirstName’, u’LastName’, u’Shift’, u’Department’]
[Janitor]
09:34:43.824 [AWT-EventQueue-2] ERROR com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter - Error executing script for event: actionPerformed
on component: Button.
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
File “event:actionPerformed”, line 10, in
IndexError: Row 0 doesn’t have the same number of columns as header list.

at org.python.core.Py.IndexError(Py.java:250)
at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.toDataSet(DatasetUtilities.java:489)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
at org.python.core.PyObject.__call__(PyObject.java:404)
at org.python.core.PyObject.__call__(PyObject.java:408)
at org.python.pycode._pyx65.f$0(<event:actionPerformed>:10)
at org.python.pycode._pyx65.call_function(<event:actionPerformed>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:180)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:271)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:57)
at com.sun.proxy.$Proxy32.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at com.inductiveautomation.snap.swing.RibsEventQueue.dispatchEvent(RibsEventQueue.java:95)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last):
File “event:actionPerformed”, line 10, in
IndexError: Row 0 doesn’t have the same number of columns as header list.

... 57 common frames omitted

Doh! :man_facepalming: I had manually padded the row to the length I needed for the example.

Modified to grab the number of headers, and pad the rows accordingly. Now, if you change your columns, it should follow.

# Decalre table component
table = event.source.parent.getComponent('PositionTable')
# Get the data from the table.
dataIn = table.data
# Get the table headers, so we can keep them
headers = list(dataIn.getColumnNames())
# Get the column data holding the positions 
positionList = dataIn.getColumnAsList(0)
# Set how many columns we have to pad
# This is the length of the header list minus 1,
# Because position will take up one.
padding = len(headers)-1
dataOut = []
for row in positionList:
	# Generate data for a new row
	# [element] * x will give a repeating list of x elements.
	# e.g. [''] * 3 will result in ['', '', '']
	# We use this to pad out the row to the same number
	# of columns listed in the header.
	newRow = [row] +  [''] * padding
	# Add new row to dataOut
	dataOut.append(newRow)
# Write dataset back to the table
table.data = system.dataset.toDataSet(headers, dataOut)
1 Like

Ok… for future reference can you explain to me exactly what is taking place with this script?

Edited my post above to add comments.

you can see how list adding and multiplying works by putting this into the script console:

list1 = ['abc'] + ['x'] * 2
print 'list1 = ', list1

list2 = ['abc'] + ['x'] * 3
print 'list2 = ', list2

list3 = ['abc'] + ['x'] * 4
print 'list3 = ', list3

1 Like