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

Simple Calculation but Overrideable

New Here ,
Sep 17, 2018 Sep 17, 2018

Copy link to clipboard

Copied

I have a box labeled HST and another for Subtotal.

I want to perform a simple math calculation on the HST box, the result displaying in the Subtotal box.

I want the user to be able to overwrite the Subtotal Box.  This Subtotal box is included in the TOTAL Box calculation which is not to be overwritten.

Thanks!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

217

Likes

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
Engaged ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

in the custom calculation script of subtotal:

if (event.value == ""){

     //Put in your calculation script

     event.value = //the result of the calculation

}

Basically what you are doing is not doing the calculation if something is entered manually.

Likes

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 ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

LATEST

A calculation script always runs. And it runs each and every time a field on the form changes value. The results of a calculation can be blocked from affecting the actual field value by setting "event.rc" to false. This allows the user to override the calculation.

However, there is a trick to getting this right, which is to clearly define the circumstances under which the the calculation is overridden and released. Simply testing for an empty field value will not work it because after the first calc result it will block all following calculations.

The easy solution is to use an override checkbox.

Here is the calculation script for the "SubTotal", assuming the checkbox is named "SubOvr"

event.value = this.getField("HST").value;

event.rc = this.getField("SubOvr").isBoxChecked(0);

event.readonly = event.rc;

when the checkbox is checked, the user is allowed to enter a value, when it is unchecked, the field is set to read only, so the user cannot click into the subtotal, and the calculation sets the value of the field.

There are other methods for doing this that automatically detect when the user is trying to enter a value and then set a hidden checkbox value (or document variable) that does essentially the same thing.  However this is more complicated.

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

Likes

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