5 Replies Latest reply on Mar 17, 2013 8:10 PM by stl_tom_63031

    Ignoring Null or 0 for Averaging

    stl_tom_63031

      Hello,

       

      I do not code in Java so I am having a problem with a form.

       

      We have a training form that adds the total of the ratings and then is supposed to figure an average.  My problem is that the average provided is based on the canned average function for the field so it is calculating all of the fields, including the ones that have either a Null value or a zero.  Can anyone please help me with this?

       

      By the way.  If the N.O. box is checked, the rating is N/A.

       

      There are also five sections which will total all of the ratings and will need an average of those totals.

       

      Thanks in advance.

       

      Tom

       

      FTO Form SCRN SHOT.png

       

       

       

      Screen clipping taken: 3/15/2013 9:09 PM

        • 1. Re: Ignoring Null or 0 for Averaging
          try67 MVP & Adobe Community Professional

          Here's a generic function to calculate the average of a group of non-empty fields.

          This function takes two parameters: an array of field names to average, and a boolean specifying whether the function should return a blank string if the result was zero.

           

          function averageFields(fields, blankIfZero) {

              var total = 0;

              var n = 0;

              for (var i in fields) {

                  var f = this.getField(fields[i]);

                  var v = f.valueAsString;

                  if (v) {

                      total += (+v);

                      n++;

                  }

              }

              var returnValue = (n==0) ? "" : total/n;

              if (blankIfZero && returnValue===0) returnValue = "";

              return returnValue;

          }

           

          You can place this function at the document-level and then call it like so (from a custom calculation script, in this case):

          event.value = averageFields(["Field1", "Field2", "Field3"], false);

          • 2. Re: Ignoring Null or 0 for Averaging
            stl_tom_63031 Level 1

            How do I put this at a document level?

             

            Tom

            • 3. Re: Ignoring Null or 0 for Averaging
              try67 MVP & Adobe Community Professional

              In Acrobat XI: Tools - JavaScript - Document JavaScripts

              • 4. Re: Ignoring Null or 0 for Averaging
                stl_tom_63031 Level 1

                It does not appear to be working.  How can I debug?

                 

                //-------------------------------------------------------------

                //-----------------Do not edit the XML tags--------------------

                //-------------------------------------------------------------

                 

                 

                //<Document-Level>

                //<ACRO_source>averageFields</ACRO_source>

                //<ACRO_script>

                /*********** belongs to: Document-Level:averageFields ***********/

                function averageFields(fields, blankIfZero) {

                    var total = 0;

                    var n = 0;

                    for (var i in fields) {

                        var f = this.getField(fields[i]);

                        var v = f.valueAsString;

                        if (v) {

                            total += (+v);

                            n++;

                        }

                    }

                    var returnValue = (n==0) ? "" : total/n;

                    if (blankIfZero && returnValue===0) returnValue = "";

                    return returnValue;

                }

                //</ACRO_script>

                //</Document-Level>

                 

                 

                //<AcroForm>

                //<ACRO_source>Average for Section:Calculate</ACRO_source>

                //<ACRO_script>

                /*********** belongs to: AcroForm:Average for Section:Calculate ***********/

                event.value = averageFields(["Rating 1", "Rating 1","Rating 2"],"Rating 3","Rating 4","Rating 5","Rating 6","Rating 7", false);

                //</ACRO_script>

                //</AcroForm>

                • 5. Re: Ignoring Null or 0 for Averaging
                  stl_tom_63031 Level 1

                  Disregard.  I found the error in the call out.  Looks like it is working great.  Thank you for the help!