Skip to main content
peanutmom
Participant
February 7, 2018
Answered

Acrobat X Pro calculation problem

  • February 7, 2018
  • 2 replies
  • 832 views

I have an order form which has six items for sale. The first field on each item is the number of items the customer is purchasing. The second field is a hidden field which creates the cost of the item. The third field is simply first field + second field = total cost for that item.  There are another five lines of exactly the same code. The problem comes in with the final Grand Total line. I wasn't able to simply perform a sum function and pick my fields. I had to do a custom calculation.  It works. However, if the person picks something from line one and skips to line three, the grand total turns into a mess.

Examples:

Field One (how many, limited to three characters - a/k/a "order.0.0")

Field Two (hidden - validate value of 75 to 75, or whatever each line costs - a/k/a "cost.0.0")

Field Three (custom calculation script):

var a = this.getField("order.0.0")

if(a.value!=0)

event.value = a.value * 75

else

event.value = ""

Each line is assigned a different variable, and the "a.value" changes to whatever the publication costs. Customer needed the if/else statement to keep the zeroes out of the way if nothing was ordered.

The final "grand total" calculation is done this way:

var a = this.getField("price.0.0");

var b = this.getField("price.1.0");

var c = this.getField("price.2.0");

var d = this.getField("price.3.0");

var e = this.getField("price.4.0");

var f = this.getField("price.5.0");

event.value = a.value + b.value + c.value + d.value + e.value + f.value;

All works fine as long as no fields are skipped. Otherwise, $75.00 + $120.00 ends up being $75,120.00 instead of the actual $195.00. I am not a programmer; is there something else I am missing? Thank you!

This topic has been closed for replies.
Correct answer Thom Parker

That works too

Here's a shorter modified script. With this script you can add any number of order lines and the sum script stays the same because it relies on the group naming.

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=Number(a.value);});

event.value = sum;

2 replies

try67
Community Expert
Community Expert
February 7, 2018

You need to explicitly convert all the values to numbers. Use this code:

var a = Number(this.getField("price.0.0").valueAsString);

var b = Number(this.getField("price.1.0").valueAsString);

var c = Number(this.getField("price.2.0").valueAsString);

var d = Number(this.getField("price.3.0").valueAsString);

var e = Number(this.getField("price.4.0").valueAsString);

var f = Number(this.getField("price.5.0").valueAsString);

event = a + b + c + d + e + f;

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 7, 2018

That works too

Here's a shorter modified script. With this script you can add any number of order lines and the sum script stays the same because it relies on the group naming.

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=Number(a.value);});

event.value = sum;

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
February 7, 2018

This code assumes that those are all of the "price" fields. There might be more, which are not involved in this calculation...

Thom Parker
Community Expert
Community Expert
February 7, 2018

There are a few different ways to get your calculation correct. One method, which I think is the correct way is to allow all the fields to contain real number values, so all the calculations flow nicely with none of these empty field issues. I know you want the 0 value fields to show blank, so to do this use a format script to control the field display.

However, the easiest thing for you to do right now is to fix the calculation so that it tests for blank fields and doesn't add them in.

Here's the script, I noticed you use group naming, so this script will work as long as there are no "price.0.1, price.1.1, etct" fields.

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=a.valueAsString.length?a.value:0;});

event.value = sum;

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often