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

Javascript help for calculating average in fields for a spawned template

Community Beginner ,
Aug 18, 2023 Aug 18, 2023

Hello,

 

I've been searching this and other discussions about a solution for my problem. I've created a PDF document that has nine different templates embedded.  On one template called 'Pellet', there are five fields that I'm looking to calculate an average (to output to a sixth field), they are called:

 

"Test Fire 1", "Test Fire 2", "Test Fire 3", "Test Fire 4" and "Test Fire 5" to output an average to "Test Fire Average".

 

Depending on where the user wants to input that particular page, the above noted fields will spawn with the prefix "P10.Pellet" (In this example P10 is for page 10) such as:

"P10.Pellet.Test Fire 1","P10.Pellet.Test Fire 2", etc...

 

Depending on how many different test fire pages that the user would need to create, there may be more than one 'Pellet' template spawned on one PDF document that will have their own specific page prefix.

 

I initially created an average in the 'Calculate' section in the 'Text Field Properties' on the template, however, once its spawned, the new prefixes mess it up.

 

I've searched and tried examples reading through Thom Parker's posts and I'm at a frustrating loss at the moment and any help would be greatly appreciated.


In short, I'm looking for some help coding a script for calculating an average of five fields on that specific page to output to a sixth field on that same page.

TOPICS
How to , JavaScript , PDF , PDF forms
1.5K
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
1 ACCEPTED SOLUTION
Community Expert ,
Aug 21, 2023 Aug 21, 2023

Thanks Try67 for catching that:

Here's the corrected code

var nSum = 0, nMax = 5;
var cPrefix = event.targetName.replace(/[\w\s]*$/,"");

for(var i=1;i<=nMax;i++)
{
    nSum += Number(this.getField(cPrefix + "Test Fire " + i).valueAsString);
}
event.value = nSum/nMax;

 

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

View solution in original post

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 ,
Aug 18, 2023 Aug 18, 2023

You can extract the prefix from the name of the sixth field.

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 ,
Aug 18, 2023 Aug 18, 2023

As Bernd says, if the field where the calculating is taking place will have the same prefix as the other fields. Assuming there are no dots, ".",  in the root field name. This script will exatract the prefix, including the last ".".

 

var cPrefix = event.targetName.replace(/[\w\s]*$/,"");

 

Then an average script would look like this

 

var nSum = 0, nMax = 5;
var cPrefix = event.targetName.replace(/[\w\s]*$/,"");

for(var i=1;i<=nMax;i++)
{
    nSum += this.getField(cPrefix + "Test Fire " + i);
}
event.value = nSum/nMax;

 

 

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
Community Beginner ,
Aug 21, 2023 Aug 21, 2023

Thank you all for the replies and your patience.

I've inputted the script in the sixth field under Text Field Properties > Calculate > Custom calculation script. Regardless of what number is inputted in any of the five "Test Fire" fields, I get the error "The value entered does not match the format of the field [ P1.Pellet.Test Fire Average ]".

I've ensured that all six field formats are listed as 'Number' with the same values in the 'Number Options' portion of the 'Format' tab.

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 ,
Aug 21, 2023 Aug 21, 2023

What does you see in the field when you change the format to None?

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 Beginner ,
Aug 21, 2023 Aug 21, 2023

NaN

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 ,
Aug 21, 2023 Aug 21, 2023

NaN = Not a Number

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 ,
Aug 21, 2023 Aug 21, 2023

There's a small mistake in Thom's code that's causing this issue. This line:

nSum += this.getField(cPrefix + "Test Fire " + i);

Should be changed to:

nSum += Number(this.getField(cPrefix + "Test Fire " + i).valueAsString);

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 ,
Aug 21, 2023 Aug 21, 2023

Thanks Try67 for catching that:

Here's the corrected code

var nSum = 0, nMax = 5;
var cPrefix = event.targetName.replace(/[\w\s]*$/,"");

for(var i=1;i<=nMax;i++)
{
    nSum += Number(this.getField(cPrefix + "Test Fire " + i).valueAsString);
}
event.value = nSum/nMax;

 

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
Community Expert ,
Aug 21, 2023 Aug 21, 2023

One more thing to note about this code: It will handle empty fields as if they were zero. So if the values are "1", "3", "", "4", "" the result will be 1.6 (8/5), not 2.66 (8/3)... Not sure if that's how you want it to work. If not, the code will need to be adjusted to check for empty values, and setting the value of nMax accordingly.

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 Beginner ,
Aug 22, 2023 Aug 22, 2023
LATEST

Thank you all so very much!  

 

It works as we need it to.  Thank you for pointing out about how the fields are calculated.  Thankfully, we require values greater than zero in all five fields.

 

Again, I sincerely appreciate the quick responses to solve this issue for me.

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