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

Adobe Acrobat Pro 2017: Averages, avg of averages, ignore blanks, ignore unchecked

New Here ,
Jun 12, 2020 Jun 12, 2020

Copy link to clipboard

Copied

Good Afternoon,

I've tried to use the below formula from TYR67 on a form I'm making for my Sleep Clinic and ran into some errors I was looking for some help to square it away.

/*

    @Params

        aFields : an array of the field names to use for the calcluation

        bIgnoreBlanks : a boolean that tells the function whether or not to ignore blank fields

        bIgnoreZeros : a boolean that tells the function whether or not to ignore zero values

*/

function calcAverage(aFields, bIgnoreBlanks, bIgnoreZeros) {

    var total = 0;

    var n = 0;

    for (var i in aFields) {

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

        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;

}

 

You can then call it from the field's calculation script like this:

 

calcAverage(["Field1", "Field2", "Field3"], true, true);

__________________________

There were errors finding fields and as per forum suggestions I changed:

var f = this.getField(aFields);

to

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

Now the only error I get is:

ReferenceError: bIgnoreBlank is not defined
31:Document-Level:calAverage.

 

*no clue what I'm doing and just winging all of this...

I ended up making check boxes and radio buttons to perform the same function, just to test the script on a different kind of choice selection....

 

anyhow,

I have a form and part of it calculates FOSQ-10(Sleep Quality/ quality of life)

If  a question is blank then it only averages checked values that aren't zero.

I'd like all slots to be blank as well until filled out (so we can print a copy for patients)

form template: Q 1-2 get averaged, Q 3,4,5 averaged, Q 6,7,8 averaged, Q9, Q10.

each of those five sections are averaged together if answered, if 0 or unanswered they aren't included in average.

then final # is multiplied by 5....resulting in a # from 1-20.

 

-Besides the (ReferenceError: bIgnoreBlank is not defined
33:Field:Calculate.),

I find that the form doesnt calculate until Q10 is answered (or all the questions from q1-10 are answered)(calculation order is correct)

...if the check boxes are all selected, but then one is changed to zero, that change isn't reflected...

 

Thanks for any help you can provide!!

It's greatly appreciated.

Semper Fi

 

*Your post has been changed because invalid HTML was found in the message body. The invalid HTML has been removed. Please review the message and submit the message when you are satisfied.

 

No clue what may be missing now...

 

TOPICS
How to , PDF forms

Views

3.0K

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 , Jun 12, 2020 Jun 12, 2020

Use bIgnoreBlanks, not bIgnoreBlank

Votes

Translate

Translate
Community Expert ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

When you use the function you list the field names at the first parameter. Like this:

calcAverage(["Field1", "Field2", "Field3"], true, true);

Votes

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 ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

So if this first parameter currently says: 

 

function calcAverage(aFields, bIgnoreBlanks, bIgnoreZeros) {

 

I need to rewrite it to say to this? I'm confusd about where "true, true" is meant to go if we're including "bIgnoreBlanks, bIgnoreZeros"

 

function calcAverage(["License Deal Size1","License Deal Size2", "License Deal Size3", "License Deal Size4", "License Deal Size5", "License Deal Size6", "License Deal Size7", "License Deal Size8", "License Deal Size9", "License Deal Size10", bIgnoreBlanks, bIgnoreZeros) {

Votes

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 Expert ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

Don't change the function definition!

Learn the usage of functions:

https://www.w3schools.com/js/js_functions.asp#:~:text=A%20JavaScript%20function%20is%20a%20block%20o... 

Votes

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 ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

So where is the right place to include what you're saying about the field
names?

--

Votes

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 Expert ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

Like this:

calcAverage(["Field1", "Field2", "Field3"], true, true);

 

You should learn the basics of Javascript coding.

Votes

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 Expert ,
Dec 13, 2020 Dec 13, 2020

Copy link to clipboard

Copied

LATEST

That's when you call the function, from the field's custom calculation script. The function definition itself should not be changed (except for the issue with bIgnoreBlanks).

Votes

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