2 Replies Latest reply on Sep 25, 2012 12:54 PM by gkaiseril

    How do I have Adobe Acrobat 9.0 calculate the minimum of 10 fields BUT ignore empty fields?

    ocean-scape

      How do I have Adobe Acrobat 9.0 calculate the minimum of 10 fields BUT ignore the field if it is empty? Currently it counts blanks as 0 so when it comes up with the minimum it will always show 0 unless all 10 fields have values in them.

       


        • 1. Re: How do I have Adobe Acrobat 9.0 calculate the minimum of 10 fields BUT ignore empty fields?
          George_Johnson MVP & Adobe Community Professional

          You could loop through the fields and add the non-blank values to an array, and use the Math.min method to get the minimum. Here's an example custom Calculate script:

           

          // Custom calculate script

          (function () {

           

              // Array to hold non-blank field values

              var fa = [];

           

              // Loop through the fields...

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

           

                  // ...get the current field value, as string...

                  var v = getField("text1." + i).valueAsString;

           

                  // ...if not blank, add value to the array, as a number

                  if (v) fa.push(+v);

              }

           

              // Set this field's value to the minimum value in the array, otherwise blank

              event.value = fa.length > 0 ? Math.min.apply(null, fa) : "";

           

          })();

           

          This assumes the fields are named "text1.1", "text1.2", ..."text1.10" and that they're numeric. This type of field naming simplifies the code, so it would be good to use something similar and adjust the getField statement to match.

          • 2. Re: How do I have Adobe Acrobat 9.0 calculate the minimum of 10 fields BUT ignore empty fields?
            gkaiseril MVP & Adobe Community Professional

            I would put the non-blank values of the fields into an array and then find the minimum value for the items in the array.

             

            // array of field names to process
            var sFields = new Array("Text0", "Text1", "Text2", "Text3", "Text4", "Text5", "Text6", "Text7", "Text8", "Text9");  
            event.value = ""; // default value

            // add values of fields to array
            var aNumbers = new Array(); // array of values
            var sValue = ""; // value for a field
            // loop through the fields
            for(i = 0; i < sFields.length; i++) {
            sValue = this.getField(sFields[i]).valueAsString;
            // add to array if non-blank value
            if(sValue != "") aNumbers[aNumbers.length] = sValue;
            } // end loop

            function Min(aValues) {
            /*
            determine the minum value from an array of values
            input parameter array of values
            return the minum value for the passed parameter values
            */
            var Minimum = aValues[0];
            console.println(aValues);
            // loop through the passed parameter array
            for(i = 0; i < aValues.length; i++) {
            console.println(Minimum + " " + aValues[i]);
            Minimum  = Math.min(Minimum, aValues[i]);
            } // end loop
            return Minimum;
            } // end function

            // if any values in aNumbers process
            if(aNumbers.length != 0)
            event.value = Min(aNumbers);