Skip to main content
Participant
March 22, 2017
Answered

Custom Calculation Script for PDF Form

  • March 22, 2017
  • 2 replies
  • 13913 views

Hello,

I have no javascript experience at all; however, I learned by searching on the internet that I need to create a Custom Calculation Script on my .pdf form to get it to do what I want. I researched and attempted it myself and was unsuccessful.

I have four fields created that are named:

  • NumofCriteriaMet
  • TotalNumofCriteria
  • PercentofCriteriaMet
  • CPRatingScore

Currently, I have a simplified field notation (division) in the PercentofCriteriaMet as follows: NumofCriteriaMet/TotalNumofCriteria

and it is working.

What I am trying to do is take the number auto-populated from PercentofCriteriaMet and assign a value based on the range below and have the value auto-populated in CPRatingScore

0.0 to 0.59 = 0

0.60 - 0.89 = 1

0.90 - 1.00 = 2

Also, I don't know if it makes a difference, but I currently have the PercentofCriteriaMet and CPRatingScore field set to "read only"

If anyone can assist in helping me create a working Custom calculation script, it would be much appreciated.

Shelly

This topic has been closed for replies.
Correct answer try67

As the custom calculation script of CPRatingScore enter this code:

var percCriteriaMet = Number(this.getField("PercentofCriteriaMet").value);

if (percCriteriaMet>=0 && percCriteriaMet<=0.59) event.value = 0;

else if (percCriteriaMet<=0.89) event.value = 1;

else if (percCriteriaMet<=1) event.value = 2;

else event.value = "";

It doesn't matter that the fields are read-only. However, what does matter is the calculation order. You need to make set it so that CPRatingScore is calculated after PercentofCriteriaMet.

Also, you might have problems with the calculation you created for PercentofCriteriaMet. When the divisor field (TotalNumofCriteria) is empty or equals zero that will cause an error, as division by zero is not allowed. To avoid that you would need to use a custom calculation script that only applies the formula if the divisor is not zero. That code could look something like this:

var v1 = Number(this.getField("NumofCriteriaMet").value);

var v2 = Number(this.getField("TotalNumofCriteria").value);

if (v2==0) event.value = "";

else event.value = v1/v2;

2 replies

Participant
November 2, 2017

Hey everyone,  I don't write scripts in a PDF docs, however, I need a quick solution for a document I have.

    As it is layed out of the PDF document

      box 1a         box 1b        box 2a        box 2b         box 3a        box3b           4c

   ($amount * # of days) + ($amount * # of days) + ($amount * # of days) = total cost

Thanks

try67
Community Expert
Community Expert
November 2, 2017

Use this code as the custom calculation script of "4c":

var v1 = Number(this.getField("box 1a").valueAsString);

var v2 = Number(this.getField("box 1b").valueAsString);

var v3 = Number(this.getField("box 2a").valueAsString);

var v4 = Number(this.getField("box 2b").valueAsString);

var v5 = Number(this.getField("box 3a").valueAsString);

var v6 = Number(this.getField("box 3b").valueAsString);

event.value = (v1*v2)+(v3*v4)+(v5*v6);

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 22, 2017

As the custom calculation script of CPRatingScore enter this code:

var percCriteriaMet = Number(this.getField("PercentofCriteriaMet").value);

if (percCriteriaMet>=0 && percCriteriaMet<=0.59) event.value = 0;

else if (percCriteriaMet<=0.89) event.value = 1;

else if (percCriteriaMet<=1) event.value = 2;

else event.value = "";

It doesn't matter that the fields are read-only. However, what does matter is the calculation order. You need to make set it so that CPRatingScore is calculated after PercentofCriteriaMet.

Also, you might have problems with the calculation you created for PercentofCriteriaMet. When the divisor field (TotalNumofCriteria) is empty or equals zero that will cause an error, as division by zero is not allowed. To avoid that you would need to use a custom calculation script that only applies the formula if the divisor is not zero. That code could look something like this:

var v1 = Number(this.getField("NumofCriteriaMet").value);

var v2 = Number(this.getField("TotalNumofCriteria").value);

if (v2==0) event.value = "";

else event.value = v1/v2;

shelly109Author
Participant
March 23, 2017

Good morning try67.

Thank you for your help! I plugged the code into the CPRatingScore field's custom calculation script along with the code you provided for the PercentofCriteriaMet field and it is somewhat working.

The code for PercentofCriteriaMet is working perfectly; however the value in the CPRatingScore field does not update properly.  For example, I inputted a value of "8" in NumofCriteriaMet and a value of "12" in TotalNumofCriteria which auto-populated a percent of 66.67% in PercentofCriteriaMet, but the CPRatingScore value did not change to "1" like it should.  However, when I changed the NumofCriteriaMet value to another number, "10", the CPRatingScore changed to the correct value. When I changed the NumofCriteriaMet value a third time, the CPRatingScore failed to update again.

Attempt #Field: NumofCriteriaMetField: TotalNumofCriteriaField: PercentofCriteriaMet (auto-filled)

Field:

CPRatingScore (auto-filled)

181266.67%0 (incorrect)
2101283.33%1 (correct)
31128.33%1 (incorrect)

Is this the calculation order problem you mentioned? If so, how do I correct it?

Thanks again for your help.

Shelly

try67
Community Expert
Community Expert
March 23, 2017

What version of Acrobat do you use?