Highlighted

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

New Here ,
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...

 

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use bIgnoreBlanks, not bIgnoreBlank

TOPICS
How to, PDF forms

Views

260

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

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

New Here ,
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...

 

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use bIgnoreBlanks, not bIgnoreBlank

TOPICS
How to, PDF forms

Views

261

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
Jun 12, 2020 0
Adobe Community Professional ,
Jun 12, 2020

Copy link to clipboard

Copied

Use bIgnoreBlanks, not bIgnoreBlank

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
Reply
Loading...
Jun 12, 2020 0
New Here ,
Jun 12, 2020

Copy link to clipboard

Copied

Awesome!!

So close... that one little "s"....

Cheers!

Thanks for the help and quick reply

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
Reply
Loading...
Jun 12, 2020 0
Adobe Community Professional ,
Jun 13, 2020

Copy link to clipboard

Copied

The correct code is:

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[i]);
            continue;
        }
        if (f.valueAsString=="" && bIgnoreBlanks) 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;
}

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
Reply
Loading...
Jun 13, 2020 0
New Here ,
Aug 17, 2020

Copy link to clipboard

Copied

Trying to get a Document level script and a field script that ignores blanks but includes 0 in the averages. Any suggestions would be helpful!

-S

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
Reply
Loading...
Aug 17, 2020 0
Most Valuable Participant ,
Aug 17, 2020

Copy link to clipboard

Copied

My function does that... Specify the second parameter as false and the third as true.

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
Reply
Loading...
Aug 17, 2020 0
New Here ,
Aug 17, 2020

Copy link to clipboard

Copied

Tried that and it's still not working. Any other suggestions would be helpful! Thank you!

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
Reply
Loading...
Aug 17, 2020 0
Most Valuable Participant ,
Aug 17, 2020

Copy link to clipboard

Copied

If you want more help please explain what exactly happened when you used the code.

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
Reply
Loading...
Aug 17, 2020 0
New Here ,
Aug 17, 2020

Copy link to clipboard

Copied

When I use the code it does not count the blanks, but it also doesn't include the null (0s) in the average calculation. I posted an example to Google Drive

https://drive.google.com/file/d/1E-4cb6L9Ejoa94T24nitolXPJjQVECsm/view?usp=sharing

 

For example, if I enter 1,2,0,0,2 into the first set of subscores the average I get back is 1.67 instead of 1. It appears that it is counting the number of fields that have a number that is not null. I need it to read the null (0) scores and skip the blanks.

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
Reply
Loading...
Aug 17, 2020 0
Most Valuable Participant ,
Aug 17, 2020

Copy link to clipboard

Copied

Make the file publicly available.

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
Reply
Loading...
Aug 17, 2020 0
Most Valuable Participant ,
Aug 17, 2020

Copy link to clipboard

Copied

I made a mistake in my initial reply to you. The second parameter needs to be true and the third one false, not the other way around. Try it again that way, please.

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
Reply
Loading...
Aug 17, 2020 0
New Here ,
Aug 17, 2020

Copy link to clipboard

Copied

Thank you so much! That worked 🙂

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
Reply
Loading...
Aug 17, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Still have trouble with this form. I believe it's because the formula expects the blanks in a certain order. It's not calculating the subaverages or the averages correctly (not counting all of the fields towards the average and not allowing for blanks within the subaverages or the averages), Any help would be greatly appreciated!! 

 

https://drive.google.com/file/d/1aOWlwS2CknKjsEzHO9f74idofLtAzJ4A/view?usp=sharing 

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
Reply
Loading...
Aug 19, 2020 0
Adobe Community Professional ,
Aug 20, 2020

Copy link to clipboard

Copied

Change the field calculation order.

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
Reply
Loading...
Aug 20, 2020 0
New Here ,
Aug 20, 2020

Copy link to clipboard

Copied

Thanks! I thought of that and I am going to try it!! The Cumulative Average field is having problems if someone takes a score out of any of the other fields that are calculating subscore averages. I went ahead and reversed the calculation order. I had to go back and hit return on several of the field entries to get the calculations to register blanks, 0s, new numbers, etc. I think it will also be a question of instructing the user to go back and hit return on all fields for a subarea if they make any adjustments.

Appreciate everyone's help!!

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
Reply
Loading...
Aug 20, 2020 0
try67 LATEST
Most Valuable Participant ,
Aug 20, 2020

Copy link to clipboard

Copied

That is not needed at all, if the calculation order is set up correctly. And the order in which the fields appear in the array is not important.

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
Reply
Loading...
Aug 20, 2020 0
Most Valuable Participant ,
Jun 12, 2020

Copy link to clipboard

Copied

Sorry about that... If you would point me to where you found it I'll fix it there, too.

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
Reply
Loading...
Jun 12, 2020 0
New Here ,
Jul 01, 2020

Copy link to clipboard

Copied

Can anyone show me were these scripts go into the form would be a great help.

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
Reply
Loading...
Jul 01, 2020 0
Most Valuable Participant ,
Jul 02, 2020

Copy link to clipboard

Copied

The function should be placed as a doc-level script, via Tools - JavaScript - Document JavaScripts.

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
Reply
Loading...
Jul 02, 2020 0