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

Creating functions in an Acrobat form that were in the original excel file

lisaew
Registered: Sep 16 2011
Posts: 2

Hi,
I do not know anything about JavaScript, but need to learn enough to create some calculation scripts for an excel spreadsheet that converted into a pdf form with fields.
 
I made one version from the wizard and that didn't work(I thought the functions would transfer over) and then created another one individually by hand. Is one better than the other?
 
There are 12 columns of 30 fields. Each column needs to be added. I was able to accomplish this by adding a 31st field to each column called 130SUM, 3160SUM etc.. at bottom of each column and adding a predefined calculation: In Text field properties/Calculate Tab I used the predefined " Value is the Sum+" then picked the individual fields in each column to add.
 
I then created another " Value is the Sum+" field property that calculates the sum of all of the 12 SUM fields and picked those SUM from the list. This all works great!
 
However, I have another field that needs to calculate the count of the fields that have numbers entered into them of the 360 fields. I HAVE NO IDEA HOW TO DO THIS???
 
Finally, I need to get the average of all the amounts entered into the 360 fields (not all the fields are always used). I used the predefined " Value is the /average" which would work fine, but it adds all 360 fields and not all of them are used, but it still adds "0" to the total and then divides field by 360.
 
So I believe I will need to write a custom script for the "count" and the average of fields entered, and whether I need to write a custom script for the others as well since they are affiliated with the other two equations. And when I write these scripts, do I need to list out each of the 360 fields, or can I use a shortcut like in Excel its =Average(B8:M37).
 
I did look at other forums in here, but really not sure where I should start to get this task accomplished. Any help or guidance would be appreciated!
 
I need to know where I can find information on what I need to learn without starting from square one in Acrobat Javascript Calculating for Dummies. I hope I articulated my questions well. Thanks in advance for any help!
 

L.W.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Here you'll find some good info about mathematical functions available in JS:
http://w3schools.com/jsref/jsref_obj_math.asp

To calculate the average you can use a script like this (let's say you have 30 fields and the sum is saved in a field called "130SUM"):
event.value = getField("130SUM").value / 30;
Your other question, about counting the number of fields that have values in them is more complex.
Basically, it all depends on how you named your fields. If they are named consistently, like "Field1" to "Field360", then it becomes easier. In that case you can use something like this:
  1. var count = 0;
  2. for (var i=1; i<=360; i++) {
  3. var f = getField("Field"+i);
  4. if (f!=null && f.value!="")
  5. count++;
  6. }
  7. event.value = count;

This will be the custom calculation code of your counting field.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
PS - you should be aware that in Acrobat the field calculation order is not set automatically, like in Excel, so you have to make sure that it's set correctly (for example, that the average field is calculated after the total field).
You can edit it via Forms - Edit Forms - Set Field Calculation Order...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
hi,

If fields are named using the Adobe form field naming convention it should be easier :

- fields must be named as "amount.0", "amount.1", "amount.2", and so on.
This can be done automaticaly using "Place Multiple fields" (Right-clic upon any form field with the Select Object Tool: http://help.adobe.com/en_US/acrobat/pro/using/WS8C2DDC1C-C174-4ad1-893F-B14A1C0289B4.php )

- then, in the "SUM" field you simply have to select the average (or the sum, etc.) of the (virtual) field "amount" available in the form field list.

This cause the SUM field to add all values caming from field named "amount.x".

See that screenshot: http://pix.am/ccb0/

;-)
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
How do you want to count and sum your fields?

Do you want to include null or empty fields?

Do you want to include zero value fields?

There is the feature to not only pick the fields to sum, but you can also average them if you want a null or empty field to be included as one of the fields with a value of zero.

If you want to exclude null or empty fields, not zero values, you will need to write a special function to skip the empty fields from the count of the fields.

There are a number of on demand videos and articles on this site that discuss forms, calculations, and JavaScript.

You might even consider using the OpenOffice.org suite, as the Write program can create form fields and export them to PDF.

If you use hierarchical field names:

// variable name for top most hierarchical field
var cTopName = "Text";

// sum hierarchical fields
function SumHfield(cTopName) {
// get top field object
var oFields = this.getField(cTopName);
// convert to an array
var aFields = oFields.getArray();
// variable for sum
var nSum = 0;
// sum field values for members of array
for ( i = 0; i < aFields.length; i++) {
nSum += Number(aFields[i].value);
}
return nSum;
}

// count the hierarchical fields
function CountHfield(cTopName) {
// get top field object
var oFields = this.getField(cTopName);
// convert to an array
var aFields = oFields.getArray();
// variable for count
var nCount = 0;
// sum field values for members of array
for ( i = 0; i < aFields.length; i++) {
// add test for null adjustment if needed
nCount++;
}
return nCount;
}

// average the hierarchical fields using the sum and count functions
function AvgHfield(cTopName) {
// variable for average
var nAvg = 0;
// variable for the count of fields
var nCount = CountHfield(cTopName);
if(nCount != 0) {
nAvg = SumHfield(cTopName) / nCount;
}
return nAvg;
}

// set the field value using the functions
event.value = AvgHfield(cTopName);

The above code will also automatically adjust for added or deleted fields as long as the hierarchical name structure is maintained.

If you place the functions at the document level, you only need to call the AvgHfield function with the appropriate field name to obtain the average of the fields.

FormCalc in LiveCycle Designer has functions that can dynamically sum, and count fields with the same name.

George Kaiser