# Exporting table data to CSV with Button script Event

**URL:** <https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452>\
**Category:** Ignition\
**Tags:** ignition81, perspective, scripting\
**Created:** [February 8, 2024, 5:38pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452 "2024-02-08T17:38:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@Nathan\_Smith](https://forum.inductiveautomation.com/u/Nathan_Smith)\
**Post date:** [February 8, 2024, 5:38pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452/1 "2024-02-08T17:38:17Z")

</div>

I followed this IU tutorial:

[Exporting Data to CSV Video at Inductive University](https://inductiveuniversity.com/video/exporting-data-to-csv/8.1)

to create a button that exports data from a table to a CSV file, but I get an error when I run it. I didn't make any changes to the script compared to what was used in the video, but when I try to use the button, I get this error message:

Error running action 'dom.onClick' on orderStatus/open-sales-orders@D/root/Button: Traceback (most recent call last): File "function:runAction", line 3, in runAction AttributeError: 'com.inductiveautomation.ignition.common.script.ada' object has no attribute 'source'

Here is my code for reference:

```auto
def runAction(self, event):
	#get table data as a dataset
	dataset = event.source.parent.getComponent("Table").data
	
	#convert dataset to multi-line csv string
	csv = system.dataset.toCSV(dataset)
	
	#prompt for desired file location
	file = system.file.saveFile("export1.csv")
	
	#intercept any cancel case, then write to file
	if file:
		system.file.writeFile(file, csv)

```

I assume the "event" object is referening to the "onClick" mouse event, but I do not understand what the ".source" or ".parent" objects/attributes are referring to. What I am wondering is if I made an error somewhere or if this code may no longer be valid and I need to use something else?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 8, 2024, 5:43pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452/2 "2024-02-08T17:43:27Z")

</div>

Any script that uses `event.source` is a script intended for Vision, not Perspective. You cannot cut and paste it into a Perspective action and expect it to work.

The function `system.file.saveFile()` doesn't work at all in Perspective, as Perspective cannot open a file save dialog on a client like that. The `system.file.writeFile()` function writes to the gateway filesystem, not a client filesystem, as Perspective scripts run in the gateway.

The technique needed in Perspective is the use of `system.perspective.download()`. Numerous examples on this forum.

---

<div class="post-metadata">

**Author:** ![Alex\_DeBate](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alex_debate/32/56483_2.png) [@Alex\_DeBate](https://forum.inductiveautomation.com/u/Alex_DeBate)\
**Post date:** [February 8, 2024, 5:47pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452/3 "2024-02-08T17:47:26Z")

</div>

Nathan, here's a script I wrote to download table data into an .xlsx format. That, along with what pturmel wrote, should give you a good understanding of how this is accomplished.

```auto

def runAction(self, event):
	dataset = self.parent.parent.getChild("TabContainer").getChild("Downtime Log").props.data
	download = system.dataset.toExcel(1, dataset)
	date_start = self.getSibling("StartDate").props.value
	date_range = self.getSibling("DateRange").props.value
	if date_range == 1:
		date_end = system.date.addDays(date_start, 1)
	elif date_range == 2:
		date_end = system.date.addWeeks(date_start, 1)
	elif date_range == 3:
		date_end = system.date.addMonths(date_start, 1)
	
	now = system.date.now()
	if date_end > now:
		date_end = now
	
	date_start_formatted = system.date.format(date_start,"yyyy-MM-dd")
	date_end_formatted = system.date.format(date_end,"yyyy-MM-dd")
	system.perspective.download("Downtime Export "+date_start_formatted+" to "+date_end_formatted+".xlsx", download)

```

Granted, this downloads the data in .xlsx format, but the process is very similar for CSV.

---

<div class="post-metadata">

**Author:** ![Nathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@Nathan\_Smith](https://forum.inductiveautomation.com/u/Nathan_Smith)\
**Post date:** [February 8, 2024, 7:42pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452/4 "2024-02-08T19:42:23Z")

</div>

Ah, I didn't know it was a vision script in the video I only have perspective on my license and haven't ever used it. That would makes sense as to why I couldn't find much information in the manual. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Nathan\_Smith](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@Nathan\_Smith](https://forum.inductiveautomation.com/u/Nathan_Smith)\
**Post date:** [February 8, 2024, 7:43pm UTC](https://forum.inductiveautomation.com/t/exporting-table-data-to-csv-with-button-script-event/85452/5 "2024-02-08T19:43:41Z")

</div>

I was able to adapt this script to get it working. Thanks a lot for the example!
