calculate sum of fields but ignore N/A
Hi Adobe, I have successfully applied this script to my PDF form, but what I actually need is not an average of the fields... rather a sum of the fields - except where N/A is selected, then it should sum only the fields with numbers in them. In brief, if the form has 10 ratings where the user can choose N/A, 0, 1, 2, 3, 4, 5 - if the selection of one rating is N/A then it should not be included in the final total, which needs to then be calculated as a total of 9 ratings. The final score will be shown as a percentage achieved out of a total.
Please help!
// Sum non-N/A values;
var aFieldNames = new Array("rating1", "rating2");
// counter for non-N/A values;
var nCount = 0;
// variable to sum non-N/A values;
var nSum = 0;
// default value for result if no sum computed;
event.value = 0;
// process array of field names;
for(i = 0; i < aFieldNames.length; i++) {
if(this.getField(aFieldNames).valueAsString != "N/A") {
// field does not have a value of "N/A";
nCount++; // increment counter
nSum += Number(this.getField(aFieldNames).value); // add value to sum
} // end value not N/A;
} // end loop processing one field;
// compute the sum;
if(nCount != 0) {
// non-zero divisor so we can compute the sum;
event.value = nSum / nCount;
}
