Highlighted

## Creating a Custom Calculation Script in Adobe Acrobat

Community Beginner ,
Jun 01, 2018

Copied

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?

kristy

Most Valuable Participant
| Most Valuable Participant

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();`
TOPICS
Macintosh, PDF forms

Views

10.2K

Likes

Report

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

## Creating a Custom Calculation Script in Adobe Acrobat

Community Beginner ,
Jun 01, 2018

Copied

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?

kristy

Most Valuable Participant
| Most Valuable Participant

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();`
TOPICS
Macintosh, PDF forms

Views

10.2K

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2018 1
17 Replies 17
Most Valuable Participant ,
Jun 01, 2018

Copied

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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2018 1
Community Beginner ,
Jun 02, 2018

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 02, 2018 0
Most Valuable Participant ,
Jun 02, 2018

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 02, 2018 0
Community Beginner ,
Jun 02, 2018

Copied

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?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 02, 2018 0
Most Valuable Participant ,
Jun 02, 2018

Copied

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();`

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 02, 2018 2
Community Beginner ,
Jun 11, 2018

Copied

Thank you so much, try67!! This worked perfectly!!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 11, 2018 0
New Here ,
May 28, 2019

Copied

Can you assist me. I am wanting to add a divide formula: StaffCount/BedCount for the BEdRatio Cell, which will be a percentage. Do I add this formula into the Simplified Field notation or the Custom Calculation script?

Also, I have placed it in the Simplified Field just as StaffCount/BedCount and it works, calculates and all. But it brings up an error message "The Value entered does not match the format of the field [ BedRatio ]

Can you assist. Thank you!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 28, 2019 0
Most Valuable Participant ,
May 28, 2019

Copied

That happens when the divisor is zero. Use this code as the custom calculation script of your field:

var v1 = Number(this.getField("StaffCount").valueAsString);

var v2 = Number(this.getField("BedCount").valueAsString);

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

else event.value = v1/v2;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 28, 2019 0
New Here ,
May 28, 2019

Copied

Thank you so much try67​ This is amazing. My form is completely done and everything works.

Also thank you for replying so quickly!!!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 28, 2019 0
New Here ,
May 30, 2019

Copied

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?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 30, 2019 0
Most Valuable Participant ,
May 30, 2019

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 30, 2019 0
New Here ,
May 30, 2019

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 30, 2019 0
Most Valuable Participant ,
May 30, 2019

Copied

Can you share the file with us (via Dropbox, Google Drive, Adobe Document Cloud, etc.)?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 30, 2019 0
New Here ,
Jan 10, 2020

Copied

Hi! I came across this forum and I know it's a little dated but I was hoping you could help me with some calculation issues. I a not Javascript savvy ..
I am creating a fillable PDF in Adobe Acrobat PRO DC. I am needing to calculate one column so that everything in the 15 rows(milage 54milerow1, milage 54milerow2, milage 54milerow3, etc) is multiplied by .54 and then totaled at the bottom in Sum16... Can someone help me with this?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 10, 2020 0
New Here ,
Sep 21, 2020

Copied

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.

G

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 21, 2020 0
Contributor ,
Sep 21, 2020

Copied

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 21, 2020 0
LATEST
Most Valuable Participant ,
Sep 21, 2020

Copied

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;

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 21, 2020 0
Resources