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

Fillable Form - Average of multiple fields with options to skip

New Here ,
Feb 10, 2020 Feb 10, 2020

Copy link to clipboard

Copied

Hi Acrobat Community,

 

I'm trying to get an average of six fields, with the option of skipping blank fields or N/A, and have the value autofill a seventh field. It looks like this:

Sample1.jpg

 

I've searched all over these forums and many others and I've found a few ways to do this. The one I've settled on was made by try67 on these forums. The problem I believe I'm running into now is that, no matter what I do, I can't get an array to call back to my function. The code I'm using looks like this:

 

function calcAverage(aFields, bIgnoreBlanks, bIgnoreZeros) {

    var total = 0;

    var n = 0;

    for (var i in aFields) {

        var f = this.getField(aFields);

        if (f==null) {

            console.println("Error! Can't locate a field called: " + aFields);

            continue;

        }

        if (f.valueAsString=="" && bIgnoreBlank) continue;

        var v = Number(f.valueAsString);

        if (isNaN(v)) continue;

        if (v==0 && bIgnoreZeros) continue;

        total+=v;

        n++;

    }

    if (n==0) event.value = "";

    else event.value = total/n;

}


calcAverage(["Knowledge1", "Knowledge2", "Knowledge3", "Knowledge4", "Knowledge5", "Knowledge6"], true, true);

 

The error I'm getting is this:

Error-Sample2.jpg

 

If I'm understanding correctly, it is ignoring the array and just creating one long string of the array elements. I've tested this and cut the array down to only include "Knowledge1" and the script works perfectly for that one field. I've read through all of the comments on every post I've found discussing this and similar scripts, but no one else seems to have run into this issue, so any help would be greatly appreciated!

 

The original post I got the script from is here.

TOPICS
Acrobat SDK and JavaScript

Views

175

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

Community Expert , Feb 10, 2020 Feb 10, 2020

The "aFields" variable is an array of field name. To use it you need to reference a singe name in the array.

Change the line that gets the field value to this.

 

 var f = this.getField(aFields[i]);

 

Likes

Translate

Translate
Community Expert ,
Feb 10, 2020 Feb 10, 2020

Copy link to clipboard

Copied

The "aFields" variable is an array of field name. To use it you need to reference a singe name in the array.

Change the line that gets the field value to this.

 

 var f = this.getField(aFields[i]);

 

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 ,
Feb 10, 2020 Feb 10, 2020

Copy link to clipboard

Copied

LATEST

That did it! Thank you so much!

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