8 Replies Latest reply on Dec 28, 2017 5:46 AM by johnl62149758

    the Custom Calculation Script

    johnl62149758 Level 1

      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

        • 1. Re: the Custom Calculation Script
          try67 MVP & Adobe Community Professional

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

          • 2. Re: the Custom Calculation Script
            johnl62149758 Level 1

            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

            • 3. Re: the Custom Calculation Script
              try67 MVP & Adobe Community Professional

              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?

              • 4. Re: the Custom Calculation Script
                johnl62149758 Level 1

                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

                • 5. Re: the Custom Calculation Script
                  try67 MVP & Adobe Community Professional

                  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);
                  }
                  
                  • 6. Re: the Custom Calculation Script
                    johnl62149758 Level 1

                    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

                    • 7. Re: the Custom Calculation Script
                      try67 MVP & Adobe Community Professional

                      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[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);
                      }
                      
                      1 person found this helpful
                      • 8. Re: the Custom Calculation Script
                        johnl62149758 Level 1

                        Perfect, thank you so much