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

Averaging form fields conditionally

tddirks
Registered: May 7 2009
Posts: 13
Answered

I have an Acrobat form that has a 11 x 7 grid of text fields. I want to be able to average the text fields based on the user input in this grid. However, I want to include in the average only those fields in which the user has entered a value. So, for example, if the user only enters a value in three fields of the grid, the average would be the total of those three fields divided by three. Selecting all the fields in the grid and using the "Value is the Average" option of the Calculate tab divides the total of those fields by 77, or the total number of fields in the grid.

I'm assuming that JavaScript would allow me to perform this calculation, though I'm not familiar with JavaScript and have no idea how to create the script for this. In trying to research other scripts similar to what I need, one problem I see is the naming of the fields in the grid. I used the "Place Multiple Fields" option to create the 77 text fields from one field. Thus, I ended up with names of 1Field.0.0 to 1Field.0.10 for the fields in the first row, down to 1Field.6.0 to 1Field.6.10 for the last row. So I'm wondering how a JavaScript For loop can account for this numbering without having to type out the name of every field in the grid in the script.

I also need to find the Maximum and Minimum for only those fields that have user input, but I'm assuming the script for Averaging would be easily modified to handle these calculations.

Any help with the script for this would be greatly appreciated. I'm using Acrobat Windows version 8.1.0, and this is an Acrobat form, not LiveCycle Designer.

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In a LiveCycle form, empty fields return null. But it's also possible for the user to enter spaces so that the field looks empty. Since the fields have a regular naming structure for a 2 dimensional array of fields its easy to write a double loop to create the names and acquire the field values. Then you need a way to detect the non-empty fields. Something like this
var rgEmpty = /^\s*$/;var nCount = 0;var nSum = 0;var nAverage = 0;for(var nRow=0;nRow<7;nRow++){for(var nCol=0;nCol<11;nCol++){var cName = "1Field." + nRow + "." + nCol;var oFld = this.parent.resolveNode(cName);// Test for non Empty fieldsif(!rgEmpty.test(oFld.rawValue)){nCount++;nSum += oFld.rawValue;}}}if(nCount != 0)nAverage = nSum/nCount; nAverage;

This code relies on the "resolveNodes()" function to find the fields, but since I don't know how your form is organized this line may be incorrect. You'll need to fix it up for the specific field organization on your form. The min and max values can be added right into this calculation.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

tddirks
Registered: May 7 2009
Posts: 13
Thanks for the help. I think after looking at this that my best bet is to just redo the form using LiveCycle. Hopefully it will simplify things. Thanks again,

Tom
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Opps, I misread the last line of your post. I took it to mean you had a liveCycle form:( So the code I wrote is for an LC form.

However, you'll need almost exactly the same code regardless of whether its a LiveCycle or an AcroForm. To covert the code AcroForm code do this:

1. Replace "this.parent.resolveNode" with "this.getField"
2. Replace "rawValue" with "value"
3. Replace the last line "Average" with "event.value = Average;"

Those three things are the only difference between an AcroForm and a LiveCycle form

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

tddirks
Registered: May 7 2009
Posts: 13
Thanks. Right now the form is an Acroform, but I thought it might be easier to change to LiveCycle. Here's why. This grid for this form is 7 x 11, as I stated, but I will need to make other sub-grids inside of that grid, and use only the fields for the subgrids. This makes looping through each field in the grid impossible, since some fields in the loop would need to be excluded. The way I understand it, in LiveCycle you can create fields with the same name, then average the values of all instances of that name that are not null. This sounds like the easiest solution for me. I can create different names for the main grid and the sub grid fields. then average those areas by the name I use.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ok for complex form structures the LC for is better. And you are correct that there are tools for handling groups of fields. This is done through what's called a SOM path, and all the fields don't have to be the same name.

Good Luck
Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script