Copy link to clipboard
Copied
Hi,
I'm trying to write a custom calculation script for a text box that calculates the average value of the numbers entered into several other text boxes, but also omits from the denominator any text boxes that don't have a value. For simplicity sake, I named the text boxes that will contain the numbers going into the average calculation A1, A2, etc. through A11. So for example if 9 of the text boxes have values, it would be the sum of values divided by 9 instead of 11. I looked at a couple of code examples on this board and couldn't get them to work correctly. Could anyone show me how to calculate this average?
Thank you.
Copy link to clipboard
Copied
Search the forum for calcAverage. It's a function I wrote a while ago that does just that.
Copy link to clipboard
Copied
Search the forum for calcAverage. It's a function I wrote a while ago that does just that.
Copy link to clipboard
Copied
Thank you! I found it and I'm good to go! Appreciate the help.
Peter
Copy link to clipboard
Copied
Where is the information that you wrote? I have the same issue. Thank you...
Copy link to clipboard
Copied
Hi,
This is what I ended up with - the "AB" values are the numbers from the text boxes that I wanted averaged.
// Custom calculation script for Adobe Acrobat
// Calculate average of six numerical values, ignoring blanks and zeros
var values = new Array(
getField("AB1").value,
getField("AB2").value,
getField("AB3").value,
getField("AB4").value,
getField("AB5").value,
getField("AB6").value,
getField("AB7").value,
getField("AB8").value,
getField("AB9").value,
getField("AB10").value,
getField("AB11").value
);
var sum = 0;
var count = 0;
for (var i = 0; i < values.length; i++) {
var value = parseFloat(values[i]);
if (!isNaN(value) && value !== 0) {
sum += value;
count++;
}
}
var average = count === 0 ? 0 : sum / count;
event.value = average;
Copy link to clipboard
Copied
Thank you and where would I put this? in one of these?? I've attached my document as well..... Thank you Peter
Also, do you know if I can do if statements? I'd like to put a letter grade by the class (in a drop down) and be able to have the GPA in the top cell. Also, there may not be 7 courses (some students take 6) and would need the Freshman Fall GPA cell only calculate the grades that are placed in the cell. Thanks.. Ruth
If