Skip to main content
Known Participant
January 9, 2024
Question

Manually override calculation field

  • January 9, 2024
  • 2 replies
  • 1660 views

Hi All,

 

I have the following script in a field that pulls berth depths from another field

 

var allberths = {

"SCPT": "10.1",

"WBCT": "10.9",

"WHT4": "11.1",

"GLB1": "12.0",

"GLB2": "12.0",

"GLB7": "10.6",

"GLB8": "9.4",

"GOR1": "13.2",

"GOR2": "8.9",

"WAHEN": "4.6",

"ICCD": "11.0",

"FLTBASE": "8.0",

"CBOFF": "11.8",

"BANK1": "14.0",

"BANK2": "13.5",

"ATHOL BAY": "11.6",

};

 

var movementField = this.getField("MOVEMENT");

var fromField = this.getField("FROM");

var toField = this.getField("TO");

 

if (movementField.value === "SELECT ONE") {

event.value = "0.00";

} else if (movementField.value === "ARRIVAL" && fromField.value === "SEA" && toField.value === "SELECT ONE") {

event.value = "0.00";

else if (movementField.value === "DEPARTURE" && fromField.value === "SELECT ONE" && toField.value === "SEA") {

event.value = "0.00";

} else if (movementField.value === "ARRIVAL" && allberths[toField.value]) { event.value = allberths[toField.value];

} else if (movementField.value === "DEPARTURE" && allberths[fromField.value]) { event.value = allberths[fromField.value];

} else if (movementField.value === "REMOVAL" && allberths[toField.value]) { event.value = allberths[toField.value];

} else { event.value = "0.00";

}

 

 

I was going to put in an option called "other" in my table, but want to able to enter the depth value manually but not sure how to do it. Currently if I enter a value manually it just reverts back to the value from the table.

 

Thanks

LC

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
January 9, 2024

In order to override a calculation there as to be a way for the script to know not to set the value. And the opposite has to be true as well. The script needs to know that it needs to set the value.   

 

The simplest solution is use an indicator value. You mentioned using an "Ohter" value in something? Is this a form field? If so then selecting "Other" could be used to block the calculation so a manual value could be entered. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
January 10, 2024

Thanks,

 

Yep "Other" will be an option to select instead of the ones listed above. I have added "other": "", as the last line in my table above

 

and removed the 

 

else { event.value = "0.00";

 

as per Nesa's suggestion above. I think it now works as required, just need to do some more testing to be sure.

 

Thanks for your help.

Nesa Nurani
Community Expert
Community Expert
January 9, 2024

Remove last line:

else { event.value = "0.00";

}

Known Participant
January 10, 2024

Thanks for that

 

see my comments below - i think i now have it working the way I want.

Thom Parker
Community Expert
Community Expert
January 10, 2024

Use this code in the "else if" where the "Other" value is specified.

 

else if(movementField.value == "Other"); 

     event.rc = false;

 

Setting "event.rc" to false blocks the script from having any effect on the field value. 

 

It's also a best coding practice to provide complete conditions. So use a final "else" that sets the value to indicate an invalid selection.  

And, just a suggestion. It is not necessary or a good idea to use the identity operator (===) for text comparisons. Use the equality operator (==) .  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often