Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Sep 08, 2017 Sep 08, 2017

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.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;

}

TOPICS
Acrobat SDK and JavaScript
442
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2017 Nov 02, 2017

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2017 Nov 02, 2017
LATEST

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;

}

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines