# Save selected column to CSV

**URL:** <https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455>\
**Category:** Ignition\
**Tags:** database, scripting, ignition81, perspective\
**Created:** [August 25, 2022, 2:25pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455 "2022-08-25T14:25:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 2:25pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/1 "2022-08-25T14:25:45Z")

</div>

Hello!

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

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

```

This is my table:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3bc23f39f0083bf9b112ba026b3454b5c34504cd.png)

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!

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [August 25, 2022, 2:34pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/2 "2022-08-25T14:34:48Z")

</div>

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

This will work (with slight modification) if the data is a dataset.

```auto
# 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

```

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/3 "2022-08-25T14:36:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 3:01pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/4 "2022-08-25T15:01:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [August 25, 2022, 3:04pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/5 "2022-08-25T15:04:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 3:09pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/6 "2022-08-25T15:09:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [August 25, 2022, 3:13pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/7 "2022-08-25T15:13:54Z")

</div>

The appears to work at least as far as the printing step.

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 3:20pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/8 "2022-08-25T15:20:57Z")

</div>

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 🙂

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

Thanks again!

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [August 25, 2022, 3:22pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/9 "2022-08-25T15:22:38Z")

</div>

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.

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [August 25, 2022, 3:24pm UTC](https://forum.inductiveautomation.com/t/save-selected-column-to-csv/63455/10 "2022-08-25T15:24:38Z")

</div>

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!
