Export table to csv file selected row and column

Hello,

I have 2 Table inside my template, Table1 and Table2.
Trying to export the data from both table to one single CSV file.
I am using below scripting. I would like to have Table1 to start from CSV Row 1, Column 1 and Table2 to start from CSV Row 20, Column 5. in the same CSV file. Is that possible ? Thank you

template = event.source.parent.getComponent('Summary_Report')
loadedTemplate = template.loadedTemplate
myTable = loadedTemplate.getComponent("Table1")
data = myTable.data

filePath = system.dataset.exportCSV("data.csv", 1, data)
if filePath != None:
system.net.openURL("file:///"+filePath.replace('\','/'))

No even opened the designer for this haha, hope it works. ds1 is dataset1 and ds2 obviously is dataset2

headers1 = system.dataset.getColumnHeaders(ds1)
headers2 = system.dataset.getColumnHeaders(ds2)
headers = headers1+headers2
rows = []
for r in range(ds1.rowCount):
	row = []
	for c in range(ds1.columnCount):
		row.append(ds1.getValueAt(r,c))
	rows.append(row +[None for i in headers2])
for r in range(ds2.rowCount):
	row = [None for i in headers1]
	for c in range(ds2.columnCount):
		row.append(ds2.getValueAt(r,c))
	rows.append(row)
weirdDataset = system.dataset.toDataSet(headers,rows)

Then change this to

filePath = system.dataset.exportCSV(“data.csv”, 1, weirdDataset)

hi jespinmartin, both my table is not having the same column…will that works?

no you will have to add in the misisng columns on both then (with value null or whatever is appropriate)

hmm, then I still can’t select the row number on csv file isn’t it

rownumber shoudlnt be affected?
https://docs.inductiveautomation.com/display/DOC81/system.dataset.addColumn

here is some pseudo code, doesnt work but you get the idea^^ you ofc should use a loop for the missing columns

#ds1headers[“1”,“2”,“3”]
#ds2headers[“4”,“5”,“6”]

ds1 =system.dataset.addColumn(ds1, 3, [None]*ds1.getRowCount(), "4", type )
ds1 =system.dataset.addColumn(ds1, 4, [None]*ds1.getRowCount(), "5", type )
ds1 =system.dataset.addColumn(ds1, 5, [None]*ds1.getRowCount(), "6", type )
ds2 =system.dataset.addColumn(ds2, 0, [None]*ds2.getRowCount(), "1", type )
ds2 =system.dataset.addColumn(ds2, 1, [None]*ds2.getRowCount(), "2", type )
ds2 =system.dataset.addColumn(ds2, 2, [None]*ds2.getRowCount(), "3", type )

#ds1headerss=[1,2,3,4,5,6]
#ds2headerss=[1,2,3,4,5,6]

the rest of the code should work just fine

It should work, I made an edit, test it yourself

yes sir thanks, your script worked…but if I can have Table 2 to be at (A,10) coordinate or somewhere below the last line of Table 1 then that will be wonderful though…

I’m afraid you were not clear at first, anyways. Here:

ds1 = event.source.parent.getComponent('Table').data
ds2 = event.source.parent.getComponent('Table 1').data
wider = max(len(system.dataset.getColumnHeaders(ds1)),len(system.dataset.getColumnHeaders(ds2)))
narrower = min(len(system.dataset.getColumnHeaders(ds1)),len(system.dataset.getColumnHeaders(ds2)))
largeDataset = ds1 if wider == ds1.columnCount else ds2
smallDataset = ds2 if narrower == ds2.columnCount else ds1
rowsGap = 5
rows = []
for r in range(ds1.rowCount):
	row = []
	for c in range(largeDataset.columnCount):
		try:
			row.append(str(largeDataset.getValueAt(r,c)))
		except:
			row.append(None)
	rows.append(row)
for i in range(rowsGap):
	rows.append([None for j in range(largeDataset.columnCount)])
midHeader = [h for h in system.dataset.getColumnHeaders(smallDataset)]
rows.append(midHeader+[None for k in range(largeDataset.columnCount-smallDataset.columnCount)])
for r in range(ds2.rowCount):
	row = []
	for c in range(largeDataset.columnCount):
		try:
			row.append(str(smallDataset.getValueAt(r,c)))
		except:
			row.append(None)
	rows.append(row)
weirdDataset = system.dataset.toDataSet(system.dataset.getColumnHeaders(largeDataset), rows)

1 Like