need help with Adobe Acrobat Pro DC custom calculation script

Community Beginner ,
Oct 09, 2017

Copy link to clipboard

Copied

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

    }

})();

TOPICS
Acrobat SDK and JavaScript

Views

399

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

need help with Adobe Acrobat Pro DC custom calculation script

Community Beginner ,
Oct 09, 2017

Copy link to clipboard

Copied

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

    }

})();

TOPICS
Acrobat SDK and JavaScript

Views

400

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 09, 2017 0
Most Valuable Participant ,
Oct 09, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 09, 2017 2
Community Beginner ,
Oct 09, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 09, 2017 0
Most Valuable Participant ,
Oct 10, 2017

Copy link to clipboard

Copied

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;

    }

})();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 10, 2017 2
Community Beginner ,
Oct 10, 2017

Copy link to clipboard

Copied

Thank you so much for your help!

It is throwing an error for a missing     )    in parenthetical 27:Field:Calculate, but I don't see where this error is stemming from.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 10, 2017 0
try67 LATEST
Most Valuable Participant ,
Oct 10, 2017

Copy link to clipboard

Copied

Works fine for me... Did you copy the whole code?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 10, 2017 0