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

How to Get the Adobe Pro Custom Calculation Script to Ignore N/A Responses When Averaging Numerical

Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

In searching through posts it appears I will need to use custom calculation script to ingnore N/A repones in a drop down menu when averaging. This is my first time doing a custom calculation script. I have the drop down Export Values set as 0, 1, 2, 3, and N/A when N/A is selected I would like it excluded from the average. I have 82 drop down menus set-up. Each average will include specific dropdown menus into the average. I have one that should include the following dropdown mensu named: Dropdown7 QMS, Dropdown8 QMS and Dropdown9 QMS. I am hoping someone can help me. 

TOPICS
Acrobat SDK and JavaScript

Views

167

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 , Dec 02, 2021 Dec 02, 2021

PS. There's a small mistake in that code.
Change this line:

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

To this:

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

If you could post a link to where you found it I'll fix it there, too.

Likes

Translate

Translate
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Search the forums for "calcAverage". I posted a function with that name that calculates an average value, while ignoring empty fields, or zero values, or non-numeric values.

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
Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

I was able to get the following script to work:

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;

}

 

calcAverage(["Dropdown7 QMS", "Dropdown8 QMS", "Dropdown9 QMS"], true, true);

 

But for some reason when I tried it for the other two averages it didn't work

 

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;

}

 

calcAverage(["Dropdown7 QMS", "Dropdown8 QMS", "Dropdown9 QMS", "Dropdown10", "Dropdown11", "Dropdown12", "Dropdown13", "Dropdown14", "Dropdown15", "Dropdown16", "Dropdown20", "Dropdown21", "Dropdown22", "Dropdown23", "Dropdown24", "Dropdown25", "Dropdown29", "Dropdown30", "Dropdown31", "Dropdown32", "Dropdown33", "Dropdown34", "Dropdown38", "Dropdown39", "Dropdown40", "Dropdown41", "Dropdown42", "Dropdown43", "Dropdown44", "Dropdown45", "Dropdown46", "Dropdown47", "Dropdown48", "Dropdown49", "Dropdown55", "Dropdown56", "Dropdown57", "Dropdown58", "Dropdown59", "Dropdown60", "Dropdown61", "Dropdown65", "Dropdown66", "Dropdown67", "Dropdown68", "Dropdown69", "Dropdown70", "Dropdown74", "Dropdown75", "Dropdown76", "Dropdown77", "Dropdown78", "Dropdown79"], true, true);

 

And

 

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;

}

 

calcAverage(["Dropdown7 QMS", "Dropdown8 QMS", "Dropdown9 QMS", "Dropdown10", "Dropdown11", "Dropdown12", "Dropdown13", "Dropdown14", "Dropdown15", "Dropdown16", "Dropdown17GS", "Dropdown18GS", "Dropdown19GS", "Dropdown20", "Dropdown21", "Dropdown22", "Dropdown23", "Dropdown24", "Dropdown25", "Dropdown26GS", "Dropdown27GS", "Dropdown28GS", "Dropdown29", "Dropdown30", "Dropdown31", "Dropdown32", "Dropdown33", "Dropdown34", "Dropdown35GS", "Dropdown36GS", "Dropdown37GS", "Dropdown38", "Dropdown39", "Dropdown40", "Dropdown41", "Dropdown42", "Dropdown43", "Dropdown44", "Dropdown45", "Dropdown46", "Dropdown47", "Dropdown48", "Dropdown49", "Dropdown50GS", "Dropdown51GS", "Dropdown52GS", "Dropdown53GS", "Dropdown54GS", "Dropdown55GSa", "Dropdown55", "Dropdown56", "Dropdown57", "Dropdown58", "Dropdown59", "Dropdown60", "Dropdown61", "Dropdown62GS", "Dropdown63GS", "Dropdown64GS", "Dropdown65", "Dropdown66", "Dropdown67", "Dropdown68", "Dropdown69", "Dropdown70", "Dropdown71GS", "Dropdown72GS", "Dropdown73GS", "Dropdown74", "Dropdown75", "Dropdown76", "Dropdown77", "Dropdown78", "Dropdown79", "Dropdown80GS", "Dropdown81GS"], true, true);

 

Not sure why the second two didn't create an average.

 

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
Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

I found this script searching for your other post.

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
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Were any errors reported in the console window?  If a field name is not valid, the field will be skipped and reported in the console. 

 

Also, about the script usage. The function definition is intented to be place in a document script. Then, the "calcAverage" function is called in the calculation script for each text field that will display the average. 

Did you place the script for each different average into different text fields? 

 

When discussing scripts it is important to be explicit and complete. We can't make any assumptions about what you are doing, we have to know exactly. 

 

 

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
Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Sorry wasn't what information to supply. No errors kicked back and each script is placed in a seperate text field. 

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
Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Sorry wasn't sure what information to supply. No errors kicked back and each script is placed in a seperate text field. Thanks again for your assistnace.

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
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Press Ctrl+J and see if there are any errors there.

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
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

PS. There's a small mistake in that code.
Change this line:

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

To this:

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

If you could post a link 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
community guidelines
Community Beginner ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

That did it the scipt is working. Thanks so much for your help. Here is were I found the code. How to Get the Adobe Pro Custom Calculation Script... - Adobe Support Community - 12565965

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
Community Expert ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

That's this thread...

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
Community Beginner ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

LATEST

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
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Your question is almost exactly like this post:

https://community.adobe.com/t5/acrobat-sdk-discussions/how-can-i-average-values-of-only-checked-chec...

 

 

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