Formula to calculate the average of a number of fields

New Here ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

I am trying to write a formula in java script to calculate the average of 42 numbers, where some of the fields can be empty.

TOPICS
PDF forms

Views

2.1K

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

Adobe Community Professional , Sep 25, 2017 Sep 25, 2017

I've developed a function that allows you to do it easily. The basic code is:

/*

    @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.getFiel

...

Likes

Translate

Translate
Adobe Community Professional ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

If a field is empty, do you want to treat it as zero and include it in the calculation, or ignore it?

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 ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

If the field is empty I would like for it to be ignored.

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 ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

var TotalMoistValue = 0.0;

var TotalMoistCount = 0;

if (this.getField("CRow1").valueAsString !== "") 

{

  TotalMoistValue += this.getField("CRow1").value;

  TotalMoistCount++;                                                                 

}

event.value = TotalMoistValue / TotalMoistCount;

Here is what I am curerently using.

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
Adobe Community Professional ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

I've developed a function that allows you to do it easily. The basic code is:

/*

    @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);

        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, false);

Edit: Fixed some small spelling mistakes in 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
community guidelines
New Here ,
Sep 27, 2017 Sep 27, 2017

Copy link to clipboard

Copied

How do I use this function? Where do I but the field names that the script is pulling from? For example I usually use

this.getField("CRow1").value

I have 42 rows that the information has to pull from and I want to make sure that I use the function properly. I am a novice to javascript, and any help would be appreciated.

Thank you for all your 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
community guidelines
Adobe Community Professional ,
Sep 27, 2017 Sep 27, 2017

Copy link to clipboard

Copied

Put the names of the fields (like "CRow1") inside an array and supply it to the function as the first parameter.

You call the function from the custom calculation script of the field where you want the result to appear.

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

Copy link to clipboard

Copied

LATEST

HtetHtet24836823qww5_0-1655080066358.png

It doesn't work for me. Could someone check my codes and fix them?

HtetHtet24836823qww5_1-1655080105722.png

HtetHtet24836823qww5_2-1655080132236.png

 

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 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Could someone explain much more simple for beginners

I want to make cusomer saisfaction form which has 8 criteria and rate from 1 to 6 (built by Form) that means the value receives from cusomer and at the end will calculate the avarage of given points. Normally the default avarage program works well but I have a "not to be rated" field that mean the criteria should be ignored in the calculation!

I got the code and modified it but still doesn't work . (I made field and in field properties activate calculation and choosed the last option customize calculation and added the code , but doesnt work at all.

could someone help me?

BR

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Did you use the code I shared above? If so, how did you call it, and what were the exact results? Saying "it didn't work" is not very helpful to us. You need to provide more details.

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 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

I tried 2 ways :

1- I made a field and in calculation I choosed the 3rd option "custom calculation script"

2-Second way : I added the calculation function in Javascript tool

and call the function on the custom calculation i the field.

in both ways nothing showed up!

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Change the value of one of the fields and then press Ctrl+J. Are there any error messages in the Console window that opens?

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 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Yes , here it is :

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Sorry about that, it's a small bug in my code. I fixed it in the version above, and if you can just replace all instances of "fields" in it with "aFields" for it to work properly (hopefully)...

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Actually, there were a couple more issues (again with the parameter names), so just copy it from above...

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 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

what is the exact procedure ?

Should I put the calculation it in Document Javascript tool and call it from field or can I put the function and calling same in the customze calculation field ?

BR

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Either way should work. If you have more than one field that you want to use it for, though, then the doc-level approach is better.

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 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

It works now, but it doesn't ignore emty fields or Zero fields !

It takes into account and ruines the calcluation

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
Adobe Community Professional ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Change the last parameter to 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
community guidelines
Community Beginner ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Thank you That was amazing Help.

Have a nice day wish you . Hope other can use this helpful code as well.

BR

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