Exporting table data to CSV with Button script Event

I followed this IU tutorial:

Exporting Data to CSV Video at Inductive University

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:

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?

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.

5 Likes

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.


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.

5 Likes

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.

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

1 Like