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

Calculating fields in Acrobat

eliannah
Registered: Jun 13 2008
Posts: 17
Answered

Hi,
I created a simple javascript that will add four fields. The first time I did it I just added the text fields but if the first field was blank then I got a string of numbers instead of the calculation adding the blank field so I specified the fields as numbers.

My problem is that sometimes the form needs to be printed with no fields filled in so if the field is blank the total needs to stay blank. I figured out how to do that but then I realized that sometimes the field has a zero value and so the total needs to be zero.

Is there a way to add these fields and get a zero if the true value is a zero and for it to remain blank if there is no data in the form fields and to avoid the problem with text strings?

Thanks for your help.

My Product Information:
Acrobat Pro 8.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Yes, in your code you need to check if the other fields have any values filled in (ie not null or an empty string). If so, then you can use their values for the total. To be on the safe side, you can make sure the values are treated as numbers (and not strings) by placing each value inside the Number constructor, like so:
event.value = Number(value1)+Number(value2)+Number(value3)+Number(value4)

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

eliannah
Registered: Jun 13 2008
Posts: 17
Thanks but my code is exactly like that and I added an if statement to put a blank if the data is less than zero but since the default number is zero it doesn't blank out the field. It is because the number default is zero and I need to add the zeroes if it is valid data. I guess I need to figure out how to test if the string has data.

Thanks,
Natalie
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So if the user inputs zero you want the total to show zero, but if not you want it to be empty, even though the fields have the value zero (the default value)? Doesn't make much sense to me...

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

eliannah
Registered: Jun 13 2008
Posts: 17
Thanks for your response.

I am adding attendance totals. Sometimes during the quarter the student has 0 absences and so that zero needs to be added to show a 0 in the total absence field. This would apply to students with perfect attendance. However, since we work on a online system and a printed system the fields need to be blank when there is no data so that they will print blank when teachers print them out so they can write on them. Unfortunately,not all teachers are using the online version.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Are you trying to create a PDF form that can be filled in on the computer or printed and filled out with pen or pencil and then snail mailed?

You want your calculation script to check each value and sum the fields. You will need to determine how a zero value is to be handled. Yes, zero is different from a blank value.

A custom calculation script could be:
// array of field names to be summedvar aFields = new Array ("Text1", "Text2", "Text3", "Text4");// edit above field names as needed // do not make any changes below this line // force field value to a nullevent.value = ""; // variable to indicate a non-blank entry// assume all fields are blankvar bNonBlank = false; // variable for field valuevar nValue;// variable for sum of fieldsvar sum = 0; // controlled loop to test the value of a field for non-blank and sumfor (indx = 0; indx < aFields.length; indx++) {// get the string value of the indx fieldnValue = this.getField(aFields[indx]).valueAsString;// test value for non-null, including zero if (nValue != "") {// set non blank flag to truebNonBlank = true;// add value to the accumulated sumsum = sum + Number(nValue);} // end if non-blank} // end processing for indx // if non-blank present set field value to sumif(bNonBlank) {event.value = sum;} // end non-blank sum

George Kaiser

eliannah
Registered: Jun 13 2008
Posts: 17
That works but I only want the blank if all the fields are blank. If one of the fields is filled in then it needs to return a total on what is entered. In other words it is a progressive total. The teachers would fill in quarter 1 and those numbers would go in the total blank. When they fill in quarter 2 nine weeks later, then it would add those two numbers and so on and so forth. The only time it would need to blank for teachers to fill in is when all the fields are blank or empty.

Thanks
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The field is set to null or blank by default, and only has the sum if there is one or more non-null values in the fields to sum. And when all of the fields are null or blank there is no sum computed and no total entered into the total field or the field remains with a null value. Entering a zero into any field will result in the sum being zero.

If I were programing a form with this calculation repeated several times, I would be using a document level function to perform the task and return turn sum or a null value depending upon the value of the inputed fields. That way I could sum rows or columns as needed.

George Kaiser

eliannah
Registered: Jun 13 2008
Posts: 17
Thank you so much for your time. I figured out what I did. I have been messing with this for days.