Save selected column to CSV

Hello!

I have a simple script that saves the table data as a CSV.

	csv = system.dataset.toCSV(self.getSibling("Table").props.data)
	fileName = "test.csv"
	system.perspective.download(fileName, csv)

This is my table:

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!

Thank you!

Is the table data a dataset, json, or other?

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
1 Like

I apologize, yes it is a dataset coming from a named query!

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!)

Thanks!

My bad. You’re in Perspective.

system.perspective.print(newCsv) would probably be more appropriate. Crashing is unexpected. I’ll give it a try and see what happens.

1 Like

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?

Thank you for all the help!

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)
2 Likes

That was it! Thank you so much for your help with this!

I have one last question, feel free to tell me to kick rocks :slight_smile:

Is there any way I can also bring in the header for the column at the top before the data displays?

Thanks again!

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)
2 Likes

Brilliant, thank you once again for all your help on this! I will look into adding in the error handling now!

Have a great rest of your day!