Skip to main content
February 9, 2020
Question

Automatically increment date in spawned page from date field in previous page

  • February 9, 2020
  • 1 reply
  • 1501 views

I have generated a staff timesheet with a button that will spawn a each new page at page 1 (making it easier to find the current week). The previous week will then become page 2 and so forth. I'm happy that the hours worked, charge codes copy across to the new spawned page and values can be unique to other sheet fields.

The trouble I'm having is, I need the date in SunRow1 on the previous page (page 2) to increment by 1 day in the new spawned page (page 1) field named WeekBeginning1. So effectively Im trying to have a new week at the top of the document with an auto-populated date incremented from the sunday of the next sheet down. Then the fields MondayRow1 to equal Weekbeginning1, +1 TuesdayRow1, +1 WednesdayRow1, +1 ThursdayRow1, +1 FridayRow1, +1 SatRow1, +1 SunRow1 on the new sheet. 

 

Script for spawn button is as follows:

// JavaScript to create duplicate pages from templates P1

var num_copies = 1; // Set the number of copies here

var T1 = getTemplate("Timesheet"); // Use the actual tempate name here

if (num_copies > 0) {

var oXObjT1 = T1.spawn({nPage:this.numPages-1, bRename: true, bOverlay: false});

if (num_copies > 1) {

for (var i = 1; i < num_copies; i += 1) {

T1.spawn({nPage:this.numPages-1, bRename: true, bOverlay: false, oXObject: oXObjT1});


}

}

}

Im guessing unique script needs to apply in the spawn script for the day fields above . Im at the extent of my limited knowledge of javascript.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 10, 2020

So you've done great so far. But you've got a complex situation here, and there are more issuse than you've indicated. The script you've posted adds new pages to the end of the file, not the beginning, and its setup to add muplitple copies, which complicates thing more. So, you haven't yet discovered the issues with spawning templates in the same page position 😉    

 

Getting to the functionality that you've specified is going to take some scripting skills. I'm not getting the impression that you have much programming experience. Are you willing to spend some effort to come up to speed on what you need to know to do this? 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
February 11, 2020

Hi Thom,

So I've adjusted that spawn script to the following and it looks like its working (spawning to first pdf page)

var T1 = getTemplate("Timesheet"); // Use the actual tempate name here

T1.spawn({nPage:this.numPages-1, bRename: true, bOverlay: false})

 

I have also figured out how to adjust template to calculate fields within spawned pages using the below.

For example, this for each Tuesdays spawned field

var prefix = "";
var nameParts = event.target.name.split(".");
if (nameParts.length > 2) {
prefix = nameParts[0] + "." + nameParts[1] + ".";
}

var sDate = this.getField(prefix + "MondayRow1").value; // get date string
var oDate = util.scand("dd/mmm/yy", sDate); // convert to object
oDate.setDate(oDate.getDate() + 1); // add 1 days to date
event.value = util.printd("dd/mmm/yy", oDate); // format result

 

But I am still not able to work out how to get the date in SunRow1 on the previous weeks page (page 2) to increment by 1 day in each new spawned page (page 1) field named WeekBeginning1. Can you please help me on this one?

 

Thom Parker
Community Expert
Community Expert
February 13, 2020

So var oLastField = this.getField("P"+(this.numPages-1).toString()+".Timesheet.SunRow1"); is to be written into the spawn script and not the template field WeekBegining1? If so I'm sturggling with the complete composure of the spawn action script.

 

Should my template page be hidden to avoid such errors with "P"+(this.numPages-1).toString()+".Timesheet.SunRow1" as the template does not have the P#.Timesheet prefix.


Yes, in general template pages should always be hidden.

The script should follow this pattern.

 

1. Get last date field

2. Spawn template to last page.

3. Increment date

4. Add to new date field on last page.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often