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

Display blank for empty calculations

hap000
Registered: Jun 5 2007
Posts: 9

Hi ...
I have two questions.

1. If I am calculating a percentage of two fields ("Played" and "Points"), how can I display the field as blank (rather than 0.0%) if either of the source fields are zero or blank? I tried the following, but it didn't work:

var a = this.getField("Played.0").value;
var b = this.getField("Points.0").value;

event.value = b / (a * 2);
if (a == "")
event.value = "";
if (b == "")
event.value = "";

2. I have created an array of 30 rows and need to do calculations for each row. From the sample above, you can see that I'm brute forcing the array for "Percent.0" rather than using indexing. Is there an easy way to refer to a form field by its row?

Thanks,

Hap

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The percentage field is a special format and the only way to suppress it is to write your on custom format and you would also have to have a function to convert the your string for the percentage back to a number for calculations. Of course, you can always make the field hidden. You will also need to adjust your script to avoid division by zero.

George Kaiser

hap000
Registered: Jun 5 2007
Posts: 9
Okay.

How do I make the field hidden if another field is blank or zero?

Also, does anyone have a suggestion for my array question?

Hap
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
That depends on where you want to do the calculation:

// field custom calculation only
var sField = event.target.name; // get the event name
var aField = event.target.name.split("."); // make strings between "." array element
var Length = aField.length; // get number of elements in the array
var Position = Length - 1; // adjust for zero start of array elements
var Index = aField[Position]; // get value from index position
var Played = this.getField("Played." + Index);
var Points = this.getField("Points." + Index);
var Percent = this.getField("Percentage." + Index);
if (Points.value == 0 | Played.value == 0) Percent.value = 0;
else Percent.value = Points.value / (Played.value * 2);
if (Percent.value == 0) Percent.textColor = color.white;
else Percent.textColor = color.black;

/*
using a Document level and field custom calculation script
*/

// document level function to get sField's last element value
function LastLevelValue (sName) {
var aName = sName.split("."); // make strings between "." array element
var Length = aName.length; // get number of elements in the array
var Position = Length - 1; // adjust for zero start of array elements
return aName[Position]; // get value from index position
} // end LastLevelValue function

// field custom calculation script
var Index = LastLevelValue(event.target.name); // get index from field name
var Played = this.getField("Played." + Index);
var Points = this.getField("Points." + Index);
var Percent = this.getField("Percentage." + Index);
if (Points.value == 0 | Played.value == 0) Percent.value = 0;
else Percent.value = Points.value / (Played.value * 2);
if (Percent.value == 0) Percent.textColor = color.white;
else Percent.textColor = color.black;

/*
using only Document Level Script for the calculation and a single field call
*/

// Document level function
function ComputePercent() {
var oPercent = this.getField("Percentage"); // get percent field object
var aPercent = oPercent.getArray(); // make an array of the field object's members
var Length = aPercent.length; // get number of elements in array for the field

for (Index = 0; Index < Length; Index++) {
var Played = this.getField("Played." + Index);
var Points = this.getField("Points." + Index);
var Percent = this.getField("Percentage." + Index);
if (Points.value == 0 | Played.value == 0) Percent.value = 0;
else Percent.value = Points.value / (Played.value * 2);
if (Percent.value == 0) Percent.textColor = color.white;
else Percent.textColor = color.black;
} // end for Index

return;
} // end ComputePercent function

// field call for calculation of all percentages
ComputePercent();

George Kaiser

scottsheck
Registered: May 3 2007
Posts: 138
For your array question, use a table, not form fields. A cell of a table acts just like a form field.

Also, your code for setting the value to "" should work.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
My mistake, the op's inital codde looked like Acrobat Forms JavaScript, not LiveCycle Designer JavaScript.

George Kaiser

hap000
Registered: Jun 5 2007
Posts: 9
Thanks for all the suggestions. They make my learning curve less vertical. By the way, I'm using Acrobat Pro 8.1, not LiveCycle Designer.