The error message is quite clear. You’re trying to use a variable named filePath, but it doesn’t exist.
I’ll let you read the script and figure it out (hint: you have an unused variable with a very similar name)
Hi,
I played a littlebit.
I have some values in Excel, but the problem is, every new roll the data of the previous roll got deleted and overwrite with the data from the last roll. So there is only one roll in the Excel.
You need to pass append=True to the call to system.file.writeFile.
edit: If you’re appending data to a file, you might need a different function to generate that data, as you might want not to add the column names again. I think passing showHeaders=False to system.dataset.toCSV might be enough.
If I add the showHeaders, I still have the same problem.
I get the csv file, but still the same. Strange headers + every roll the previous roll is deleted.
So every new roll the previous roll is getting deleted.
Somebody have a clue? With the headers I can life :p.
#set a list of tag paths of all tag values needed in script and read them all at once
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
#system.tag.readBlocking() returns a list of qualified values so this builds a list with just the values
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
#Get the current date, format it and add to the front of the tagValues list
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
#crete the CSV file
fileName = r"D:\TIS_TEST_" + system.date.format(now, "yyMMdd") + ".csv"
csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)
system.file.writeFile(fileName,csv)
#reset the "reset" tag. system.tag.writeBlocking blocks the thread until the write has finished.
system.tag.writeBlocking([str(event.getTagPath())],[0])
So I tried to change: system.file.writeFile(fileName, csv, append=True)
But like that it was not working, instead I did it like Irose: system.file.writeFile(fileName,csv,True `)
I have all data of the rolls, but my lay out is not ok:
Any idea how to make it nice? I mean only one header colum, remove the colums with str, str, D, D and also remove the columns with the #
My script is this:
if not initialChange and newValue.getValue():
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
headerValues = ["Datum","Tijd","LENGTE","GEWICHT"]
now = system.date.now()
tagValues = [system.date.format(now,"dd/MM/yyyy"),system.date.format(now,"HH:mm:ss")] + tagValues
fileName = r"D:\TIS_TEST_" + system.date.format(now, "yyMMdd") + ".csv"
csv = system.dataset.toCSV(system.dataset.toDataSet(headerValues,[tagValues]),True,True,False)
system.file.writeFile(fileName,csv,True)
system.tag.writeBlocking([str(event.getTagPath())],[0])
And as @lrose just said, and I warned you about a few posts above, you need your subsequent calls to not include the headers, otherwise you're appending headers every time.
I was going to recomend using system.dataset.toExcel(), however, I couldn’t get that to append correctly to a file.
Since what you’re working with here isn’t very much data we can pretty easily just build the csv string. We will also only add the headers if the file doesn’t exist already.
tagPaths = ["[Bontexgeo Kft]Rol lengte laatste rol (m)","[Bontexgeo Kft]Gewicht laatste Rol (kg)"]
tagValues = [qValue.value for qValue in system.tag.readBlocking(tagPaths)]
tagValues = [system.date.format(system.date.now(),"dd/MM/yyyy"),system.date.format(system.date.now(),"HH:mm:ss")] + tagValues
#create the csv file
fileName = r"D:\TIS_TEST_" + system.date.format(system.date.now(), "yyMMdd") + ".csv"
csv = ''
#this checks to see if the file exists and if it doesn't adds the headers
if not system.file.fileExists(fileName):
csv = ','.join(["Datum","Tijd","LENGTE","GEWICHT"])
#this adds the actual data to the string.
csv = '\n' + ','.join(tagValues)
system.file.writeFile(fileName,csv,True)
system.tag.writeBlocking([str(event.getTagPath())],[0])