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

Templates with field calculations

Community Beginner ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

I'm getting closer but I still need some help puzzling this out.

 

Big Picture:

1) I have created a pdf form for a data sheet that contains 7 separate sections (entire form is 11 pages long).

2) The form has a header which is filled out on the first page and then carried through to all the other pages.

3) Some scenarios will require multiple copies of section E (page 7) and/or section F (pages 8-9)

4) All three pages have fields with dynamic calculations, which reference other fields on the same page (but not on other pages).

5) When the form is reset, the spawned sections E and F needed to be deleted, but not the original E and F.

 

What I have done so far:

1) Created duplicates of pages 7-9 to be the hidden templates, and renamed the fields slightly so that they aren't tied to the original pages.

2) Created a template page that just contains the header information without renaming the fields so that they are filled in from the original header page.

3) Created a reset button that deletes everything after page 11 using this script:

this.deletePages(11, this.numPages-1)

 

Where I am now:

Trying to puzzle out how to write the scripts in the calculated fields on the template so that they will account for the renamed fields once spawned. I am aware of the naming pattern: PXX.templateName.fieldName

 

I found this script for aquiring the prefix:

var cPreFix = "";

// get prefix elements for spawned pages;

var aFieldName = event.target.name.split(".");

if(aFieldName.length > 2)

{

// first 2 elements of name array makeup the prefix for fields on a spawned page;

cPreFix = aFieldName[0] + "." + aFieldName[1] + ".";

}

 

I'm assuming it makes the most sense for that to be a document level script (is that the same as a global variable?). So then I just write my calculation scripts using:

this.getField(cPreFix + "myField")

 

So if I have three fields named: F4a.MicroFeat.Tot.0, F4a.MicroFeat.Tot.1, F4a.MicroFeat.Tot.2

and then a field named F4b.MicroFeat.Avg which returns the average of those fields. This is my calculation on the template:

var a = this.getField(cPreFix + "F4a.MicroFeat.Tot.0");

var b = this.getField(cPreFix + "F4a.MicroFeat.Tot.0");

var c = this.getField(cPreFix + "F4a.MicroFeat.Tot.0");

event.target.value = (a.value + b.value + c.value)/3

Am I on the right track or have I gone astray somewhere?

 

TOPICS
How to , JavaScript , PDF forms

Views

468

Translate

Translate

Report

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

You can determine the prefix in a document level function. When you need the prefix you can use this function.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

So I put this in as a document level function, does this look correct?

 

function PreFix()

// get prefix elements for spawned pages;
{
var aFieldName = event.target.name.split(".");
if(aFieldName.length > 2)

// first 2 elements of name array makeup the prefix for fields on a spawned page;
PreFix = aFieldName[0] + "." + aFieldName[1] + ".";
}

Votes

Translate

Translate

Report

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 ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

At the end of the function add:

return PreFix;

 

Votes

Translate

Translate

Report

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 Beginner ,
Apr 19, 2021 Apr 19, 2021

Copy link to clipboard

Copied

So then this is my calculation script:

 

var a = this.getField(PreFix + "F4a.MicroFeat.Tot.0");

var b = this.getField(PreFix + "F4a.MicroFeat.Tot.0");

var c = this.getField(PreFix + "F4a.MicroFeat.Tot.0");

event.target.value = (a.value + b.value + c.value)/3

 

Or do I need to add

PreFix();

Votes

Translate

Translate

Report

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
New Here ,
Jul 07, 2022 Jul 07, 2022

Copy link to clipboard

Copied

LATEST

Hi, I am working a PDF that needs the exact same functions. Did you figure this out?

 

Votes

Translate

Translate

Report

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