1 Reply Latest reply on Feb 15, 2012 10:06 AM by try67

# Need columns to calculate seperately

I have a form with several columns. There are two columns right next to each other that need to calculate independently of each other. Each column has to calculate the total and the average. On the total it was easy, a simple 'vaule is the sum', I put a custom format so if the value is 0 the box is blank. The names of these text boxes are 1-20. The name of the text field is 'Distance ft 1TOTALS' The average is the part I am having trouble with. I have created this script to count each box in the column with a value as 1. name 'text12'

// Custom Calculate script for text field
(function () {

// Initialize the count
var count = 0;

// Loop through the column fields to determine the count of non-blank fields
for (var i = 1; i <= 20; i++) {
if (getField(i).valueAsString !== "") count += 1;
}

// Set the value of this field to the count
event.value = count;

})();

Then I put this script for the average button ('Distance ft 1Row22')

event.value = ( this.getField("Distance ft 1TOTALS ").value /    (this.getField("Text12").value ))

I put the same script , except variable names in the next column. Now column 2 uses some of the values from column 1 in its calculations.

Total- 'Diameter in 2TOTALS' The names of the text boxes used for this calculation are 21-40

Average- 'Diameter in 2Row22'

'Text 14'-

// Custom Calculate script for text field
(function () {

// Initialize the count
var count = 0;

// Loop through the column fields to determine the count of non-blank fields
for (var k = 21; k <= 40; k++) {
if (getField(k).valueAsString !== "") count += 1;
}

// Set the value of this field to the count
event.value = count;

})();

Think it is my script to loop through the columns that is messing me up, but I don't know how to fix it.

Thanks to anyone who can help

• ###### 1. Re: Need columns to calculate seperately

What exactly is the problem? Are you getting error messages? Is the result

not ok?

Couple of small tips:

- Field names are strings. So you should convert your iterators (i and k)

to strings when using them in that method, although it should work either

way.

- Always when dividing by the value of a field you must test first that

it's not empty or zero, to prevent the illegal operation of division by

zero.