• Formula to calculate the average of a number of fi...

# Formula to calculate the average of a number of fields

New Here ,
Sep 25, 2017 Sep 25, 2017

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

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 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

19 Replies 19
Sep 25, 2017 Sep 25, 2017

Copied

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 25, 2017 Sep 25, 2017

Copied

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

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 25, 2017 Sep 25, 2017

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 25, 2017 Sep 25, 2017

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 27, 2017 Sep 27, 2017

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 27, 2017 Sep 27, 2017

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 12, 2022 Jun 12, 2022

Copied

LATEST

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

Likes

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

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

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

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

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

Copied

Yes , here it is :

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

Copied

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

Likes

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

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

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

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

Copied

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

It takes into account and ruines the calcluation

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2018 Dec 17, 2018

Copied

Change the last parameter to true.

Likes

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

Copied

Thank you That was amazing Help.

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

BR

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources