Skip to main content
Brittany128
Participating Frequently
September 26, 2018
Answered

calculate sum of fields but ignore N/A

  • September 26, 2018
  • 2 replies
  • 5005 views

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;

}

This topic has been closed for replies.
Correct answer gkaiseril

The script you copied was for computing the average or a groups of form fields and excluding the "N/A" value. Since the computation of the average requires the sum divided by the count of the items and division by zero in JavaScript may not result in a numeric value, this division needs to be excluded. And the is what the test for a count of zero does.

This is way just cutting and pasting a script from one PDF form to another without editing can cause a number of problems. As noted one could use the "Field is the ____ of the following fields:" or one could even use the "Simplified field notation" since both methods exclude any non-numeric values from their calculation.

The copied script was provided for the explicate exclusion of the "N/A" items from the count for the computation of the average. The first calculation option uses the total number of fields in the calculation of the average.

This scale and the inclusion of the "N/A" option is very common in consumer product service surveys as well as employee opinion surveys where they may also be grouped for the computation of the average of the respondents.

2 replies

gkaiserilCorrect answer
Inspiring
September 26, 2018

The script you copied was for computing the average or a groups of form fields and excluding the "N/A" value. Since the computation of the average requires the sum divided by the count of the items and division by zero in JavaScript may not result in a numeric value, this division needs to be excluded. And the is what the test for a count of zero does.

This is way just cutting and pasting a script from one PDF form to another without editing can cause a number of problems. As noted one could use the "Field is the ____ of the following fields:" or one could even use the "Simplified field notation" since both methods exclude any non-numeric values from their calculation.

The copied script was provided for the explicate exclusion of the "N/A" items from the count for the computation of the average. The first calculation option uses the total number of fields in the calculation of the average.

This scale and the inclusion of the "N/A" option is very common in consumer product service surveys as well as employee opinion surveys where they may also be grouped for the computation of the average of the respondents.

try67
Community Expert
Community Expert
September 26, 2018

The technical solution to your question is easy. Just change the last line from:

event.value = nSum / nCount;

To:

event.value = nSum;

I would also change these lines:

if (this.getField(aFieldNames).valueAsString != "N/A") {

     nCount++; // increment counter

     nSum += Number(this.getField(aFieldNames).value);

}

To:

var v = this.getField(aFieldNames).valueAsString;

if (!isNaN(Number(v)) nSum+=Number(v);

However, you have a logical problem you need to consider. If it's a sum, what's the difference between selecting "0" and not selecting anything at all? Or selecting "N/A"? You need to re-think your scales.

Brittany128
Participating Frequently
September 26, 2018

Thank you try67, that's already making a lot more sense... please bear with me, there needs to be a "0' for no score, but there also needs to be the "N/A" because some fields are not applicable... however, they then need the total to score to be reduced by the skipped fields so I can show a percentage score (which I will handle with a separate field)

I'm getting a syntax error - missing ) after condition   23: at line 24

// 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++) {

var v = this.getField(aFieldNames).valueAsString;

if (!isNaN(Number(v)) nSum+=Number(v); // 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;

}

try67
Community Expert
Community Expert
September 26, 2018

Sorry, my mistake. Change this:

if (!isNaN(Number(v)) nSum+=Number(v);

To:

if (!isNaN(Number(v))) nSum+=Number(v);