Making Document Custom Calculation Script
Copy link to clipboard
Copied
I have a fillable PDF that is working how I want, but I'm wondering if I can make a document javascript that makes it easier for me?
The forumla looks like this,
(MHPF1)*((InstalledRow1*.6)+(GlazedRow1*.3)+(CaulkedRow1*.1)) = Completed Man Hours
Instead of writing this for every Completed Man Hours row, can I make a document javascript to complete this?
I attached the PDF for some help.
How would I make it do this for each row?
Copy link to clipboard
Copied
Your formula is backwards: We use the "thing" that needs to be calculated on the left side:
completedManHours = (MHPF1)*((InstalledRow1*.6)+(GlazedRow1*.3)+(CaulkedRow1*.1));
... this just in case you want to convert this to JavaScript.
There are two ways you can approach this: With a global calculation script that is triggered by one of your fields. I usually use a hidden and read-only field for that, so that it does not interfere with the normal form operations. You would use a loop that loops over all your rows. You would use a JavaScript "for loop" for this. The trick now is to add a variable part to your script:
for (var i = 0; i < numberOfRows; i++) {
this.getField("calculatedManHours" + i).value =
this.getField("MHPF" + i).value * (
(this.getFeld("InstalledRow" + i).value * 0.6) +
(this.getField("GlazedRow" + i).value * 0.3) +
(this.getField("CaulkedRow" + i).value * 0.1)
);
}
You would have to define the variable numberOfRows for this to run.
Another option is to call your calculation function from each field that needs to be calculated. This does require that you touch every one of these fields, but you can do this with a script as well: You can set scripts for fields using the field.setAction() method:

