These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculation Help

HR Lady4
Registered: Nov 2 2010
Posts: 25

I'm in need of some assistance. I'm unsure how to go about a calculation.
 
I have a form that is adding up a series of scores, but needs to skip and not average those fields that are missing a figure.
 
Example: Someone can get a score of 1-5, so let's say:
1. 5
2. 3
3. (Blank)
4. 4
Total: 12
So the Total Score would be 12, but I only want the average to count on the 3 spaces where an acutal figure is placed. So I want the Average Score to say "4".
How can I program it so that it will only average the score where an acutal number between 1-5 is placed?

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You will have to use JavaScript. The script would get the value of each field (using valueAsString), check to see if it's blank, and only use the fields that are not blank in the calculation. If you need more help with the script, post again.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You clearly do not want to use the provided internal Acrobat functions for Avg since it counts blank fields as an item to be used in the calculation average.

There have been a number or post about how to write functions to perform sum, count, and average with special test to eliminate non-numeric and empty data. The code also knows how not to divide by zero.

George Kaiser

HR Lady4
Registered: Nov 2 2010
Posts: 25
Yes, I would like assistance with the script.


I have 26 Text fields.
Each field is named "1"-"26". Each of those fields have the ability to add a number from 1-5 in it.
At the bottom I have a text field named "Total Score" that adds up all the numbers from fields 1-26.
Under the "Total Score" field, I have a text field named "Average Score". I want the "average score" to add the fields from the 1-26 text boxes but only average those that have a number from 1-5 in them.
So.....in
"1" I put a value of 4
"2" I put a value of 5
"3" I leave blank
"4" I put a value of 3
"Total Score" would be 12
"Average Score" would be 4, because it will only average the 3 fields versus 4 fields because text field "3" is blank.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Here is a custom Calculate script you can use for the average score field:

  1. // Custom Calculate script
  2. (function () {
  3.  
  4. // Initialize the variables
  5. var val, sum = 0, count = 0;
  6.  
  7. // Loop through the 26 input fields...
  8. for (var i = 1; i < 27; i++) {
  9.  
  10. // Get the field value as a string
  11. val = getField(i).valueAsString;
  12.  
  13. // If the field is not blank...
  14. if (val !== "") {
  15. sum += +val; // ...convert to a number and add to the running sum
  16. count += 1; // ... and increment the non-blank field counter
  17. }
  18. }
  19.  
  20. // Set this field value to the average value if there is at least one non-blank field
  21. // otherwise blank it.
  22. event.value = count ? sum / count : "";
  23.  
  24. })();
I didn't test it, but it looks like it should work.
HR Lady4
Registered: Nov 2 2010
Posts: 25
I inserted the script into the custom script and nothing happens. Let me try clarifying since it can be hard to explain.

I have a form with 26 text fields. The text fields are named:
"1"
"2"
"3"
and so on...through "26"
Particpants can place a figure between 1-5 in the texts fields.
At the bottom of the form, I have a text field named "Total Score". This adds up the amounts from text fields "1-26"
Below the "Total Score" I have a text field called "Average Score".
I want "Average Score" to average the figures from text fields 1-26. However if text field "3" is left blank, I don't want the "average score" to count against the blank text field.

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I would create the following document level funcitons:

// start document level functions

function SumFields(aNames) {
// sum fields skipping non-numeric values
var nTotal = 0; // total of field values
var nValue; // each field value
// loop through fields
for (i = 0; i < aNames.length; i++) {
nValue = this.getField(aNames[i]).valueAsString;
// skip null, blank or not a number
if(nValue != "" & nValue != " " & isNaN(nValue) == false) {
nTotal += Number(nValue); // add value to sum
} // end if
} // end loop
return nTotal;
} // end SumFields

function CountFields(aNames) {
// count fields skipping non-numberic values
var nCount = 0;
var nValue;
for (i = 0; i < aNames.length; i++) {
nValue = this.getField(aNames[i]).valueAsString;
// skip null, blank or not a number
if(nValue != "" & nValue != " " & isNaN(nValue) == false) {
nCount++; // increment count
} // end if
} // end loop
return nCount;
} // end CountFields

function AverageFields(aNames) {
// average fields skipping non-numeric values
var nAvg = "";
var nCount = CountFields(aNames);
if (nCount != 0) {
nTotal = SumFields(aNames);
nAvg = nTotal / nCount;
} // end if
return nAvg;
} // end AverageFields

// end document level functions

For the 'Total Score' field use the following custom calculation scirpt:

(function () {
var aFieldNames = new Array("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", 12", "13",
"14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26"); // field names to sum
event.value = SumFields(aFieldNames);
}) ();

For the "Average Score" field use the following custom calculation script:

(function () {
var aFieldNames = new Array("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", 12", "13",
"14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26"); // field names to average
event.value = AverageFields(aFieldNames);
}) ();

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
In which field did you place the code. This was intended for the "Average Score" field.
HR Lady4
Registered: Nov 2 2010
Posts: 25
Hi George,

When I placed the following scripts, I get an error code "missing)after argument list 3: at line 4 for the "Total Score" and "Average Score".




For the 'Total Score' field use the following custom calculation scirpt:

(function () {
var aFieldNames = new Array("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", 12", "13",
"14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26"); // field names to sum
event.value = SumFields(aFieldNames);
}) ();

For the "Average Score" field use the following custom calculation script:

(function () {
var aFieldNames = new Array("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", 12", "13",
"14", "15", "16", "17", "18", "19", "20", "21",
"22", "23", "24", "25", "26"); // field names to average
event.value = AverageFields(aFieldNames);
}) ();

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
For the Total Score

(function() {
var aFieldNames = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26"); // field names to sum;
event.value = SumFields(aFieldNames);
}) ();

for the Average Score

(function () {
var aFieldNames = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26"); // field names to sum;
event.value = AverageFields(aFieldNames);
}) ();

Be very careful when cutting and pasting. It is very easy to have a smart character replacing a simple ASCII text character or having a line feed or carriage return inserted where it does not belong.




George Kaiser

HR Lady4
Registered: Nov 2 2010
Posts: 25
Thank you. That worked!