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

# average ignoring empty fields & zero values

Participant ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

hi, in my form i want to calculate average in 40 text fields. fields names are AB-1, AB-2, AB-3, etc up to AB-40. i have found bellow code that really works but only issue is that code not ignoring zero values. so can u pls help me edit bellow code to ignore zero. thanks..

// Initialize variables

var num = 0;

var sum = 0;

// Loop through the input fields

for (var i = 1; i < 5; i++) {

var f = getField("AB-" + i);

if (f.valueAsString) {

// increment the non-blank field counter

num++;

// add the field value to the running total

sum += +f.value;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

TOPICS
Acrobat SDK and JavaScript , Windows

Views

4.4K

Report

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

## 1 Correct answer

Community Expert , May 05, 2017 May 05, 2017

Use this:

// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 40; i++) {

v = +getField("AB-" + i).value;

if (v !== 0) {

// increment the non-blank/zero field counter

num++;

// add the field value to the running total

sum += v;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

23 Replies 23
LEGEND ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

You could change it to something like the following:

// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 40; i++) {

v = +getField("AB-" + i).value;

if (v !== 0) {

// increment the non-blank/zero field counter

num++;

// add the field value to the running total

sum += +f.value;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

Report

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

Copy link to clipboard

Copied

hi, thanks a lot for your code but answer giving zero & not calculating correctly..

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 Expert ,
May 05, 2017 May 05, 2017

Copy link to clipboard

Copied

Use this:

// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 40; i++) {

v = +getField("AB-" + i).value;

if (v !== 0) {

// increment the non-blank/zero field counter

num++;

// add the field value to the running total

sum += v;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

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

Copy link to clipboard

Copied

Hi, I'm sure someone can help me.

I have 15  fields all with different names.

I would like another field (box16) to calculate the average of all, but only of the fields which have a number inserted.

So I would like "box16" to not include empty fields when calculating the average.

Thank you.

I'm not very good at this, so I would appreciate a very simple explanation.

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

Copy link to clipboard

Copied

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

Copy link to clipboard

Copied

Here is one way to do it, Put this code in the Custom Calculation for the Average field, "box16" :

``````var aFieldNameList = ["Text1","Text2","Text3","Text4"];
var nSum = 0, nCnt = 0, oDoc=this;
aFieldNameList.forEach(function(a){if(((v=oDoc.getField(a).valueAsString) !="") && !isNaN(v)){nSum+=v;nCnt++;}});
event.value = nCnt?nSum/nCnt:"";``````

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

Hi, can you please help me with this....since your previous script worked perfectly for me.

This time the fields are not AB-1, AB-2, AB-3 like in the previous example of your script.

Instead they are A1, B1, C1, D1 ( practically  all the letters from A to Z  followed by the number 1 )

Thank you

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

Copy link to clipboard

Copied

what script are you talking about?

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

This one:

// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 40; i++) {

v = +getField("AB-" + i).value;

if (v !== 0) {

// increment the non-blank/zero field counter

num++;

// add the field value to the running total

sum += v;

}

}

// Calculate the average

if (num) {

event.value = sum / num;

} else {

// All fields are empty, so set to blank

event.value = "";

}

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

Copy link to clipboard

Copied

Incrementing letters is different from incrementing numbers.  Each letter has a character code, so you need to increment the character code, then convert it to a letter.

The character code for "A" is 65 or 0x41 in Hexidecimal.

Character codes are converted into actual characters using
String.fromCharCode();

So here's a modification of the loop portion of your script:

``````var nCharA = 65;
for (i = 0; i < 40; i++) {
var strName = String.fromCharCode(nCharA + i) + "1";
v = +getField(strName).value;``````

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

Thanks. But that is very difficult to understand.

Is this loop portion including all the letters from A to Z ?

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

Copy link to clipboard

Copied

Yes, but also other characters... Not sure why it's going up to 40 instead of 26

To get only the letters change 40 to 26 in the code above.

To verify it you can run this code from the console:

``````var nCharA = 65;
for (i = 0; i < 26; i++) {
console.println(String.fromCharCode(nCharA + i));
}``````

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

Copy link to clipboard

Copied

I really appreciate your time. I understand that incrementing letters is not the same as numbers.

Maybe I'm asking too much but could someone please post the full Custom calculation script I need ?

I have about 1000 text fields in my pdf , so it's getting a bit confusing.

Thanks.

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

Copy link to clipboard

Copied

Here's the loop for calculating the sum and number of non-empty entries. If you are writing code for a large form, then you need to understand the scripting, or hire a professional. For example, knowing that the loop should be set to a limit of 26 should be a given. It's your form, you designed the fields.

``````// Initialize variables
var i, v, num = 0, sum = 0;
// Loop through the input fields
var nCharA = 65;
for (i = 0; i < 26; i++) {
var strName = String.fromCharCode(nCharA + i) + "1";
v = Number(getField(strName).value);
if (v !== 0) {
// increment the non-blank/zero field counter
num++;
// add the field value to the running total
sum += v;
}
}``````

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

Copied the script, but it doesn't do anything.

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

Copy link to clipboard

Copied

This is only part of the script. The rest you already have, i.e. the part the calculates the average and assigns it to the field value.

Here's the bottom part of the script.

``````// Calculate the average
if (num) {
event.value = sum / num;
} else {
// All fields are empty, so set to blank
event.value = "";
}``````

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

That's exactly what I did, but it does nothing.

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

Copy link to clipboard

Copied

What's reported in the console window?

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: getField(strName) is null
7:Field:Calculate

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

Copy link to clipboard

Copied

There is no "f" in the script, so you've got an error in some other script.

This is the error in the script,

TypeError: getField(strName) is null
7:Field:Calculate

and you are correct that one of the field names that has the problem. If you've already changed the problem name to match the pattern, then all is good.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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

Copy link to clipboard

Copied

Changed the end fields name and it seems to be working now.  I'll let you know.

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

Copy link to clipboard

Copied

Worked perfectly. Thank you.

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 ,
Mar 21, 2023 Mar 21, 2023

Copy link to clipboard

Copied

LATEST

Thank you so much from the United States @Bernd Alheit, I'm completely unfamiliar with coding but got this to work on a very important document. I really appreciate your help!