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

How to count filled fields

dlettow
Registered: Jan 24 2008
Posts: 20
Answered

I've been given an Excel form to remake as a PDF. In it are 8 fields that can be used to list job duties (duty1, duty2, etc.). The original form then has a field that counts the number of those duty fields that were actually filled in, using Excel function COUNTA(). Is there a way to duplicate that function in Acrobat?

Donna

My Product Information:
Acrobat Pro 8.1.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can create a function to replicate the Excel function that returns the non-empty count. This function can be placed as a document leve l funtion or folder level fucntion. Then for the individual field or other use you would only need to call the function with a list of the fields to be tested.

// document of folder level function
function CountA() {
/*
Count the non-empty fields, up to 256 fields
Inputs: field names passes as argument to this function
Returns: count of non-empty fields
*/
var nCount = 0; // number non-empty fields
// loop through this functions augment array
for (i = 0; i < arguments.length; i++) {
if (this.getField(arguments[i]).value != '') nCount++; // increment non-empty field
} // end loop
return nCount; // return count
} // end CountA function
// end document or folder level function


// field custom calculation script
event.value = CountA('duty1', 'duty2', 'duty3', 'duty4','duty5');

George Kaiser

dlettow
Registered: Jan 24 2008
Posts: 20
That's great, thanks!

Donna
dlettow
Registered: Jan 24 2008
Posts: 20
The code worked perfectly when I used it to count the number of text fields that had been filled in. However, I ran into a snag with a set of fields that had numeric ratings in them ranging from 0 (Unsatisfactory) to 4 (Outstanding). It looks as if the code can't count the field as filled if it has a 0 in it instead of being blank. The fields have to be numeric because later on the numbers get added together then divided by the number of fields that were filled in to get an average score.

I've tried some minor variations on the code myself to no avail, but I don't really understand how Javascript handles numeric fields versus text fields. Any thoughts would be appreciated!

Donna
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use the 'valueAsSting" rather than the "value" property to test for an empty field.

George Kaiser

dlettow
Registered: Jan 24 2008
Posts: 20
Perfect! Thanks so much for all your help.

Donna
Question Queen
Registered: Sep 22 2009
Posts: 1
I think this is the answer I need for my form as well, however, I don't understand where the code should be located for either scenario, the folder level or the form level.

Any help would be greatly appreciated.

Thanks