I’m trying to write one column from a dataset tag to a new dataset tag so I can link the tag to my template I will be creating. The dataset tag I am pulling from currently has 6 columns however I only want information from 1 column. I tried using the system.tag.read(“first dataset tag”).value.getValueAt(1,"Title of the Column) and was able to get the first row of information but I would like to obtain all the rows for this column. I don’t know how to specify that I want all the rows on the dataset tag rather than a specific row. I then plan on writing the information to my new dataset tag and was wondering what the best method would be.
I wrote this one a while back for a rainy day. This one looks convoluted, but it will work with both BasicDataSets and PyDataSets, and can return either type as its output.
def colToDataSet(dataset, colName, returnPyDataSet = False):
# Check to see if input really is a dataset, then create data appropriately.
if str(type(dataset)) == "<type 'com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet'>":
data = [[row[colName]] for row in dataset]
elif str(type(dataset)) == "<type 'com.inductiveautomation.ignition.common.BasicDataset'>":
colData = dataset.getColumnAsList(dataset.getColumnIndex(colName))
data = [[item] for item in colData]
raise Exception("Input is not a dataset")
return system.dataset.toPyDataSet(system.dataset.toDataSet([colName], data))
return system.dataset.toDataSet([colName], data)
tagIn = '[default]path/to/source/tag'
tagOut = '[default]path/to/destination/tag'
columnName = 'Column Name'
dataIn = system.tag.read(tagIn).value
system.tag.write(tagOut, colToDataSet(dataIn, columnName))
To expand a bit on Jordan’s answer, as you found out, getValueAt only returns a single cell. You need a function that will return an entire column.
If you want a simpler one that works with a tag, you can try this:
# read the full dataset from the tag
data = system.tag.read("[default]MyDatasetTag").value
# extract only the column we want
smallData = system.dataset.filterColumns(data, ["Col 2"])
# display it somewhere so we know it worked
event.source.parent.getComponent('Table 1').data = smallData