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

Averages in Calculations

billh098
Registered: Jun 6 2009
Posts: 47
Answered

I have 3 fields and then another field that is set to Average and those 3 fields are picked. The problem is if I have just one of the 3 fields filled out, the average field still divides it by 3.

How do I get the average of, just the fields I have filled out?
Bill

My Product Information:
Acrobat Pro 8.1.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You will have to write a custom calculation script to count and sum the none-null fields and compute the average.

The exact code will depend upon if you are using LiveCycle Designer or Acrobat to create the forms.

LiveCycle Designer provides a "Sum()" and "Count()" builtin funcitons, see the "Scripting Reference" inlcuded with LiveCycle Designer for more information.

For Acrobat you will could write these funcitons.

George Kaiser

billh098
Registered: Jun 6 2009
Posts: 47
I am using Acobat to create the forms. I dont know what the code would be. I have 3 fields named W, X and Y. Then I have a field named Z for the average field. I have tried a couple of codes I found but they did not work.
Bill
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi billh098,

What code have you tried that didn't work?

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com
billh098
Registered: Jun 6 2009
Posts: 47
I tried to modify this code I found on this forum but could not get it to work. Not sure where to put the first function or how to modify it.
Bill


First, I woul write this function withou field references to make it more universial, and then I would make it a document level funcion so it could be used more than once in a PDF and easily brought into another PDF.

function Avg() {
// computer average of non-empty passed field names
var sum = 0; // sum of fields
var count = 0; // counter for non-blank fields
for(i = 0; i < arguments.length; i++) {
var iField = arguments[i]; // get the i element from passed arguments
var fField = this.getField(arguments[i]).value;
if(!(isNaN(fField)) ) {
// field is a number
if(fField.toString() != '') {
// field is not null or blank
sum += Number(fField); // add to sum
count++; // increment count
} // end not blank
} // end number
} // end loop through arguments
if(count != 0) {
// compute average if there is one or more items to average
return sum / count; // return average
} else {
return '';
} // end no average computed
} // end Avg function


Your custom calculation script then becomes:

event.value = Avg('column.0', 'column.1', 'column.2', 'column.3', 'column.4', 'column.5', 'column.6', 'column.7');
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The function is a document level function and requires no changes. It is placed in the document at the doucment level, [url=http://www.acrobatusers.com/tutorials/2007/07/js_document_scripts]Entering Document Scripts[/url].


You only have to change the string of passed field names to the field names ('column.#') that you need to use for the computation.

For the 'Z' field's 'Custom calculation script' would be:

event.value = Avg('W", 'X', 'Y');

George Kaiser

billh098
Registered: Jun 6 2009
Posts: 47
Tried this yesterday and could not get it to work, did it today and it worked. Guess I held my mouth different today....
Thanks Again,
Bill