13 Replies Latest reply on Jul 15, 2010 10:12 AM by beardjeff

    Incredibly simple question re: calculated field script

      Greetings all!  I have what's probably the simplest question, but I'll be darned if I can find an answer here or online.  Here goes.

       

      I have an extremely simple form...it has approx. 50 names pre-entered/printed, and there's 3 form fields.  The first two are labeled P and G, and they're setup as P1 and G1 for the first person, P2 and G2 for the next, and so on.  My third field is labeled A (1 through 50, like the other fields), and this field is to calculate P divided by G.  Simple, right?  ALL fields are formatted as numbers, right-aligned.  So, I tried a simple Javascript of (G1/P1), which works, but upon "entering" the form, I get that annoying Java popup about the field formats not matching.

       

      So, I ask, what is the simplest script I can use that works, without the popup?  I'm an admitted novice at this.

       

      Many thanks in advance!!!

        • 1. Re: Incredibly simple question re: calculated field script
          MarkWalsh Level 4

          Where do the values come from in P & G? If P is empty in any of your fields, you're dividing by zero.

          • 2. Re: Incredibly simple question re: calculated field script
            Level 1

            P and G are integers entered by the user...whole, positive, numbers; no decimals.

             

            I kinda figured that with no values entered in P and/or G, this divide by zero would be a problem...how do I remedy this?

             

            Again, P and G are simple numbers--12345, 123, etc.  Fields are blank prior to being keyed in by the user.

             

            Here's an admittedly simple view:

             

            Name               P               G               A

            John Doe     _______     _______     _______

            Jane Smith  _______     _______     _______

             

            As indicated above, P, G, and A are formatted as Number/Decimal Places-0/Right Aligned.

             

            Thanks again!

            • 3. Re: Incredibly simple question re: calculated field script
              MarkWalsh Level 4

              Showing the code you are using would be more helpful than the layout of the fields.

               

              To prevent the problem with dividing by zero, you check the values of the 2 fields, and only do the division if they have data entered into them.

              • 4. Re: Incredibly simple question re: calculated field script
                gkaiseril MVP & Adobe Community Professional

                What is the result of divison by zero?

                 

                Change the format of the P fields to 'None'.

                 

                What displays now?

                 

                With both fields empty or set ot zero?

                 

                With a posative number in 'F' and zero in 'G'?

                 

                With a negative number in 'F' and zero in 'G'?.

                 

                You need to provide the control statements to prevent dvision by zero. And the 'Simplified field notation' does not support any JavaScript control statements, properties, functions, methods, etc. So you will need to write a 'Custom calculation script'.

                • 5. Re: Incredibly simple question re: calculated field script
                  Level 1

                  I'll be honest with you, Mark:  I've jerked around with this PDF so many FREAKING times, trying to make it work, that I don't have a revision anymore with the scripts.  After a while, I said screw it, let me start all over.

                   

                  What's gotten me even more frosted is that there's a million resources online, and here at Adobe, for scripts and calculations that do every thing except change the oil in your car.  I don't NEED all that funky stuff...here's the more explicit deal.

                   

                  I've worked a LITTLE with Acrobat, creating basic, simple forms for work.  No fancy calculations, just a few drop-down choice boxes, etc.  Never dealt with any Javascript, to be honest.  The PDF in question is a bowling league final average sheet, that I'm trying to modify a bit to allow entry of whole, positive integers.  The "P" field stands for total pins; what the bowler knocked down throughout the season.  The "G" field is the number of games that person bowled.  The "A" field is their average, expressed as a whole number, no decimals, which is pins divided by games.  For each of the 50 or so bowlers in the league, I have labeled each field accordingly:  P1, G1, A1 for the first bowler's name; then P2, G2, A2 for the next person, and so on.

                   

                  Here's what I want to do:  Open the PDF.  Then, click in the "P1" field, enter Bowler #1's pins.  Hit the Tab key, move over to "G1", enter the number of games.  Tab again, their average appears in A1.  Tab again, cursor moves to P2 field, and so on.  (note:  I know how to set Tab Order.)

                   

                  Here's my questions:

                   

                  --what must I "script" in the "A" fields to perform the calculation?  (again, no decimals needed)

                   

                  --any special formatting needed in any fields? (P, G, or A)

                   

                  --How, and where, would I handle the "divide by zero" issue, and if there's a script for the calculation of P/G, where does this syntax appear?  Before the P/G, or after?

                   

                  I told you it was simple, but like I said, I'm an amateur at this...spreadsheet, no problem, but this PDF thing has got me going!

                   

                  I hope that you have a better handle on what I'm looking for now, and if not, please let me know.  Thanks again for your assistance!

                   

                  FYI:  I just wanted to add that I DON'T know the Javascript, or the syntax order.  There was another post made that said, in so many words, to do this or do that...if I knew what I was doing, I wouldn't be asking here!

                   

                  Message was edited by: mike91163

                  • 6. Re: Incredibly simple question re: calculated field script
                    Level 1

                    GKaiseril, I know that I need handle division by zero, and I know that I need a custom script.  BUT, I'm asking what I need to script to do this!  If I knew, I wouldn't be asking.

                     

                    Again, allow me to state this:  I'm an AMATEUR, looking for help.

                     

                    Thanks anyway!

                    • 7. Re: Incredibly simple question re: calculated field script
                      gkaiseril MVP & Adobe Community Professional

                      There are several ways to accomplish this.


                      One can use the 'if' statement:


                      // custom calculation script for row 0
                      var nRow = 0; // value for row
                      var sP = this.getField('P' + nRow).value;
                      var sG = this.getField('G' + nRow).value;
                      event.value = ''; // force result no matter what
                      if(sG != 0)
                      event.value = Number(sP) / Number(sG); // perform division


                      Or by testing the result:

                      var nRow = 0; // value for row
                      var sP = this.getField('P' + nRow).value;
                      var sG = this.getField('G' + nRow).value;
                      var fResult = Number(sP) / Number(sG); // perform division
                      if(Math.abs(fResult) ==  'Infinity' | isNaN(fResult))  result = ''; // force to null of inifity or NaN
                      event.value = fResult;


                      One can even write a function and reuse the code:

                      // document level function

                      function Divison(sDividend, sDivisor) {
                      // function to divide passed dividend by passed divisor
                      // returns result or null
                      var fResult = Number(sDividend) / Number(sDivisor); // perform division
                      if(Math.abs(fResult) ==  'Infinity' | isNaN(fResult))  fResult = ''; // force to null of inifity or NaN
                      return fResult;
                      } // end Division function

                      // end of document level function

                       

                      // custom calculation for division or row 0
                      var nRow = 0; // value for row
                      var sP = this.getField('P' + nRow).value;
                      var sG = this.getField('G' + nRow).value;
                      event.value = Divison(sP, sG); // perform division


                      In the above scripts changing the value 'nRow' to the row number, 0 or 1, will change the custom calculation scripts for the appropriate row.

                       

                      Adobe provides the Acrobat SDK for free download along with a lot of documentaion for Acrobat JavaScript. There are a number of web based resouces like Planet PDF, PDF Scripting, and Acrobat Users Community that are free or provide some free support.

                       

                      If you had done what I had asked you would have learned a little more about how Acrobat JavaScript works and new debugging tool that you coule use.

                      • 8. Re: Incredibly simple question re: calculated field script
                        JeffBinTN

                        The easiest way to resolve this is with "if/then/endif".

                         

                        if([YOUR DENOMINATOR]>0)then[YOUR FORUMLA]endif

                         

                        For example, if you're looking for P/G:

                         

                        if(G>0)then(P/G)endif

                         

                        That tells it only to execute the formula when the denominator is greater than zero.  If you have a more complex denominator, include the entire denominator in parentheses followed by >0 in place of the G above.

                         

                        It's not a dazzling solution, but it gets it done.

                        • 9. Re: Incredibly simple question re: calculated field script
                          try67 MVP & Adobe Community Professional

                          There's no such thing as if-then-endif in JavaScript.

                          • 10. Re: Incredibly simple question re: calculated field script
                            gkaiseril MVP & Adobe Community Professional

                            This question has been answered many times. Search for 'division' or 'divide' and there will be over 16 pages of post. Also the Mozilla developer network has free documentation about JavaScript and Adobe's Acrobat Developer Network has free documentation.

                            • 11. Re: Incredibly simple question re: calculated field script
                              beardjeff

                              Is there a reason the field must be calculated in JavaScript?  From the way the problem has been presented, I'd change the scripting language for the object to FormCalc and just use an IF statement as I said before.

                              • 12. Re: Incredibly simple question re: calculated field script
                                gkaiseril MVP & Adobe Community Professional

                                Acrobat created forms only support JavaScript.

                                 

                                FormCalc is an optional language in the XML forms created by LiveCycle Designer forms. The other language is JavaScirpt.

                                 

                                These are 2 very different approaches to form design available within the PDF format. The AcroForm structure was developed by Acrobat and the XML format was developed by JetForms. Adobe later purchased JefForms and added that product under the Adobe name of LiveCycle Forms. Acrobat/Reader versions 5.01 and above are needed to access forms created by LiveCycle Designer, while any version of Acrobat/Reader from 3.01 on can access AcroForm forms.

                                • 13. Re: Incredibly simple question re: calculated field script
                                  beardjeff Level 1

                                  Thanks for the clarification.  I was originally in the LiveCycle Designer forum and somehow at some point must have wound up in a different place.