These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Simple Sum not working

TiredEyes
Registered: Mar 24 2011
Posts: 3
Answered

Hi All,
 
I have created a form that horizontially counts the number of non blank field using the following code
 
// array of field names to check
var aFields = new Array("1Row1","2Row1","3Row1","4Row1","5Row1","6Row1","7Row1","8Row1","9Row1","10Row1","11Row1","12Row1","13Row1");
var count = 0; // variable to store count of non-empty fields
for (i = 0; i < aFields.length; i++) {
if (this.getField(aFields[i]).valueAsString != "") count++
} // end loop
event.value = count;
 
//to remove the 0
if (event.value==0) event.value="";
 
There are 15 of these counting cell, which then need to Summed to give a total.
 
The counting fields work fine but the sum does not and I can not use the simple select a field option as it shows the 0 instead of null.
 
I have tried variations of the following:
 
function SumArray(aFields){/* sum an Array of field namesParameter:
aFields = array of field names to sum
Returns sum of fields
*/var sum = 0; // result of summation of fields// loop through the array of field namesfor(i = 0; i < aFields.length; i++){// add value of i element from array of field namessum += Number(this.getField(aFields[i]).value);} // end of loopreturn sum;// return computed value // define an array of field names to sumvar aFieldNames = new Array("TotalforDayRow1","TotalforDayRow2","TotalforDayRow3","TotalforDayRow4","TotalforDayRow5","TotalforDayRow6","TotalforDayRow7","TotalforDayRow8","TotalforDayRow9","TotalforDayRow10","TotalforDayRow11","TotalforDayRow12","TotalforDayRow13","TotalforDayRow14","TotalforDayRow15");
 
//to remove the 0
if (event.value==0) event.value="";}
 
Could someone please let me know where I have gone wrong. I need the form to be blank so it can be printed as well and completed electronically.
 
I did the form in Acrobat 9 Pro but can put if into LiveCycle if that will help.
 
Thanks,
 
TiredEyes

My Product Information:
Acrobat Pro 9.3, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You never apply namessum to event.value.
Before checking if event.value == 0, you should apply the sum to it:
event.value = namessum;
if (event.value==0) event.value="";}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
function SumArray(aFields){
/* sum an Array of field names
Parameters:
aFields = array of field names to sum
Returns sum of fields
*/
var sum = 0; // result of summation of fields
// loop through the array of field names
for(i = 0; i < aFields.length; i++){
// add value of i element from array of field names
sum += Number(this.getField(aFields[i]).value);
} // end of loop
return sum;// return computed value
} // end function SumArray

// define an array of field names to sum
var aFieldNames = new Array("TotalforDayRow1", "TotalforDayRow2",
"TotalforDayRow3", "TotalforDayRow4", "TotalforDayRow5",
"TotalforDayRow6", "TotalforDayRow7", "TotalforDayRow8",
"TotalforDayRow9", "TotalforDayRow10", "TotalforDayRow11",
"TotalforDayRow12", "TotalforDayRow13", "TotalforDayRow14",
"TotalforDayRow15");

//to remove the 0
if (event.value==0) event.value="";}


I would also use a function to perform the count of a number of fields that do not have a null or empty value.

function CountArray(aFields){
/* count an Array of fields names that are not null
Parameters:
aFields = array of field names to sum
Returns sum of fields
*/
var count = 0; // result of the count of non-null fields
// loop through the array of field names
for(i = 0; i < aFields.length; i++){
if(aFields[i].value != '') {
count++; // non-null value - increment count
} // end non-null
} // end of loop
return count == 0? "" : count; // return count or null
} // end function SumArray

Use of functions cuts down on the amount of coding you need to do and reduces changing a repeated set of statements to changing the code in one location. It can also reduce coding effort for multiple forms by including tested funciotns in the other forms.

George Kaiser

TiredEyes
Registered: Mar 24 2011
Posts: 3
Thanks to you both for your quick responses.

I tried both methods and the first by try67 did show a blank cell but it showed it when the input cells have values in them as well.

George I copied as pasted yours as is and it came back with
syntax error
25: at line 26
and I cant see want is wrong - but I am a novice.

Any advice greatly appreciated.

Thanks

TiredEyes
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Make sure your field calculation order is correct. The total field should calculate after all others.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
For the sum function, the very last line that reads:

if (event.value==0) event.value="";}

Should read:

if (event.value==0) event.value="";

There should be no trailing "}".

It also needs to call the function:

function SumArray(aFields){
/* sum an Array of field names
Parameters:
aFields = array of field names to sum
Returns sum of fields
*/
var sum = 0; // result of summation of fields
// loop through the array of field names
for(i = 0; i < aFields.length; i++){
// add value of i element from array of field names
sum += Number(this.getField(aFields[i]).value);
} // end of loop
return sum;// return computed value
} // end function SumArray

// define an array of field names to sum
var aFieldNames = new Array("TotalforDayRow1", "TotalforDayRow2",
"TotalforDayRow3", "TotalforDayRow4", "TotalforDayRow5",
"TotalforDayRow6", "TotalforDayRow7", "TotalforDayRow8",
"TotalforDayRow9", "TotalforDayRow10", "TotalforDayRow11",
"TotalforDayRow12", "TotalforDayRow13", "TotalforDayRow14",
"TotalforDayRow15");

event.value = SumArray(aFieldNames);
//to remove the 0
if (event.value==0)
event.value="";

Of course you could also use the the 'Field is the Sum of:' or the simplified field notatain and then use a validation script to supress the zero result.

The 'count' function need to use the 'the.getField' for checking the value of the fields;

function CountArray(aFields){
/* count an Array of fields names that are not null
Parameters:
aFields = array of field names to sum
Returns sum of fields
*/
var count = 0; // result of the count of non-null fields
// loop through the array of field names
for(i = 0; i < aFields.length; i++){
if(this.getField(aFields[i]).value != '') {
count++; // non-null value - increment count
} // end non-null
} // end of loop
return count == 0? "" : count; // return count or null
} // end function CountArray

// define an array of field names to sum
var aFieldNames = new Array("TotalforDayRow1", "TotalforDayRow2",
"TotalforDayRow3", "TotalforDayRow4", "TotalforDayRow5",
"TotalforDayRow6", "TotalforDayRow7", "TotalforDayRow8",
"TotalforDayRow9", "TotalforDayRow10", "TotalforDayRow11",
"TotalforDayRow12", "TotalforDayRow13", "TotalforDayRow14",
"TotalforDayRow15");

event.value = CountArray(aFieldNames);

And with those two functions you can create an average fiction that does not include null values.

function AvgArray(aFields) {
/* count an Array of fields names that are not null
Parameters:
aFields = array of field names to average
Returns sum of fields
*/
nAverage = ''; // default result
var nCount = CountArray(aFieldNames)
if(nCount != 0) {
nAverage = SumArray(aFieldNames) / nCount;
}
return nAverage;
} // end AvgArray

It is best to place the code for the functions as document scripts so the code is available to all fields.

George Kaiser

TiredEyes
Registered: Mar 24 2011
Posts: 3
Sorry for the late response.

I have just tried George's response and it works perfectly!
Thank you to all those that were kind enough to respond.

Sincerely

TiredEyes