Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
3

Field Displaying Percent of Form Completed

Community Beginner ,
Aug 21, 2023 Aug 21, 2023

New to Javascript, even newer to javascript in adobe. I have a fillable adobe form. It's been requested that I add a field that displays the percentage complete of the form. So as the user fills in more of the form, that number would change. Is there a formula I could use on a calculated text field for the default value? Something like, =this.fields.count / (count(this.fields <> "").

I realize I may have to have each field listed in the denominator as an this.field.x != ""...

TOPICS
How to , JavaScript , PDF forms
753
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Aug 21, 2023 Aug 21, 2023

Calculating the Percent completion of a form is tricky since not all fields need to be filled in, and some are calculated. If this is an issue on your form, then the %complete could be restricted to fields that are marked as required and can be filled by the user, i.e., skip fields that are marked as readOnly. 

 

But, just in general, the way to do this calculation is to compare entered field values to the fields default value.

 

var nSum = 0, nCnt = 0, oFld;

for(var i=0;i < this.numFields;i++)
{
   oFld = this.getField(this.getNthFieldName(i));
   // Skip buttons and readOnly fields, could also add required test here
   if(!oFld.readonly && (oFld.type != "button"))
   {
       nCnt++;
       if(oFld.value != oFld.defaultValue)
          nSum++;
   }
}
event.value = nSum/nCnt;

 

Formatting the output is done in the Format Script.

 

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

View solution in original post

Translate
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 ,
Aug 21, 2023 Aug 21, 2023

Well, swap the numerator and denominator....oops

Translate
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 Expert ,
Aug 21, 2023 Aug 21, 2023

Calculating the Percent completion of a form is tricky since not all fields need to be filled in, and some are calculated. If this is an issue on your form, then the %complete could be restricted to fields that are marked as required and can be filled by the user, i.e., skip fields that are marked as readOnly. 

 

But, just in general, the way to do this calculation is to compare entered field values to the fields default value.

 

var nSum = 0, nCnt = 0, oFld;

for(var i=0;i < this.numFields;i++)
{
   oFld = this.getField(this.getNthFieldName(i));
   // Skip buttons and readOnly fields, could also add required test here
   if(!oFld.readonly && (oFld.type != "button"))
   {
       nCnt++;
       if(oFld.value != oFld.defaultValue)
          nSum++;
   }
}
event.value = nSum/nCnt;

 

Formatting the output is done in the Format Script.

 

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

Translate
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 ,
Aug 21, 2023 Aug 21, 2023

thank you so much! This works great. If I wanted to dispay this value as an integer would I cange the last line to
```
event.value = CInt(nSum/nCnt) * 100));```"

Translate
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 ,
Aug 21, 2023 Aug 21, 2023
LATEST

I figured out that if I use your calculations in the 'custom calculation script' then format as a number with 0 decimal places, the field displays as intended. Thank you so much!!!! 

Translate
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