You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing docs@inductiveautomation.com.
Version:
LESSON LIST
-
5:08Component Event Handlers
-
4:04Script Builders
-
4:23Event Object
-
7:34Accessing Component Properties
-
4:46Finding Components on Other Windows
-
5:01Reading and Writing Tags
-
6:35Navigation Functions
-
6:26Retargeting
-
1:39Working With Different Datatypes
-
5:19Working with Datasets
-
1:53Exporting Data to CSV
-
4:30Component Extension Functions
-
3:29Component Custom Methods
LESSON
Exporting Data to CSV
Description
Learn how to export a dataset from a query or table to a CSV file.
Video recorded using: Ignition 7.7
Transcript
(open in window)[00:00] In this lesson we're going to look at how to export a dataset to a CSV file. It doesn't take much scripting to do this. So in our example script here, we grab our table component, and then we grab it's dataset out of it. Now, this example would work equally well for a dataset that came from anywhere. Datasets are all the same kind of thing, so if you had a system.db.run query, and it returned a dataset, this example would work just as well for that. All we need to do to export it to a CSV file, is first convert it to CSV, which we do via system.dataset.toCSV, which takes the dataset, and returns a string, and that string represents the comma-separated values. And then we use system.file.savefile, and this is just a way to open up a save file prompt box for the user, so that they can pick the location of saving the file. All this does is return a file location, or if they hit cancel, it returns none. So that's what this if block is for. It's for protecting against the case where they hit cancel, so that we don't do anything. But in case they hit okay, we go ahead and use a system.file.writefile, which takes a path to a file, and a string, in this case, our CSV. So let's see this in action. Run this guy. I'll go ahead and save it right there in my documents. And now we can see the file's there, and if we open it up, we'll see the same information that came from our table, right here in Excel.