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

How can I average values of only checked Check Boxes?

New Here ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

These check boxes have hidden values (1-5) and we would like to calculate the the average of these values only if those particular Check Boxes are checked. The Check Boxes in each row are named the same. Thanks for any help you can provide!

TOPICS
Acrobat SDK and JavaScript

Views

4.5K

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 , Feb 12, 2019 Feb 12, 2019

So, the checkboxes in each row are mutually exclusive?  And these hidden values are actually the exports for the checkboxes? 

I would add a group name to the existing checkbox name. Assuming the names of the check box rows are "CkRow1" "CkRow2" etc. then the grouped names would be "Grp.CkRow1" "Grp.CkRow2" etc.

This code placed in the calculation script for a text field will produce the average.

var aChecks = this.getField("Grp").getArray();

var sum = 0;

aChecks.forEach(function(a){sum+=a.value;});

e

...

Votes

Translate

Translate
Community Expert ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

So, the checkboxes in each row are mutually exclusive?  And these hidden values are actually the exports for the checkboxes? 

I would add a group name to the existing checkbox name. Assuming the names of the check box rows are "CkRow1" "CkRow2" etc. then the grouped names would be "Grp.CkRow1" "Grp.CkRow2" etc.

This code placed in the calculation script for a text field will produce the average.

var aChecks = this.getField("Grp").getArray();

var sum = 0;

aChecks.forEach(function(a){sum+=a.value;});

event.value = sum/aChecks.length;

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

Votes

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 ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

Yes, all Check Boxes in row1 for example are named CheckBox10, row2 CheckBox20, etc. The hidden values are the export values.

Thank you for the script! It works for the most part.

But I ran into this interesting problem now. When all boxes are checked (one box from all rows), the average value returned is correct. But when even one Check Box is left out, we get the following error:

Votes

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 ,
Feb 12, 2019 Feb 12, 2019

Copy link to clipboard

Copied

I did leave something out.  The unselected value of a checkbox or radiobutton is "Off", not a number, i.e. NaN

So the summation code needs to be modified to add "Off" as zero.

aChecks.forEach(function(a){sum+=isNaN(a)?0:Number(a.value);});

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

Votes

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Interesting. Now it does not return anything:

Script on the textbox:

var aChecks = this.getField("Group").getArray();

var sum = 0;

aChecks.forEach(function(a){sum+=isNaN(a)?0:Number(a.value);});

event.value = sum/aChecks.length;

------

Any idea? Thanks!

Votes

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Did you do any debug? Are errors reported in the Console window?

And yes, there is an obvious bug. Replace the summation line with this

aChecks.forEach(function(a){sum+=isNaN(a.value)?0:Number(a.value);});

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

Votes

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

That fixed the NaN error! but when only one box is checked, it still seems to average, where it should be just returning that one box value, see this:

Votes

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Yes, that's how the code is designed.

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

Votes

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 ,
Feb 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

Thank you for your time and efforts!!

Votes

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 ,
Feb 14, 2019 Feb 14, 2019

Copy link to clipboard

Copied

If you want the average of only the fields that include a checkmark, then the code needs to count the checked rows. This can be done at the same time the sum is calculated.

var sum =0;

var cnt = 0;

