I have a formula to obtain a value and it involves dividing three field entries. I don't know enough about javascript to correctly enter the code to make this happen. I have 20 rows in a form and I need to calculate column D. I saw a thread on how to divide two fields but I need to go further with that. Can anyone help me? I've added what I was working from (left) and what I tried to develop (right).
| Internet | Personal |
|---|---|
// establish variables for the field names for the dividend and divisor. var sDividend = 'My Dividend Field Name'; var sDivisor = 'My Divisor Field Name';
// get the value of the divisor field var nDivisor = this.getField(sDivisor).value; // get the value of the dividend field var nDividend = this.getField('sDividend').vlalue;
// do not change the code below this line // clear the result field value event.value = ''; // see if we have a non-zero divisor if (nDivisor != 0) { // we can now divide event.value = nDividend / nDivisor; } // end division calculation
The above assumes your are dividing the values of 2 fields. | // Assigning the variables var PW1 = "PlotWt1"; var WBC1 = "WetbuConversion1"; var AF1 = "Acre Factor1";
// Getting the values var nPW1 = this.getField(PW1).value; var nWBC1 = this.getField(WBC1).value; var nAF1 = this.getField(AF1).value;
// clear the result field value event.value=""; // see if we have a non-zero divisor if (nWBC1 !=0) { //Actual division operation event.value = nPW1 / nWBC1 / nAF1; }// end of division calculation |
The script is not returning a value to the field I'm trying to calculate. It just remains blank. It is an all or nothing type of form. If any of the cells have data they will all have data. The reason I only check to see if nWBC1 is zero is because I thought I could make it work by removing AF1 from the equation. That did not yield any results either.
And my next question once we've cracked this little chestnut is: Is there an easy way to apply this calculation to all 20 rows of the form or do I need to copy/paste and manipulate each row individually. In Excel I'd simply copy it to the bottom but that just don't seem possible in this program.
Change the one line of code to:
if (nWBC1 !=0 && nAFN != 0) {
If you want to use the same calculation for a number of rows, you can create a function that all of the calculated fields can use, but it would depend on the field names you're using. If you're just changing the number at the end, the function could be:
function calc1(num) {
// Assigning the variables
var PW = "PlotWt" + num;
var WBC = "WetbuConversion" + num;
var AF = "AcreFactor1" + num;
// Getting the values
var nPW = this.getField(PW).value;
var nWBC = this.getField(WBC).value;
var nAF = this.getField(AF).value;
// clear the result field value
event.value="";
// see if we have a non-zero divisor
if (nWBC !=0 && nAF != 0) {
//Actual division operation
event.value = nPW / nWBC / nAF;
}
}
You can then call it in the field's custom Calculate script like:
calc1(1); // Row 1
For row #5, it would be:
calc1(5); // Row 5
This could be simplified a bit if you changed your field naming scheme to make it easier to extract the row number. That way you wouldn't have to pass it as I've shown.
To create a document-level script to place the function, select: Advanced > Document Processing > Document JavaScripts > Add
GKaiseril wrote:
You could pass the field names a parameter in the function and then use the parameter value in the function to access the form field.
I still don't know how to program any Java and as such don't know how to pass anything a parameter although it sounds like that could be helpful.
I have created the document-level script without errors from the code you provided (cut and paste). But when I put calc1(2), calc1(3), etc in the respective field's custom calculation box it is throwing back this error:
TypeError: this.getField(PW) is null
9:Field:Calculate
TypeError: this.getField(PW) is null
9:Field:Calculate
You can see that it shows up twice for each individual field. I've entered values in those fields in the Preview mode and gone back into edit and it gives the same error. The strange part is that calc1(1) works properly.
Unfortunately I don't know either language. Your response did spark a revelation though, I changed some field names and now it all works correctly with the code provided above. Thanks for that!
I do have an additional issue though. I've been working on this in Abobe Professional 9.4 and I've developed my PDF form that I want my users to fill in but when I open the form in reader the D column no longer calculates. I'm not sure if I had to do a final "publishing" step to make the form operational but if you could point me in the right direction that would be great.
North America
Europe, Middle East and Africa
Asia Pacific