Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guest
Feb 08, 2020 Feb 08, 2020

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.

TOPICS
PDF forms
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 10, 2020 Feb 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 10, 2020 Feb 10, 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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2020 Feb 11, 2020

There a few different ways to identify the previous date and/or field on the previously spawned page. 

1) Search all the "SunRow1" fields to find the latest date

2) Use the page number of the current spawn to find the previous spawn

3) Save the the date from the latest spawn in a common form field.   

4) Save the the date from the latest spawn in a global variable.

 

I like #1 the best because it only depends on determining the latest date. 

So, there is a predictable pattern to how form fields on templates pages are renamed.  

P<page#>.<template name>.<field name>

 

This gives us a predictable way to search for fields on other templates.

 

for(var pg=0;pg<this.numPages;pg++)

 {

     if(oFld = this.getField("P"+pg+".Timesheet.SunRow1")

        ... Get and compare dates ...

}

 

Pretty easy isn't it 😉

 

Now about your spawn code.

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

 

this.numPages-1 references the last page in the PDF. So the newly spawned template is placed before this page, i.e. the second to last page in the PDF. This works as far as consecutively naming the fields, which is necessary to get unique field names. You see, if you were to use this code

T1.spawn({nPage:0, bRename: true, bOverlay: false})

 

all the new pages would be the first in the PDF, but they would all have the same field names.

 

You'll find a tutorial and example on using templates here:

https://www.pdfscripting.com/public/programs/downloadsearch.cfm?keywords=template&searchtype=keyword...

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 12, 2020 Feb 12, 2020

Really don't know where to start with "... Get and compare dates ..."

From what I understand so far...

for(var pg=0;pg<this.numPages;pg++)  //Check every page incrementally

 {

     if(oFld = this.getField("P"+pg+".Timesheet.SunRow1")  //for value in this field irrespective of spawn number

But it is the script to check dates I can't really work out. Am I looking at a maxDate function?

 

Have also adjsuted spawn for new weeks to spawn at the bottom. Have included an additonal button action script to go to last page (current week)

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2020 Feb 12, 2020

If you always spawn to the bottom of the PDF, and no pages are deleted, then you can use the last page number to get the field, no comparisons necessary.

(this code is for before the spawn) 

var oLastField = this.getField("P"+(this.numPages-1).toString()+".Timesheet.SunRow1");

 

Also for the spawn, the next page is "this.numPages". So this is all you need to add the template to the end of the PDF.

 

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

 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 12, 2020 Feb 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2020 Feb 13, 2020
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines