For dummies: Acrobat average doesn't ignore empty fields.
Copy link to clipboard
Copied
I'm new to JS, however, I'm definitely able to follow instructions.
I made an editable form that aside from text and other fields, it contains 10 questions, each with a number of sub-fields to enter a grading.
I'd like the average of each one of these fields not to count empty fields.
For example:
Right now, if I leave question 1.d blank, it will count it as zero, thus providing me a wrong average.
I've tried to follow other threads, but I'm afraid I didn't do so correctly.
I am using Adobe Acrobat DC - and I am not sure I know how to do Document Level scripts. I tried to add it in Tools> Javascript > Document Javascripts?
I could really use some help.
Thanks a lot.
Copy link to clipboard
Copied
That would be the place for a document level script, but that isn't where I'd expect to put it. It replaces the AVERAGE calculation.
Copy link to clipboard
Copied
By the way, if I was grading that questionnaire, I would absolutely expect to include the non-filled in figures and treat them as zero. I would in fact default all the marks to zero and make them required. Otherwise the grade has no useful point of comparison with other questionnaires.
Copy link to clipboard
Copied
This depends upon how one wants to handle missing data. Is it treated as part of the set of values to be computed or not.
Acrobat choses to treat the missing data as a zero value. If you want to exclude the values with a null value, then you need to write a custom script for such a calculation.
Since you are specifying the number of items to be included in the calculation it might appear that your calculation might not be following your instructions.
For Quality your form has "divide by 4", but if one item is left blank you want to divide by 3. Then the result displayed does not follow the instructions.
If you want to compute the average excluding null valued fields then you need to write a custom calculation script to exclude the null values from the calculation.
Also since the "average" computation will be used several times it might make sense to create a function that takes the field names as a parameter and then computes the average using those fields and returns the result.
A possible document level script is:
// document level function;
function MyAverage(aFields) {
// function to compute average ignoring fields with null value;
// from an array of field names;
var cItem = ""; // field value to test;
var nCount = 0; // count of non empty fields;
var nSum = 0; // sum of non-empty fields;
var nAverage = ""; // average of non-empty fields;
// count and sum non-empty fields;
for(var i = 0; i < aFieldNames.length; i++) {
cItem = this.getField(aFieldNames).valueAsString;
if(cItem != "") {
nCount++;
nSum += Number(cItem);
} // end non-empty field processing;
} // end field name loop processing;
// computer average if count of non-empty fields not zero;
if(nCount > 0) {
nAverage = nSum / nCount;
}
return nAverage;
} // end MyAvarage function;
// end document level function;
Then the custom calculation script for Quality is:
var aFieldNames = new Array("Text.1", "Text.2", "Text.3", "Text.4");
// set field value to result of calculaiton;
event.value = MyAverage(aFieldNames);
The custom calculation for Productivity is:
// document level function;
function MyAverage(aFields) {
// function to compute average ignoring fields with null value;
// from an array of field names;
var cItem = ""; // field value to test;
var nCount = 0; // count of non empty fields;
var nSum = 0; // sum of non-empty fields;
var nAverage = ""; // average of non-empty fields;
// count and sum non-empty fields;
for(var i = 0; i < aFieldNames.length; i++) {
cItem = this.getField(aFieldNames).valueAsString;
if(cItem != "") {
nCount++;
nSum += Number(cItem);
} // end non-empty field processing;
} // end field name loop processing;
// computer average if count of non-empty fields not zero;
if(nCount > 0) {
nAverage = nSum / nCount;
}
return nAverage;
} // end MyAvarage function;
// end document level function;
var aFieldNames = new Array("Text.0", "Text.1", "Text.2", "Text.3");
// set field value to result of calculaiton;
event.value = MyAverage(aFieldNames);
Microsoft Excel has the AVERAGEA that can include missing data entered as a null string.

