Skip to main content
Participant
Feb 19, 2021 10:46 PM UTC
Answered

Calculating number of filled fields

Hi all,

Long time lurker, first time poster. I am working in Adobe Acrobat Pro 2017, and created a fillable PDF for employee appraisals. To calculate the employee's score at the end of the form, I need to calculate the number of fields where text was entered (like the Excel "COUNTA" function) so I can divide that by the added total of ratings. I have everything figured out except the custom calculation script for totalling the number of fields that have text entered in them. I've spent the past 8 hours deep in the weeds in Google and this support community and have found numerous similar scenarios, but I can't get any of those scripts to work. I'm old, my brain is fried, I know zilch about JavaScript and would really, REALLY appreciate any insight or help.

 

For reference, the fields are named COM.01 through COM.18, and I just need to count the total number of fields containing text, leaving out any COM. fields that are blank.

 

Thanking you in advance.

This topic is closed to new replies. Start a new post to keep the conversation going.
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Feb 21, 2021 8:20 AM UTC

Change the value of one of those fields and then check the JS Console (Ctrl+J) for error messages.

If you can't figure it out please share the file with us for further help.


See if this works for you:

var total = 0;
var fields = this.getField("COM").getArray();
for (var i=0; i<fields.length; i++) {
var f = (fields[i]).valueAsString;
if (f !="") total++;
}
event.value = total;

22 replies

Participating Frequently
Jun 12, 2024 5:48 PM UTC

I'm not real good with Excel.. where do 
I put the calculation code..?  Please give more details.

Translate
Nesa Nurani
Community Expert
Community Expert
Jun 12, 2024 7:30 PM UTC

This is for Adobe Acrobat, not Excel.

Translate
try67
Community Expert
Community Expert
Feb 19, 2021 10:57 PM UTC

You can use this code as the custom calculation script of your text field:

 

var total = 0;
var fields = this.getField("COM").getArray();
for (var i=0; i<fields.length; i++) {
	if (f.valueAsString!="") total++;
}
event.value = total;

 

If you want it to immediately display the relative amount of filled-in fields to the total, change the last line to:

event.value = total/fields.length;

Translate
Erin5C66Author
Participant
Feb 19, 2021 11:08 PM UTC

Thank you very much. I copied and pasted that into the field where I need the result, but nothing happened when I entered text into the "COM." fields.

Translate
Erin5C66Author
Participant
Feb 19, 2021 11:09 PM UTC

Edit: I pasted it into the custom calculation script in the field where I  need the result. I AM TIRED, lol.

 

Translate