Highlighted

How can I average values of only checked Check Boxes?

New Here ,
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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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;

TOPICS
Acrobat SDK and JavaScript

Views

163

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

How can I average values of only checked Check Boxes?

New Here ,
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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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;

TOPICS
Acrobat SDK and JavaScript

Views

164

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
Feb 12, 2019 0
Adobe Community Professional ,
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;

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
Reply
Loading...
Feb 12, 2019 1
New Here ,
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:

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
Reply
Loading...
Feb 12, 2019 0
Adobe Community Professional ,
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);});

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
Reply
Loading...
Feb 12, 2019 1
New Here ,
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!

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
Reply
Loading...
Feb 13, 2019 0
Adobe Community Professional ,
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);});

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
Reply
Loading...
Feb 13, 2019 0
New Here ,
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:

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
Reply
Loading...
Feb 13, 2019 0
Adobe Community Professional ,
Feb 13, 2019

Copy link to clipboard

Copied

Yes, that's how the code is designed.

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
Reply
Loading...
Feb 13, 2019 0
New Here ,
Feb 13, 2019

Copy link to clipboard

Copied

Thank you for your time and efforts!!

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
Reply
Loading...
Feb 13, 2019 0
Adobe Community Professional ,
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++;}});

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
Reply
Loading...
Feb 14, 2019 0
New Here ,
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. 

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
Reply
Loading...
Oct 29, 2020 0
Most Valuable Participant ,
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.

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
Reply
Loading...
Oct 29, 2020 0
New Here ,
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?

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
Reply
Loading...
Oct 29, 2020 0
try67 LATEST
Most Valuable Participant ,
Oct 29, 2020

Copy link to clipboard

Copied

Yes, and the code above does that.

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
Reply
Loading...
Oct 29, 2020 0