Count fields that contain text

Community Beginner ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

I have a form with 3 columns that contain package tracking numbers.  I would like to add a "package count" field that counts how many fields in each column contain input.  For instance, "Total FedEx" would look at all cells named FEDEX1, FEDEX2 ...FEDEX40 and provide a package count.

TOPICS
PDF forms

Views

207

Likes

Translate

Translate

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 guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Feb 22, 2021 Feb 22, 2021
As "Custom calculation script" of "Total FedEx" field use this:var total = ""; for (var i=1; i<=40; i++) { if (this.getField("FEDEX"+i).valueAsString != "") total++; } event.value = total;

Likes

Translate

Translate
Adobe Community Professional ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

As "Custom calculation script" of "Total FedEx" field use this:

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

Likes

Translate

Translate

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 guidelines
Community Beginner ,
Feb 23, 2021 Feb 23, 2021

Copy link to clipboard

Copied

That works great, and eSy to customize for different carriers.  Is there a way to make the total appear in Parentheses?

Likes

Translate

Translate

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 guidelines
Adobe Community Professional ,
Feb 23, 2021 Feb 23, 2021

Copy link to clipboard

Copied

Yes, change last line like this:

event.value = "("+total+")";

Likes

Translate

Translate

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 guidelines
New Here ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Can I use this same calculation script to count the total number of persons signing onto a form? The form has 60 text fields, 20 to print name, 20 to digitally sign and 20 for badge numbers, at the top right corner I have a text field titled "Total Workers" which is were I want the total to be calculated.

 

I change (var i=1; i<=40; i++) to (var i=1; i<=20; i++)if (this.getField("FEDEX"+i).valueAsString != "") total++; to if (this.getField("PRINT NAME"+i).valueAsString != "") total++; . However, the script did not calculate. Is there something else that I need to change or something that I'm doing wrong? 

Likes

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Are your fields named: PRINT NAME1,PRINT NAME2...PRINT NAME20?

Your code should look something like this:

var total = "";
for (var i=1; i<=20; i++)
if (this.getField("PRINT NAME"+i).valueAsString != "") total++;
event.value = total;

 

Likes

Translate

Translate

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 guidelines
New Here ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

You're correct, the text fields are named just as you mentioned.

I just tried the code but it doesn't calculate anything in the "total worker" when I type in the text fields.

Is there anything something that I have to do with the text fields before the code works?

Likes

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

Please look in the Console window to see if any errors are reported. 

Display the console window with Ctrl-J

 

Also, post the exact code you are using.

 

 

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

Likes

Translate

Translate

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 guidelines
New Here ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Below are the errors that console shows.
TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate

 

Below is the exact script that I have in the custom calculation script field.

var total = "";
for (var i=1; i<=20; i++)
if (this.getField("PRINT NAME"+i).valueAsString != "") total++;
event.value = total;

Likes

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

The errors reported in the console would suggest that the field name used in the script is incorrect.

 

Make sure your field names exactly match the name used in the script.

"PRINT NAME1" ..... to ....  "PRINT NAME20"

 

 

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

Likes

Translate

Translate

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 guidelines
Adobe Community Professional ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

LATEST

You should test out this script in the Console Window before putting it in the field script.

To use it in the console, just remove the last line "event.value = total"

 

This will tell you exactly where the code is going wrong.  

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

Likes

Translate

Translate

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 guidelines