Skip to main content
Participating Frequently
December 28, 2017
Answered

the Custom Calculation Script

  • December 28, 2017
  • 1 reply
  • 1336 views

hi All,

I have no experience in creating calculation scripts in Adobe Acrobat, but now having made a transition to a paperless office, I need to create some new documents that have java calculation scripts within.

Initially the documents were in excel format and had Sum calculations at the bottom of each range.

i.e.

A1:A5 have single numbers from 1 thru 5

A6 was a sum of A1:A5 but with a twist!

The sum only rounded up if the sum was greater than .6 (point 6)

and rounded down if the sum was less than .6 (point 6)

My original excel calculation is shown below if it helps

=IFERROR(IF(MOD(AVERAGE(A1:A5),1)>0.5,ROUNDUP(AVERAGE(A1:A5),0),ROUNDDOWN(AVERAGE(A1:A5),0)),0)

I assume this will use the Adobe text box names a references to calculate a sum, rather than a cell reference, but I am a little lost.

Can anyone assist in creating a java calculation to achieve the same result?

Product is Adobe Acrobat Student Teacher Edition 2017, but this is only installed on a few systems, Adobe Reader DC is the main application form filling forms.

Thanks

This topic has been closed for replies.
Correct answer try67

Hi ,

Appreciate your help here.

Although I said I may be able to modify based on text box names later, I look at the script and think not.

I bow to your knowledge and apologise for my flippant comment.

I now need a little more help to implement this.

The first range of text boxes are currently named Text7, Text8, Text9, Text10, Text11

With this added, I "may" be able to modify for further use, when I see where the reference is held within the script.

Thanks


Here, I've simplified it for you... If you want to edit the fields list it's just the first line of the code:

var fields = ["Text7", "Text8", "Text9", "Text10", "Text11"];

var total = 0;

var n = 0;

for (var i in fields) {

    var f = this.getField(fields);

    var v = Number(f.valueAsString);

    if (v==0) continue;

    total+=v;

    n++;

}

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

else {

    var avg = total/n;

    var mod = avg-Math.floor(avg);

    if (mod>=0.6) event.value = Math.ceil(avg);

    else event.value = Math.floor(avg);

}

1 reply

try67
Community Expert
Community Expert
December 28, 2017

Is it a sum or an average? Your Excel formula seems to do the latter, but you wrote you're doing the former...

Participating Frequently
December 28, 2017

Thanks for the reply,

Sorry for the typo.  As you pointed out, this is an average calculation which I am trying to figure out.

I am so used to using sum/Average in calculations I messed up my explanation.

Thanks for the clarification

try67
Community Expert
Community Expert
December 28, 2017

OK. So, what are the names of the fields in your PDF file? Do you want to include empty values in the result? How about zero values?