My question is, is there anyway I can save only the selected column to a CSV? I tried using the selectedColumn property but have had no luck so far. If there is documentation on this it would be greatly appreciated as well!
This will work (with slight modification) if the data is a dataset.
# get a dataset. You've already done this.
table = event.source.parent.getComponent('Table')
data = table.data
# Start here.
pyData = system.dataset.toPyDataSet(data)
# Filter to a single row. Change the number from 0 to columnCount-1.
oneCol = [[i[1]] for i in pyData]
newData = system.dataset.toDataSet(['onlyCol'], oneCol)
newCsv = system.dataset.toCSV(newData)
print newCsv
I did try to use this, although is the print supposed to log it to the console? If so, I am not getting any data back once I click the button to perform the script.
I also tried system.perspective.download(fileName, csv) which caused Ignition to crash (not before I saved!)
No worries! I think it was because I tired to add system.perspective.download(fileName, csv).
Adding in the system.perspective.print(newCsv) did pull the columns data, although it only pulled the first column. Is there a way I can make it to pull the selected column?
The appears to work at least as far as the printing step.
# get a dataset. You've already done this.
data = self.getSibling("Table").props.data
# Start here.
pyData = system.dataset.toPyDataSet(data)
selectedColumn = self.getSibling("Table").props.selection.selectedColumn
# Filter to a single row. Change the number from 0 to columnCount-1.
oneCol = [[i[selectedColumn]] for i in pyData]
newData = system.dataset.toDataSet(['onlyCol'], oneCol)
newCsv = system.dataset.toCSV(newData)
system.perspective.print(newCsv)
This one works to download with no crashing (in the designer at least). It also preserves the name of the selected column (your question just beat the answer).
You should probably add error handling for the case where a user hasn’t selected anything at all, but that’s left as an exercise for the reader, at least for now.
# get a dataset. You've already done this.
data = self.getSibling("Table").props.data
# Start here.
pyData = system.dataset.toPyDataSet(data)
selectedColumn = self.getSibling("Table").props.selection.selectedColumn
# Filter to a single row. Change the number from 0 to columnCount-1.
oneCol = [[i[selectedColumn]] for i in pyData]
newData = system.dataset.toDataSet([selectedColumn], oneCol)
newCsv = system.dataset.toCSV(newData)
system.perspective.download('asdf.csv',newCsv)