Skip to main content
admii17053160
Participating Frequently
February 12, 2019
Answered

How can I average values of only checked Check Boxes?

  • February 12, 2019
  • 1 reply
  • 8840 views

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!

This topic has been closed for replies.
Correct answer Thom Parker

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;

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 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;});

event.value = sum/aChecks.length;

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
admii17053160
Participating Frequently
February 12, 2019

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:

Thom Parker
Community Expert
Community Expert
December 2, 2021

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 😉


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 PDFScriptingUse the Acrobat JavaScript Reference early and often