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

How to calculate a total of ramdom fields?

isaanderson
Registered: Aug 11 2008
Posts: 72

I have a form that I am doing for a client where I need to calculate a total as follows:
 
I have a column with 9 aspects that need to be rated via radio buttons. Then I have a row with 5 radio buttons: Each of the radio buttons can get a value of (5) (4) (3) (2) (1) by selecting the appropriate radio button for each of the aspects. So I end up with 9 rows of 5 radio buttons.
 
The calculation I need code for is for calculating the grand total of all those radio buttons that are selected and divide the total by 45. All of this needs to be done in only "one" field at the end called "total" by hitting a button labeled "calculate"
 
I am using acrobat pro 8
 
PLEASE HELP ME!!

My Product Information:
Acrobat Pro 8.1.7, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
The value of a check box group or field or a radio button group or field when not selected is the string 'Off'. So if no single radio button in a row is selected the value for the group of radio butt ions will be the string "Off". You will have to test for this value and then use zero for the value of this row.

So if I have 9 rows of radio buttons with a name template of "Aspect.#" where '#' is the row number starting at zero and going to 8. Each row of buttons has 5 buttons that have a button with a value from 1 through 5. For your total field, you can use one of the following scripts for the custom calculation script:

// using hierarchical field names
// variable for value of row
var nValue = 0;
// variable for the sum of the rows
var nSum = 0
// locate the top level radio button name
var oAspect = this.getField('Aspect');
// convert top level field object to an array of fields
var aAspect = oAspect.getArray();
// loop through each index (row) of aspect array
for (i = 0; i < aAspect.length; i++) {
// get the value of row i
nValue = aAspect[i].value;
// adjust for no selection
if (nValue == "Off") nValue = 0;
// add the value for the row i
nSum += nValue;
} // end loop trough fields
// compute the value and populate result field
event.value = nSum / 45;

// using field names in an array
// variable for value of row
var nValue = 0;
// variable for the sum of the rows
var nSum = 0
// make array of field names
var aAspect = new Array("Aspect.0", "Aspect.1", "Aspect.2",
"Aspect.3", "Aspect.4", "Aspect.5", "Aspect.6", "Aspect.7",
"Aspect.8");
// loop through fields (row) of aspect array
for (i = 0; i < aAspect.length; i++) {
// get the value of row i
nValue = this.getField(aAspect[i]).value;
// adjust for no selection
if (nValue == "Off") nValue = 0;
// add the value for the row i
nSum += nValue;
} // end loop trough fields
// compute the value and populate result field
event.value = nSum / 45;

George Kaiser

isaanderson
Registered: Aug 11 2008
Posts: 72
George,
But I want the calculation to appear on a field called "total" AFTER the user clicks on a button called "calculate" so I need to put the code in the button but need the result of the calculation to appear on the "total" text field.

Can you please add to the code so I can do that?

Thanks in advance.
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
Change the last line to:

this.getField("total").value = nSum / 45;

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

isaanderson
Registered: Aug 11 2008
Posts: 72
Thanks Try67 and George! I used both of your suggestions but changed them slightly. I actually made a mistake because I was supposed to divide by 9 not 45, so this is what I ended up with:

(function () {
// using field names in an array
// variable for value of row
var nValue = 0;
// variable for the sum of the rows
var nSum = 0;

// make array of field names
var aAspect = new Array("attendance", "knowledge", "dress", "attitude", "quality", "quantity", "service", "safety", "team");

// loop through fields (row) of aspect array
for (i = 0; i < 9; i++) {// get the value of row i
nValue = this.getField(aAspect[i]).value;


//adjust for no selection
if (nValue == "Off") nValue = 0;

// add the value for the row i
nSum += nValue;
} // end loop

// compute the value and populate result field
this.getField("total").value = nSum/9;


})();

IT WORKS EXACTLY LIKE I WANT, BUT...

If someone does not check one of the checkboxes, how can I get a warning to appear letting them know that they missed one?

Thanks.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You check the value of the check box group of 'Off', that is why I included a test for the value of row being 'Off' and forcing the value for that row to zero. You can change the block of code for the zero value to also include a warning message.

George Kaiser