I have been reviewing the forum posts on csv export from ignition and cant seem to find my use case so I figured I would open a topic.
I have a page with a power table and a little dock that pops out on right side to help the user toggle on/off which columns they want on the table. On that dock they also have an export button.
The export button invokes a perspective message called ExportData which is handled in the root of the page.
this is my ExportData handler code.
# root > table-flex > body > table
table = self.getChild("Table Flex").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
# first row of data is headers
data = [colsToKeep]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(data)
system.perspective.download("Export.csv",newCsv)
for some reason when i export to csv i get 2 rows of data.
I was under the impression that if I feed toCSV() a list of lists with first row as header that i would get a csv output?
# root > table-flex > body > table
table = self.getChild("Table Flex").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)
I got a table binded to a Named Query through a Custom dat property:
The name of the table is tblWorkRequest
And I trying to export it as csv and download it using:
def runAction(self, event):
table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)
Unable to run action 'dom.onClick' on Page/AspectorPRL@D/Root Container/TabContainer/Page2/Button_3: code could not be compiled.
org.python.core.PySyntaxError
File "function:runAction", line 5
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
^
SyntaxError: mismatched input '' expecting DEDENT
Ignition v8.1.38 (b2024030513)
Java: Azul Systems, Inc. 17.0.9
def runAction(self, event):
table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)
def runAction(self, event):
table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)
def runAction(self, event):
table = self.getChild("tblWorkRequest").getChild("Body").getChild("Table")
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)
got this error detail:
org.python.core.PySyntaxError
File "function:runAction", line 13
system.perspective.download("Export.csv",newCsv)
^
SyntaxError: no viable alternative at input 'system'
You're missing a closing parenthesis at the end of this line: newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
You need to learn to debug this on your own, this is very basic and we can't keep doing it for you.
And you won't ever learn if you don't try before asking.