the Custom Calculation Script

New Here ,
Dec 28, 2017

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

494

Likes

Translate

Translate

Report

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

1 Correct Answer

Most Valuable Participant , Dec 28, 2017
try67 Most Valuable Participant , Dec 28, 2017
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.floo...

Likes

Translate

Translate
Jump to answer Jump to answer
Most Valuable Participant ,
Dec 28, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Hi

For now, lets just call the text boxes "text1"  thru "text5" then I can modify the names later to suit

Some other ranges and calculations on the form have only 4 text boxes to average, but I assume this would be fairly easy to modify once I have a good staring point.

All fields are blank until data is entered but all fields are mandatory.  I guess the calculation script could include error code to manage any divide by zero error on opening the form or saving between data input points.

There are no zeros, 1 thru 5 are the values to input and must be entered in each text box by the data input user.

Hope this helps

Thanks

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

When the form is reset, though, the fields will become blank (unless you set a different default value), so you must take this scenario into consideration. I will have it ignore those empty values, for the moment.

You can use this code as your custom calculation script:

var total = 0;

var n = 0;

for (var i=1; i<=5; i++) {

    var f = this.getField("text"+i);

    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);

}

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
johnl62149758 AUTHOR LATEST
New Here ,
Dec 28, 2017

Copy link to clipboard

Copied

Perfect, thank you so much

Likes

Translate

Translate

Report

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