Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
You can extract the prefix from the name of the sixth field.
Copy link to clipboard
Copied
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;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
What does you see in the field when you change the format to None?
Copy link to clipboard
Copied
NaN
Copy link to clipboard
Copied
NaN = Not a Number
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.

