• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
3

Manually override calculation field

Explorer ,
Jan 08, 2024 Jan 08, 2024

Copy link to clipboard

Copied

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

TOPICS
JavaScript , PDF

Views

223

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 08, 2024 Jan 08, 2024

Copy link to clipboard

Copied

Remove last line:

else { event.value = "0.00";

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2024 Jan 09, 2024

Copy link to clipboard

Copied

Thanks for that

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2024 Jan 10, 2024

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 10, 2024 Jan 10, 2024

Copy link to clipboard

Copied

Thanks for the advice Thom, much appreciated. 

 

I will make the above changes as youve suggested. 

 

cheers

LC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

After a bit reading and playing etc I settled for the following

 

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 (fromField.value == "OTHER" || toField.value == "OTHER") {

event.rc = true;

} 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";

}

 

this seems to be allowing the user to manually enter their own value when "Other" is selected. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

Actually, event.rc should be set to false. 

You're code works, not because the calculation is being blocked, but because no value is being returned to event.value, which works out to about the same thing. 

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

LATEST

Thanks. I will change it over. 

 

cheers

LC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2024 Jan 09, 2024

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 09, 2024 Jan 09, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines