Create CSV on button push with custom Strings

Good morning! Hopefully someone can assist. I have need for when a button is pressed to run a custom script that will create a CSV file in a network location that reads from an existing power table, pulls only certain information from the selected row in the power table, and write that information along with other information into a CSV.

We use a continuous transaction processor here that when a CSV file is dropped into a location, it will then log that specific information into our ERP. It has to be in a certain format so I need the CSV to read like “,(PartNumber),(qty),(date)” which can be found in the power table, but then I have some hard coded information as well that needs to be in the CSV for the ERP. So, I need to be able to modify the string in the csv file to put that information in as well. I mostly need the button to when pressed to get just specific information from the power table on the row selected and put it in the declared spots of the string to create the CSV, then to have Ignition generate the CSV. Any help would be greatly appreciated as I know zero about scripting.

Good morning!

I would recommend starting with this introduction to Scripting in the documentation

Then, here's a good resource describing how to import and export CSVs

It covers how to get a dataset from a Power Table then write it to the CSV. Since you only want certain columns, check out this link on how to work with datasets.

Here's the general layout of what your script will probably resemble:

# Get the dataset from the Power Table
initialDataset = event.source.parent.getComponent('Power Table').data

# Do your dataset manipulation
# newDataset = ?

# Convert the new dataset to CSV format and write to a network location
csv = system.dataset.toCSV(newDataset)
filePath = "YourNetworkLocation" + "YourDesiredFilename.csv"
system.file.writeFile(filePath, csv)
4 Likes

Awesome! Thank you so much for the guidance I greatly appreciate it!

[quote=“ethomason, post:2, topic:19635”]

O.K. so one of my columns in my power table is titled “Lot”, when I put the following in the script:

Get the dataset from the Power Table
initialDataset = event.source.parent.getComponent('Power Table').data

 Do your dataset manipulation
newDataset = ({event.source.parent.getComponent('Power Table').selectedRow}"Lot"}+','+'Testing')

Convert the new dataset to CSV format and write to a network location
csv = system.dataset.toCSV(newDataset)
filePath = "C" + "results.csv"
system.file.writeFile(filePath, csv)

This is what I get:

I’m positive I’m doing it wrong, I’m just not sure how to correct it.

What are you trying to do with the column? Delete it?

I’m trying to read from the selected row, in the column titled “lot” and take that data, write it into a CSV file along with some other commentary and drop it in a network location.

You’ll need to loop through the initialDataset and pull the columns you want into the newDataset. Something like this:

initialDataset = event.source.parent.getComponent('Power Table').data

header = ['Lot']
newDataset = system.dataset.toDataSet(header, [])

for row in range(initialDataset.getRowCount()):
	newDataset = system.dataset.addRow(newDataset, [initialDataset.getValueAt(row, 'Lot')])

csv = system.dataset.toCSV(newDataset)
filePath = "C" + "results.csv"
system.file.writeFile(filePath, csv)

I see curly braces. In python, curly braces create dictionaries. You appear to be mixing binding syntax into your python. Don't do that. (-:

1 Like