Skip to main content
Participant
October 9, 2017
Answered

need help with Adobe Acrobat Pro DC custom calculation script

  • October 9, 2017
  • 1 reply
  • 1244 views

I'm trying to create a form with:

1. a series of numerators and denominators to calculate

2. if the field is left blank, it is not included in the calculation

3. the calculation to be updated in real time, and not wait for each field to have a number in it before it calculates.

This is the only calculation in the form, so changing the calculation order is not used.

In the document javascript, I have

    this.calculateNow();

and in the custom calculation script for the field, I have

    (function () {

  

    var v1 = +getField("Text1").value;

    var v2 = +getField("Text2").value;

    var v3 = +getField("Text3").value;

    var v4 = +getField("Text4").value;

    var v5 = +getField("Text5").value;

    var v6 = +getField("Text6").value;

    var v7 = +getField("Text7").value;

    var v8 = +getField("Text8").value;

    var v9 = +getField("Text9").value;

    var v10 = +getField("Text10").value;

  

    event.value = (v2 * v4 * v6 * v8 * v10) !== 0 ? ((v1 * v3 * v5 * v7 * v9) / (v2 * v4 * v6 * v8 * v10)) : "";

    })();

The issues I'm having:

1. that the calculation does not calculate in real time

2. all fields still have to be entered for the calculation to occur.

3. if zero is entered, the calculation will not run.

This topic has been closed for replies.
Correct answer try67

Try this code:

(function () {

    event.value = "";

    var v1 = this.getField("Text1").valueAsString;

    var v2 = this.getField("Text2").valueAsString;

    var v3 = this.getField("Text3").valueAsString;

    var v4 = this.getField("Text4").valueAsString;

    var v5 = this.getField("Text5").valueAsString;

    var v6 = this.getField("Text6").valueAsString;

    var v7 = this.getField("Text7").valueAsString;

    var v8 = this.getField("Text8").valueAsString;

    var v9 = this.getField("Text9").valueAsString;

    var v10 = this.getField("Text10").valueAsString;

    if (v2=="" && v4=="" && v6=="" && v8=="" && v10=="") return;

    var x = 1;

    if (v2!="") x*=Number(v2);

    if (v4!="") x*=Number(v4);

    if (v6!="") x*=Number(v6);

    if (v8!="") x*=Number(v8);

    if (v10!="") x*=Number(v10);

    if (x!=0) {

        var y = Number(v1) * Number(v3) * Number(v5) * Number(v7) * Number(v9);

        event.value = y/x;

    }

})();

1 reply

try67
Community Expert
Community Expert
October 9, 2017

You don't need the calculateNow command. The calculation occurs each time the value of any field in the file is changed.

The issue is with your condition. If any of the even fields (2, 4, 6, 8, 10) are empty (or zero), the result will be zero and your field will remain empty.

If you don't want that to happen then you need to better explain how you do want it to work.

drwniccAuthor
Participant
October 9, 2017

Thank you for your reply!

You are correct, I would like the field to calculate even if 4, 6, 8, or 10 are empty - even though they are denominators.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 10, 2017

Try this code:

(function () {

    event.value = "";

    var v1 = this.getField("Text1").valueAsString;

    var v2 = this.getField("Text2").valueAsString;

    var v3 = this.getField("Text3").valueAsString;

    var v4 = this.getField("Text4").valueAsString;

    var v5 = this.getField("Text5").valueAsString;

    var v6 = this.getField("Text6").valueAsString;

    var v7 = this.getField("Text7").valueAsString;

    var v8 = this.getField("Text8").valueAsString;

    var v9 = this.getField("Text9").valueAsString;

    var v10 = this.getField("Text10").valueAsString;

    if (v2=="" && v4=="" && v6=="" && v8=="" && v10=="") return;

    var x = 1;

    if (v2!="") x*=Number(v2);

    if (v4!="") x*=Number(v4);

    if (v6!="") x*=Number(v6);

    if (v8!="") x*=Number(v8);

    if (v10!="") x*=Number(v10);

    if (x!=0) {

        var y = Number(v1) * Number(v3) * Number(v5) * Number(v7) * Number(v9);

        event.value = y/x;

    }

})();