Copy link to clipboard
Copied
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 != ""...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Well, swap the numerator and denominator....oops
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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));```"
Copy link to clipboard
Copied
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!!!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now