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!
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;
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!
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;
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;
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:
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);});
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!
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);});
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:
Copy link to clipboard
Copied
Yes, that's how the code is designed.
Copy link to clipboard
Copied
Thank you for your time and efforts!!
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++;}});
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.
Copy link to clipboard
Copied
That code should only count the checked-boxes. Non-checked boxes will not be included in the total.
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?
Copy link to clipboard
Copied
Yes, and the code above does that.