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

Creating a Custom Calculation Script in Adobe Acrobat

Community Beginner ,
Jun 01, 2018 Jun 01, 2018

Copy link to clipboard

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?

Thank you for your assistance!!

kristy

TOPICS
PDF forms

Views

55.4K

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

correct answers 1 Correct answer

Community Expert , Jun 02, 2018 Jun 02, 2018

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;

    }

}

A

...

Votes

Translate

Translate
Community Expert ,
Jun 01, 2018 Jun 01, 2018

Copy link to clipboard

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.

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 Beginner ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

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

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

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

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 Beginner ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

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?

Screenshot.jpeg

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 ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

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

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 Beginner ,
Jun 11, 2018 Jun 11, 2018

Copy link to clipboard

Copied

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

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
Guest
May 28, 2019 May 28, 2019

Copy link to clipboard

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!

BedRatio.JPG

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 ,
May 28, 2019 May 28, 2019

Copy link to clipboard

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;

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
Guest
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

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

Also thank you for replying so quickly!!!

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
New Here ,
Jan 18, 2022 Jan 18, 2022

Copy link to clipboard

Copied

Can you also help me, please? I am trying to get a rate with out doing the calculations by hand.

Misty22711590uzlq_0-1642519098868.png

 

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 18, 2022 Jan 18, 2022

Copy link to clipboard

Copied

Use this:

 

var v1 = Number(this.getField("cases").valueAsString);
var v2 = Number(this.getField("Hours").valueAsString);
if (v2==0) event.value = "";
else event.value = (v1*200000)/v2;

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 Beginner ,
May 30, 2019 May 30, 2019

Copy link to clipboard

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?

Thanks in advance.

Screen Shot 2019-05-30 at 3.26.43 PM.png

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 ,
May 30, 2019 May 30, 2019

Copy link to clipboard

Copied

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

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 Beginner ,
May 30, 2019 May 30, 2019

Copy link to clipboard

Copied

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

Screen Shot 2019-05-30 at 4.07.38 PM.png

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 ,
May 30, 2019 May 30, 2019

Copy link to clipboard

Copied

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

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 Beginner ,
Jan 10, 2020 Jan 10, 2020

Copy link to clipboard

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?

expense milage.png

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
New Here ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

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.

Thanks in advance,

GScreenshot 2020-09-21 at 1.54.31 PM.png

 

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
Enthusiast ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

Copied

Can you share your file?

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 ,
Sep 21, 2020 Sep 21, 2020

Copy link to clipboard

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;

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
New Here ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

Hi TRY67.

 

It would be amazing if you could help me too?

Is there a formula you can use that enables me to multiply a cell by an amount and show it in the total field?

I have created a form but I don't want to include the column m3 column (make it visible). I just want the person completing the form to see the Quantity and Total M3 columns. Is it possible to enter a formula such as .2xQuanitiyBedSingle=TotalM3BedSingle

 

Also, each furniture item is a different size. Is there a way to quickly add calculations without going throuch each Total cell (my form is very comprehensive)? 

Thanks so much.

 

screen capture Adobe Pro.png

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 ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

Copied

What are the exact field names you want to use for these calculations?

The visibility of the fields is not important. You can hide them and still use their values in your formula or script. It is important, though, to set those values as their default ones, though, or they will be cleared when the form is reset, and then your calculations won't work any longer.

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
New Here ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

Copied

Thanks for your reply.  I know you can hide the value of a cell from view but I actually don't want to show the entire column 'M3' (above), I'd rather it not be on there for the client to see. 

 

In answer to your question:

Each 'Total M3' will be different as each 'M3' is unique. So, for example in the 'Bed - Single' row the 'M3' value is '.2', the cell name is 'M3Bed Single'. The cell name for the Bed - Single row Quantity field is 'QuantityBed Single' and for the Total M3 cell the name is 'Total M3 Bed Single'  These cell names were all auto allocated when I impoted the document. 

 

Does that help?

 

I guess I have 2 questions:

1. Is there a way to hide an entire column from view but still use its value? or

2. Is there a way to add a value to a custom calculation so there is no need to have an extra column, hidden or visible?

 

I hope that's clear.

 

Thanks so much.

 

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 ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

Copied

Try something like this:

var qty = ["QuantityBed Single","QuantityBed Double","QuantityBed Queen","QuantityBed King"];
var total = ["Total M3 Bed Single","Total M3 Bed Double","Total M3 Bed Queen","Total M3 Bed King"];
var price = [.2,.4,.6,.8];
for( var i in qty){
if(this.getField(qty[i]).value != "")
this.getField(total[i]).value = Number(this.getField(qty[i]).value)*price[i];
else this.getField(total[i]).value = "";}

 

Put it in one of the fields as 'Custom calculation script'. I used prices as example you can change them to your actual prices and you can add  more fields and prices if needed.

First price match first field 2nd to 2nd field...etc.

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
New Here ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Thanks for your reply Nesa. I'm a bit confused by the formula and wondered if I could clarify.

 

There is no price field in my table/form. This form is a house inventory caluculator which measures space required for a moving van. There are just 3 columns/cell types - M3 (which is Metres cubed), Quantity (as in the number of single beds for example) and Total M3 (Total metres cubed).

 

All I want to do is:

A. Remove the M3 column above from the table (as this is too much information than is needed from the client and will make the sheet look more comlicated than it is), and instead add a value of each M3 into a calucualtion for the corrresponding Total M3 cell like this:

 

M3 (M3Bed Single) X (multiplied) by Quantity (QuantityBed Single) = Total M3 (Total M3 Bed Single).

 

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

 

B. In addition, it would be awsome if there was a way to easily carry/replicate this formula over to all 100(!) Total M3 cells rather than be having to type it in seapartely.

 

Can you let me know if this is what your suggested caluculation will do. Sorry I'm new to Adobe Pro and also have no idea about Java Script and complex formulas.

 

Thanks so much.

 

 

 

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