4 Replies Latest reply on Oct 31, 2011 9:18 AM by try67

    How to write a simple script to divide

    hdcedd

      I have created a form with an embedded chart to calculate failure rate.  The text fields are (horizonally) Block, Subject, # failed students, # total students, failure rate where subject is a text box, # failed students is a number, # total students is a number, and failure rate is a percentage.  I need to create:

       

      # failed students / # total students

       

      I tried using simplified field notation that reads:

       

      Failing StudentsA1B1 / Total StudentsA1B1

       

      I have little to no knowledge of Java Script, but that is what I'm seeing on the some "how to"/tutorial sites online.  I'm getting 0.00% in the Failure Rate box regardless of my entry.  I hope this makes sense.  Please help.

        • 1. Re: How to write a simple script to divide
          try67 MVP & Adobe Community Professional

          You can't use this option if your fields contain spaces in their names.  Also, you have to account for the cases when the divisor is zero, which  is illegal. Use this code instead as the custom calculation script:

           

           

          event.value = "";

          var failing = getField("Failing StudentsA1B1").value;

          var total = getField("Total StudentsA1B1").value;

          if (failing!="" && total!="" && Number(total)!=0)

                event.value = Number(failing) / Number(total);

          • 2. Re: How to write a simple script to divide
            gkaiseril MVP & Adobe Community Professional

            Just be glad Acrobat/Reader does not go into an infninate loop like other applications did in the past.

             

            Are you getting any error messages in the Acrobat JavaScirpt debugging console?

             

            I get the following messages:

             

            2:Field:Calculate
            missing ; before statement
            3:Field:Calculate

             

            The division operation is certainly available to the Simplified Field notation. The most common issue is that when the divisor is zero, the divsion operation returns the character string "NaN", "Infinity" or "-Infinity" all of which do not match the numeric format. "Infinity" and "-Infinity" are both valid numeric values but not for the Number or Percentage format.

             

            If you were to try another aritmetic operation, like addition, I would expect your code to also fail.

             

            You have inclueded spaces in your field names and Adobe uses white space charaters to identify variable names, constants, and operands. You need to use the special excape charater to use special values in a variable name for the Simplified Field Notation. The special Escape character is the "\" character. Your code code read:

             

            Failing\ StudentsA1B1 / Total\ StudentsA1B1

             

            And with a field with a format of "None" you will get an answer of "NaN" for the calculation. A null string is treated as a zero.

             

            If you want to use the "Number" or 'Percent" formats you will need to use a custom JavaScript calculation to test for zero or null and control the executed code accordingly.

             

            var nNumerator = this.getField("Failing StudentsA1B1").value;
            var nDivisor = this.getField("Total StudentsA1B1").value;
            var nResult = 0;
            // test for non-zero value
            if(nDivisor != 0) {
            // perform division
            nResult = nNumerator / nDivisor;
            } // end non-zero

            event.value = nResult;

            • 3. Re: How to write a simple script to divide
              DC Dowd

              I used a modified version of try67's script in fields that needed to be divided by other fields in a form I'm buiding and it works like a charm. try67, whether or not the original poster notes it, this is an excellent answer to his question.

              • 4. Re: How to write a simple script to divide
                try67 MVP & Adobe Community Professional

                Glad to hear it!