Skip to main content
New Participant
June 1, 2018
Answered

Creating a Custom Calculation Script in Adobe Acrobat

  • June 1, 2018
  • 4 replies
  • 68213 views

Hi! I don't have any experience with script, but I think that I need to create a custom calculation script in my Adobe Acrobat form.

I have created a budget form and simply want to be able to enter expenses and have them automatically add up the running total. I'm trying to figure out a way to code this in the calculation area easily, without needing to write new code for each box.

So, for example, the form looks like this:

Date     Description     Category     Amount     Balance

4/23     Chipotle             Food            $25          $25

4/25     Starbucks          Food            $10          $35

4/27     Target                Supplies      $100        $135

I want the form to be able to automatically calculate that the balance is 35 when I add in the 10 for Starbucks, then 135 when I enter 100 for Target, etc. From looking it up in the past, I used the following for some similar calculations:

// Obtain the value from the first field

var v1 = getField("balance1").value;

// Obtain the value from the second field

var v2 = getField("amount2").value;

// Set this field value equal to the sum

event.value = v1 + v2;

When balance1 is the $25 and amount2 is the $10.

Is there a way I can change this formula to be able to use it for every balance field without needing to edit the formula in each one separately?

Thank you for your assistance!!

kristy

This topic has been closed for replies.
Correct answer try67

Thank you so much for your help!!

I have attached a shot of the file below. So the amount and balance columns are sequential (amount1, amount2, amount3, etc, and balance1, balance2, balance3, etc). I hope this is what you need?


OK, then this is the doc-level script you can use (insert it under Tools - JavaScripts - Document JavaScripts):

function calcBalance() {

    var rowNumber = Number(event.target.name.replace("balance", ""));

    var amount = Number(this.getField("amount"+rowNumber).valueAsString);

    if (amount==0) event.value = "";

    else {

        var prevBalance = 0;

        if (rowNumber!=1) prevBalance = Number(this.getField("balance"+(rowNumber-1)).valueAsString);

        event.value = amount+prevBalance;

    }

}

And then the custom calculation script of each "balance" field is simply:

calcBalance();

4 replies

New Participant
February 28, 2023

Hello, I'm looking for assitance.  I need to calculate a percentage which would be the (Actual Lodging + Actual M&IE) divided by the (Per Diem Lodging + M&IE) but I don't know how to write the custom calculation script.

 

try67
Community Expert
February 28, 2023

You can do it using this code:

 

var v1 = Number(this.getField("Actual Lodging").valueAsString);
var v2 = Number(this.getField("Actual M&IE").valueAsString);
var v3 = Number(this.getField("Per Diem Lodging").valueAsString);
var v4 = Number(this.getField("M&IE").valueAsString);

var v1_2 = v1+v2;
var v3_4 = v3+v4;
if (v3_4==0) event.value = "";
else event.value = v1_2 / v3_4;
New Participant
March 8, 2024

I need help. I need to know the code to calculate the Freight=Labels &Tags + Valves & Components (0.07)

 

 

New Participant
September 21, 2020

Hi there,

I was wondering if you could help me with the below problem:

I have to apply 15% promotional discount if the customer purchases 2 or more items from the given choice, would you know how can I acheive the same.

Thanks in advance,

G

 

try67
Community Expert
September 21, 2020

In order to help we need to either see the file or at least know the names of the fields.

However, if you name them something like Price1, Price2, etc. and place the number (without "EUR" or the commas) as their export values you could use this script as the custom calculation script of your text field:

 

var total = 0;

for (var i=1; i<=18; i++) {

var v = this.getField("Price"+i).valueAsString;

if (v!="Off") total+=Number(v);

}

event.value = total*0.85;

Nesa Nurani
Community Expert
September 12, 2021

Correction! Note: I can't see how to edit my comment.

 

This line:

This might look like .2 x 2 = .4 - Can I put a formula that translates to this in the M3 cells?

 

Should read: 

This might look like .2 x 2 = .4 - Can I put a formula that translates to this in the Total M3 cells?


You put script just in one field, as custom calculation script.

I used only four of each fields for example, you should add the rest of your fields to script.

Test script to see if it works as you intended with those fields already in the script before you add additional fields.

It would be easier to help you if you could share actual file with us.

Here is the script:

var M3Bed = ["M3Bed Single","M3Bed Double","M3Bed Queen","M3Bed King"];
var QuantityBed = ["QuantityBed Single","QuantityBed Double","QuantityBed Queen","QuantityBed King"];
var TotalM3 = ["Total M3 Bed Single","Total M3 Bed Double","Total M3 Bed Queen","Total M3 Bed King"];

for( var i in QuantityBed){
if(this.getField(QuantityBed[i]).value != "")
this.getField(TotalM3[i]).value = Number(this.getField(M3Bed[i]).value)*Number(this.getField(QuantityBed[i]).value);
else this.getField(TotalM3[i]).value = "";}

Participating Frequently
May 30, 2019

I am working on a PDF that needs to calculate positive and negative numbers to get a subtotal. One item might have a credit of -$1,000 and the next line might show a -$250 credit and then a line might show a $300 charge. How can I get an accurate subtotal mixing positive and negative numbers?

Thanks in advance.

try67
Community Expert
May 30, 2019

Just use the built-in Sum command. It doesn't care if the value is negative or positive...

Participating Frequently
May 30, 2019

I did do that, but it does not calculate correctly (see below)

try67
Community Expert
June 1, 2018

Depends on how you named the fields. If you did it in a consistent way then you could write a doc-level function that does the calculation and then simply call it from each one of the Balance fields.

New Participant
June 2, 2018

Thank you for your response! Do you know if there is a tutorial somewhere that explains how to do that? Thank you!

try67
Community Expert
June 2, 2018

If you provide the field names I can help you with the code.