Script for Auto save Table

First let me say im not a pro at scripting,
with that said, I have created a button that the operator uses to save a table trend.
we use 2 different popup calendars to choose the begin date and end date of the table trend. which is usually the last 24hrs. 8am day before till 8am today.
here is the script I have for the button.


table = event.source.parent.getComponent(“Table”)
data = table.data
csv = system.dataset.toCSV(data)
file = system.file.saveFile(“Save File Name”, “csv”, “Comma Separated Values”)
if file:
system.file.writeFile(file, csv)


My Question is how to auto save a Table from 8am day before to 8am day now.?
also give it a new saved name each day. (date and time is fine for file name)

I have started a Gateway Event script (scheduled) with the settings set for every day at 8am.
But cant seem to figure out the script to auto save.
this is what I have so far. ( any example scripts or help would be great)


def onScheduledEvent():
“”"
A scheduled script that will execute periodically on the gateway.

"""
##Line 1 can not be edited it is default script##

def onScheduledEvent():

	table = event.source.parent.getComponent("Table")
	data = table.data
	csv = system.dataset.toCSV(data)
	file = system.file.saveFile("Export to excel file name", "csv", "Comma Separated Values")
	if file:
		system.file.writeFile(file, csv)

You wont be able to use event.souce in a gateway script. You’ll need to get the dataset from the script and then you can convert to CSV and save the file to the gateway.

How are you getting the dataset for the table, a history binding, database query, etc… ?

Reports are actually really good for this. If you create a report that displays the data you want, you can schedule it to run every day at 8:00, and set the date parameters like:

Start:

dateArithmetic(now(), -1, "days")

End:

now()

Then configure an action Email, Save File, etc… and include the date range in the file name like this:

{Report.Name}+"("+dateFormat({StartDate},"MM-dd-yyy HH")+" to "+dateFormat({EndDate},"MM-dd-yyy HH")+")"
+ " - "
+ ".csv"