Using Tag Values in Strings & Date Formatting

I’m writing a script to export a Report that will run on the value change of a UDT mem tag and I’m having some issues with trying to dynamically adjust the file name of the Report pdf.
I have a Job # (MO) and batch # (control number) stored on int mem tags and then a start date and end date stored on date mem tags. I’m trying to format the dates so that they’re “yyyy-MM-dd” but using system.date.format complains that “1st arg can’t be coerced to java.util.Date” and DateFormat doesn’t seem to exist in any capacity even with imports.
On top of that. I’m having issues substituting the MO and c_num into the string, getting errors saying “int argument required” when those tag values are ints. I was able to get a report generated with just the base string with no substitutions using the variable containing the string as the filename before but now it’s ignoring that string variable, choosing to create the PDF with the default “M-dd-dd hhPM” name.

import system
#if currentValue.value != previousValue.value and initialChange == false:
#Control # report range
batch = system.tag.read("[.]batchStart")
system.tag.write("[.]reportStart", batch)
system.tag.write("[.]reportEnd", system.date.now())
system.tag.write("[.]batchStart", system.date.now())

title = "Zone_Mem MO %i Control# %i Report %s - %s.pdf"
mo = system.tag.read("[.]MO")
c_num = system.tag.read("[.]ControlNum")
reportStart = system.tag.read("[.]reportStart")
rStart = system.date.format(reportStart, "MM-dd-yyyy")	
reportEnd = system.tag.read("[.]reportEnd")
rEnd = system.date.format(reportEnd, "MM-dd-yyyy")	
settings={"path":"C:\Capstone", "filename":title % (mo, c_num, rStart, rEnd), "format":"pdf"}
system.report.executeAndDistribute(path="C-num Report", project="Line5", action="save", actionSettings=settings)

Wherever you read a tag in a script, you get a QualifiedValue object, not the raw value. That explains your “cannot coerce” error, as a QV that contains a date is not itself a date. You need to add a .value to those QVs to get the actual value. Also, consider using .readAll() and .writeAll() when you work with multiple tags.

1 Like

I believe I at lease did the .value with the mo and c_num to test them in the string and it still complained about expecting an int (it’s case sensitive, correct?). I’ll try that with the date formats and see if it will accept them.

So after realizing that when you do a system.tag.read(), it always pulls the QualifiedValue of the tag and doesn’t actually pull the value unless you add a .value after the tag read. Doing that does successfully read the tag value in its proper format and everything seems to execute fine but it’s still ignoring my fileName for the exported Report and saving it as the default “2-17-20 08PM”. I can tell it executes properly because the File Explorer modified date reflects the execution time and just saves over that default fileName.

	import system
	#if currentValue.value != previousValue.value and initialChange == false:
	#Control # report range
	batch = system.tag.read("[.]batchStart")
	report = ["[.]reportStart", "[.]reportEnd","[.]batchStart"]
	adjust = [batch, system.date.now(), system.date.now()]
	adjust = system.tag.writeAll(report, adjust)
	
	title = "Zone_Mem MO %i Control# %i Report %s - %s.pdf"
	range = ["[.]MO.Value", "[.]ControlNum", "[.]reportStart", "[.]reportEnd"]
	stats = system.tag.readAll(range)
	rStart = system.date.format(stats[2].value, "MM-dd-yyyy")	
	rEnd = system.date.format(stats[3].value, "MM-dd-yyyy")	
	settings={"path":"C:\Capstone", "filename":title % (stats[0].value, stats[1].value, rStart, rEnd), "format":"pdf"}
	system.report.executeAndDistribute(path="C-num Report", project="Line5", action="save", actionSettings=settings)

That's because it isn't receiving a fileName key; capitalize "N" in "filename".

settings={"path":"C:\Capstone", "fileName":title % (stats[0].value, stats[1].value, rStart, rEnd), "format":"pdf"}
1 Like

You know, it’s the simple stuff that always throws us for a loop…
I’ll go and fix that and I feel like I’ll be back when it comes time to edit the executeAndDistribute for email delivery but thank you guys for the help.

1 Like

Also, either change your slashes to forward or double them up. A backslash is a python escape character for injecting control codes.

2 Likes

Adding to @pturmel’s note, it looks like you got lucky with your path. \C works as you expected, but a lot of characters after the backslash would not. See examples below–the last two are safe ways of including backslashes, while #3 works using a forward slash instead.

test = "C:\Capstone"
test2 = "C:\test"
test3 = "C:/test"
test4 = "C:\\test"
test5 = r"C:\test"
print test, test2, test3, test4, test5

Output–note \t becomes a tab:

>>> 
C:\Capstone C:	est C:/test C:\test C:\test
>>> 
3 Likes