• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Calculating number of filled fields

# Calculating number of filled fields

Community Beginner ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

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.

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript , PDF forms

Views

3.2K

Report

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

## 1 Correct answer

Community Expert , Feb 21, 2021 Feb 21, 2021

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 22
Community Expert ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

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;

Report

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

Copy link to clipboard

Copied

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.

Report

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

Copy link to clipboard

Copied

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

Report

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

Copy link to clipboard

Copied

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.

Report

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

Copy link to clipboard

Copied

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

Report

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

Copy link to clipboard

Copied

This worked!!! Thank you SO MUCH!!! I can't tell you how much I appreciate it.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

Hi!

I searched for this answer and your custom calculation is so very close to what I need. Can you tell me what the custom calculation would be for counting all filled fields containing a number value greater than 0? I would be so appreciative!

Report

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

Copy link to clipboard

Copied

Try like this:

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

Report

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

Copy link to clipboard

Copied

Hello! I, too, have been looking for hours for a custom calculation that will work for our employee appraisal form. I need something similar that will count all of the fields that contain a number between 1-4. For reference, the fields are named Rating1, Rating2, Rating3...Rating28. The problem I'm running into is that I need it to exclude any fields that have a 0 plugged in. I tried these codes and a few others I found on other threads here, but none of them have worked. I know nothing about this, so any help would be amazing!

Report

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

Copy link to clipboard

Copied

For reference, the closest code I've gotten to work is:

var total = "";
for (var i=1; i<=28; i++) {
if (this.getField("Rating"+i).valueAsString != "") total++;
}
event.value = total;

but it doesn't exclude any fields that have a 0.

Report

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

Copy link to clipboard

Copied

Change:

!= ""

To:

!== ""

Report

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

Copy link to clipboard

Copied

Nope, no luck there. It's still counting the fields with a 0.

Report

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

Copy link to clipboard

Copied

This will count only numbers between 1-4:

``````var total = "";
for (var i=1; i<=28; i++) {
var f = Number(this.getField("Rating"+i).valueAsString);
if (f > 0 && f <= 4) total++;}
event.value = total;``````

Report

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

Copy link to clipboard

Copied

That worked! Thank you so much!!!

Report

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

Copy link to clipboard

Copied

I've been searching for hours and this string seems to be the closest to what I am looking for.  For reference, I'm very new at building adobe forms.

I have created the form (attached), added some drop downs lists, digital signature blocks, and a few very basic calculations.  I'd like to have the form tally specific responses from the drop down list in the fields titled "dropdown6.0" through "dropdown6.19" and then place those responces in specific fields at the top of the form.

Essentially, I want to use the "COUNTIF" function from Excel, but the Adobe version.

How would I go about doing this?

Report

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

Copy link to clipboard

Copied

For "Leave" field, you can use this as custom calculation script of "Leave" field:

``````var total = 0;
for( var i=0; i<=19; i++){
var f = this.getField("Dropdown6."+i).valueAsString;
if(f === "Leave")total++;}
event.value = total;``````

Use same script for other fields, just replace "Leave" with another field value.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jul 01, 2024 Jul 01, 2024

Copy link to clipboard

Copied

Hi Nesa,

I have absolutely no knowledge in this and have run into a similar situation. I used the formula that you gave the original poster. My situation is that I have 4 fields in my fillable pdf, respectively labled as: csid, csid1, csid2, csid3. I need a calculation that I can use that will create a total when a text is entered in the 4 fields (whether its only in one or all of the 4 fields). Thank you so much!

Report

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

Copy link to clipboard

Copied

By calculating total you mean to count number of filled fields?

Try this:

``````var total = 0;
for (var i = 0; i <= 3; i++) {
var fields = (i === 0) ? "csid" : "csid" + i;
var f = this.getField(fields).valueAsString;
if(f !== "")total++;}
event.value = total;``````

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jul 02, 2024 Jul 02, 2024

Copy link to clipboard

Copied

To answer your question, yes. That is what I was trying to say lol. I copy and pasted it into the "custom calculation script" but it's not working. What am I doing wrong?

Report

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

Copy link to clipboard

Copied

LATEST

There can be any number of reasons why it's not working (script by itself does work fine), your field names can be wrong, or you put script in the wrong place...etc you need to check console for errors (CTRL+J) or post your file here.

Report

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

Copy link to clipboard

Copied

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

Report

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

Copy link to clipboard

Copied

This is for Adobe Acrobat, not Excel.