aChecks.forEach(function(a){if(!isNaN(a.value){sum+=Number(a.value);cnt++;}});

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

Votes

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 ,
Oct 29, 2020 Oct 29, 2020

Copy link to clipboard

Copied

Good day, I have been looking for averaging values in the checkbox that have check on it and I found this thread. This is very helpful. My concern is like the last concern of the thread starter, I would like the calculation to average those checkbox with check and disregard in the calculation those do not have check. The last script which is 

var sum =0;

var cnt = 0;

aChecks.forEach(function(a){if(!isNaN(a.value){sum+=Number(a.value);cnt++;}});

seems not working. May I ask help regarding this. 

Votes

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 ,
Oct 29, 2020 Oct 29, 2020

Copy link to clipboard

Copied

That code should only count the checked-boxes. Non-checked boxes will not be included in the total.

Votes

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 ,
Oct 29, 2020 Oct 29, 2020

Copy link to clipboard

Copied

Yes, its valur won't be included in the computation for average.

For example, I checked 5 boxes with each boxes having different value so TOTAL VALUE/5=Average

And if I checked 4 boxes in the 5 checkbox arrays the average should be computed as TOTAL VALUE/4=Average instead of 5.

Would that be possible?

Votes

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 ,
Oct 29, 2020 Oct 29, 2020

Copy link to clipboard

Copied

Yes, and the code above does that.

Votes

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 ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

I'm running into an issue with the custom calculation script. I've named each row "CkRow1" "CkRow2" etc. And the grouped names "Grp.CkRow1" "Grp.CkRow2" etc. And I've used this custom calculation script:

 

var aChecks = this.getField("Grp").getArray();

var sum = 0;

aChecks.forEach(function(a){sum+=isNaN(a.value)?0:Number(a.value);});

event.value = sum/aChecks.length;

 

But it's not calculating anything, just a zero. Any suggestions on what I'm doing wrong?

Votes

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 ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

apparently all the input values are either Not-A-Number, or 0.

What are the export values for the checkboxes?

 

 

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

Votes

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 ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

Oops, you're correct. I didn't have numbers in the export values. Now it's calculating, but I have that same issue that was above - showing the 0.333333333333 instead of averaging. I tried switching the the script to this:

 

var aChecks = this.getField("Grp").getArray();

var sum = 0;

aChecks.forEach(function(a){if(!isNaN(a.value){sum+=Number(a.value);cnt++;}});

event.value = sum/aChecks.length;

 

But I get this error: "SyntaxError: missing ) after condition 5: at line 6

Votes

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 ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

So what value should be shown instead of  0.333333333?

 

And fix the syntax error. Parentheses and brackets must be balanced. Find the place that is missing a closing parentheses and add it back in.

 

But you didn't need to change the code. The first script you posted was find.

 

 

 

 

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

Votes

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 ,
Nov 29, 2021 Nov 29, 2021

Copy link to clipboard

Copied

I thought I needed to average just the checked boxes (this is for a client of mine)... But what I actually need to average is this: (see screenshot)

The numbers selected in the checkboxes, but averaged to how many checkboxes are checked. If that makes sense - this is confusing to me. I have another screenshot attached to show the numbers checked (5, 4, 5, 4, 4) total 22, divided by 5 = 4.4 (average). The N/A's don't get included in that average. I don't know if there is a way to do this, I have very little experience in all the bells & whistles of Acrobat.

Votes

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 ,
Nov 30, 2021 Nov 30, 2021

Copy link to clipboard

Copied

So what is the export value of the N/A column?  You need to know this to write a script that doesn't include it in the calculation.  Is it NA? If it is, and all the other columns export numbers, then the script that counts entries that are !NaN is the one you want. Because "NA" is Not-A-Number. So you only want to sum entries that Are A Number. 

Here is the corrected script. 

 

var aChecks = this.getField("Grp").getArray();
var sum = 0;
aChecks.forEach(function(a){if(!isNaN(a.value)){sum+=Number(a.value);cnt++;}});
event.value = sum/aChecks.length;

 

 

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

Votes

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 ,
Nov 30, 2021 Nov 30, 2021

Copy link to clipboard

Copied

Oh awesome, I think this is working!! Thank you sooooo much!! Just one more thing, is there any way to round off the total? For instance, on the sample attached - could we get it to show 3.17 instead of the 3.6666666?

Screen Shot 2021-11-30 at 11.45.51 AM.png

 

Votes

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 ,
Nov 30, 2021 Nov 30, 2021

Copy link to clipboard

Copied

Actually, I think I got that figured out... it was the formatting I was using with the decimal points.

THANK YOU so much for your help on this, I would have been lost without your help! 😄

Votes

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 ,
Nov 30, 2021 Nov 30, 2021

Copy link to clipboard

Copied

Oh my gosh, it's me again! LOL

 

I have just about everything working great now, but I'm wondering how I would get this done:

 

In the total score, I need to leave out the NA boxes in the calculation. So say for instance, 4 "number" boxes are checked and the other 2 checked are "NA", I need to calculate only the average of the 4 number boxes checked & total of those 4 checked boxes.

In this instance attached: the total of the checked boxes is "8" (3, 2, 2, 1) - I just need the average "total" of 8 divided by 4 checked boxes, the NA ones won't count.

 

I hope this makes sense how I'm explaining it 😉

Votes

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

Here is the corrected script. It's all about counting the boxes you want to use:

 

var aChecks = this.getField("Grp").getArray();
var sum = 0;
var cnt = 0;
aChecks.forEach(function(a){if(!isNaN(a.value)){sum+=Number(a.value);cnt++;}});
event.value = sum/cnt;
Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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

Thank you so much! The calculations work perfectly now, but I get an error message that the value entered does not match the format of the field. I'm just going to attached the document instead of screenshots, it would make more sense than my trying to explain. That is, if you wouldn't mind taking a look at it for me.

Votes

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