Saving excel file from script console and vision



ccc			= ['a', 'b', 'c', 'd', 'g', 'k', 'm']
fruits 		= [['apple', 'banana', 'cherry', 'date', 'grape', 'kiwi', 'mango']]
print ( str(len(ccc)) + ' ' + str(len(fruits)))
datasetOut2 = system.dataset.toDataSet(ccc, fruits)

fileName 	= 'myFile.xls'
ss 			= system.dataset.toExcel(True, [datasetOut2,datasetOut], ['Fruits','tests'])
#system.perspective.download(filename, ss)

filePath = system.file.saveFile(fileName)
if filePath is not None:
	system.file.writeFile(filePath, ss)

This almost works, but my sheet names don't show as Fruits and tests, instead they show as dataset1 and dataset2.

Take a closer look at the arguments:

https://docs.inductiveautomation.com/display/DOC81/system.dataset.toExcel

I would try this:

system.dataset.toExcel(True,[datasetOut2,datasetOut],sheetNames = ['Fruits','tests']

Also, just because it bothers me, you can use print like this:

print len(ccc),' ',len(fruits)
2 Likes

Thanks
That worked.

I set the extension as '.xlsx' and specified sheetNames like you said.

Solved two issues it seems.
The sheets are named correctly now, thanks.
I had been getting a warning the file was possibly corrupted.

I am not very good at reading the arguments and understanding what they mean for me to do.

The problem in this instance is that the nullsEmpty argument is expecting a boolean. Since many things in python can have a Truthy value, the function assumed that your list of sheetNames was actually you setting nullsEmpty true.

Yes Truthy and Falsy is a thing.

As a general rule of thumb, if you look at a function with multiple optional arguments and you want to skip one in the order, then use keyword arguments (e.g. sheetNames=['name']) to insure you're setting the argument you actually want to set.

3 Likes

I expected too much of the optional functions.

Because they are different datatypes, I thought the function detected to datatype to determine the intended use. The flexibility of booleans in python though, have to remember that. Thanks