2 Replies Latest reply on Nov 2, 2017 12:42 PM by Thom Parker

    Sum across rows named "Hour.row#.col#"

    ryank29217023

      I have a 28x14 grid of form fields that are named "Hour.1.1" where the first digit is the row number and the second is the column number.  I have managed to figure out how to sum across the columns using a document level function and a custom calculation script, but I can't figure out how to tweak it to sum across rows.  The code that works to sum across columns is below.  Please help!!!

       

      // document level function

      function SumH(cParent){

      var nSum = 0;

      var oParent = this.getField(cParent);

      var aParent = oParent.getArray();

      for(var i = 0; i < aParent.length; i++){

      nSum += Number(aParent[i].value);

      }

      return nSum;

      } // end SumH function

      // end document level script

       

      // Custom Calculate script

      // use document level function

      var sum = SumH("Hour.1");

      // field value is blank unless sum greater than zero

      if (sum === 0) {

      event.value = "";

      } else {

      event.value = sum;

      }

        • 1. Re: Sum across rows named "Hour.row#.col#"
          try67 MVP & Adobe Community Professional

          Do you want to hard-code the number of rows into the script, or make it start from 1 and run until it can't find any more fields?

          • 2. Re: Sum across rows named "Hour.row#.col#"
            Thom Parker Adobe Community Professional

            The "getArray" function is nifty, but only works in one dimension because of the hierarchical nature of field grouping. However, since you want to add in either direction it is much easier, and more general, to dynamically build the field names.

             

            Here's a sample function, it will add either a row or a column, depending in the inputs:

             

            // Lets assume that the function already knows how many rows and columns are in the array,

            // this however could be another input into the function

            // cRootName is the root name of the fields

            // nStart is the row or column index that is being summed

            // bRow , if true, a row is being summed and nStart is a column number, if false, a column is being summed and nStart is a row number

             

            function SumRowCol(cRootName, nStart,  bRow)

            {

                var nEnd = bRow?28:14;

                var nSum = 0;

                for(var i=1;i<=nEnd;i++)

                     nSum += Number( this.getField(cRootName + "." + (bRow?i:nStart) + "." + (bRow?nStart:i)).value );

             

                 return nSum;

            }