# How can I average values of only checked Check Boxes?

New Here ,
Feb 12, 2019 Feb 12, 2019

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

235

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

Likes

13 Replies 13
Feb 12, 2019 Feb 12, 2019

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

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

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:

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

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

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

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!

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

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

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

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:

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

Copied

Yes, that's how the code is designed.

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

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

Copied

Thank you for your time and efforts!!

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

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

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

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.

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

Copied

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

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

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?

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

Copied

LATEST

Yes, and the code above does that.

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