Skip to main content
itgse
Participating Frequently
Dec 2, 2021 5:29 PM UTC
Answered

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

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. 

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Dec 2, 2021 9:46 PM UTC

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


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.

12 replies

Thom Parker
Community Expert
Community Expert
Dec 2, 2021 6:05 PM UTC
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
Dec 2, 2021 5:36 PM UTC

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.

Translate
itgse
itgseAuthor
Participating Frequently
Dec 2, 2021 6:49 PM UTC

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.

 

Translate
itgse
itgseAuthor
Participating Frequently
Dec 2, 2021 6:51 PM UTC

I found this script searching for your other post.

Translate