• 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.2K

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

Copy link to clipboard

Copied

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?

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

Copy link to clipboard

Copied

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

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

Hi Nesa. No, that calculation doen't populate the Total M3 field with anything. How can I share the file with you privately? Thanks.

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


Thinking about it Nesa, it's going to be a nightmare trying to put all the 120+ variables into one calculation. Can you just provide the calculation for this and I'll enter each variation into each relevant Total M3 cell:

 

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

 

E.G: .2 x 2 = .4

 

I can share the form with you privately, I just don't know how.

 

Thanks

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

Bearing in mind that I want to delete the M3 column (as it's info the client doen't need to see) so I actually want to add/input the value of M3 into the Total M3 calculation rather than reference an M3 cell. Arhh this is getting so complicated to explain, but it seems like it should be a simple calculation!

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

Copy link to clipboard

Copied

I'm sure you answered this question. I've created a script to calculate a percentage change. I'd like the script to calculate the same for each subsequent row. Those subsequent row's have different text names field names. I saw that I can add an additional script to move to the next row. Can you tell me how to fix my calculation? 

rnaschfairmanagecom_1-1632696372905.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 26, 2021 Sep 26, 2021

Copy link to clipboard

Copied

I can't see how many fields you have so you will have to change that yourself, first remove your codes from percentage fields if you have any and use this script in only one of the fields (doesn't matter which one):

for( var i=0; i<=5; i++){
if(Number(this.getField("Text1.60."+i+".1").value)!= 0)
this.getField("1."+i).value = (Number(this.getField("Text1.60."+i+".1").value)-Number(this.getField("Text1.60."+i+".0").value))/Number(this.getField("Text1.60."+i+".0").value);
else
this.getField("1."+i).value = 0;}

 

In this part 'i<=5' change 5 to the number of fields you have "Text1.60.0.0" see orange number, change 5 to the number your last field have where is orange.

 

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

Copy link to clipboard

Copied

Will do! I have 4 pages of fields, should I count all 4 pages of fields or just per page? There are 116 fields total

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

Copy link to clipboard

Copied

If they are all named in same manner, it should work on all pages.

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

Copy link to clipboard

Copied

Your amazing, thank you so much for helping me at 11 pmPST. I love it!!! I entered the script but nothing happens in the next field? I deleted my original scrip and only entered what you sent. Now that I think about it, I probably should leave my old script and just add your script below. Is that correct? 

 

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

Copy link to clipboard

Copied

No, you should only use my script. Is your last field named 115 or 116?

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

Copy link to clipboard

Copied

Here is the script I entered in the 1st field, no scripts entered in the subsequent 41 field. 

var v1=+this.getField("Text1.60.0.0").value;
var v2=+this.getField("Text1.60.0.1").value;
event.value=0;
if(v2!=0){
//compute difference between v1 and v2 as a percentage of v1:
event.value=(v2-v1)/v1}

for( var i=0; i<=41; i++){
if(Number(this.getField("Text1.60."+i+".1").value)!=0)
this.getField("1."+i).value = (Number(this.getField("Text1.60."+i+".1").value)-Number(this.getField("Text1.60."+i+".0").value))/Number(this.getField("Text1.60."+i+".0").value);
else
this.getField("1."+i).value = 0;}

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

Copy link to clipboard

Copied

Thanks again, 

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

Copy link to clipboard

Copied

Remove your script.

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

Copy link to clipboard

Copied

Forgot to add "%'' next to percentage field:

for( var i=0; i<=41; i++){
if(Number(this.getField("Text1.60."+i+".1").value)!=0)
this.getField("%1."+i).value = (Number(this.getField("Text1.60."+i+".1").value)-Number(this.getField("Text1.60."+i+".0").value))/Number(this.getField("Text1.60."+i+".0").value);
else
this.getField("%1."+i).value = 0;}

 

Not all of your percentage field are format as percentage (only first few are) also if you format them as percentage you need to divide calculation by 100.

Your percentage fields are not named correctly they go to up to "%1.20" you need to fix that, they should be named "%1.0 to %1.41".

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

Copy link to clipboard

Copied

Ignore last reply. I will rename the field and change format to %. Where do i ad the divisor/100 in the formula?

 

[Signature removed]

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

Copy link to clipboard

Copied

That's what i had in the first attempt. Maybe i dont know what is supposed to happen? Using your script only, i tried entering data in in 2nd . I want the calculation to return a % like it did in the first row using my script. but nothing happened.

[Signature removed]

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

Copy link to clipboard

Copied

I fixed script and your fields, your first page should work now:

https://drive.google.com/uc?export=download&id=1Q2zwSGVPwN_9GmYJuhZAJzb16H_f8b7T 

 

On your other pages fields are named differently, if you want same script to work you need to rename fields to continue order.

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

Copy link to clipboard

Copied

I tried to continue the naming sequence on the next page of the form. However I don't think it's correctly renamed and I don't want to manually change each field name. I create multiple fields using the sequence and this is what happens (see % field name sequence) and I can't get the formula to work on this page.  

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
Nov 16, 2021 Nov 16, 2021

Copy link to clipboard

Copied

We have a form that we are using but I can't get the "Total Hrs" field to sum up the fields called "Travel To Hrs," + "Lunch Hrs" + "Travel From Hrs." Once those are summed up, I would need add in the difference between the start time and the stop time.  I am basically trying to get a total hours worked for the day calculated. I tried the Java Script and a few other options but I am completely stuck. I have attached a picture and the file. Photo 1.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 ,
Nov 17, 2021 Nov 17, 2021

Copy link to clipboard

Copied

Calculating time differences is not trivial using Acrobat JavaScript.

See these tutorials on how to approach it:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

 

If you're interested, I've developed a (paid-for) tool that allows you to set up such calculations very easily, without having to write any code. You can find it here:

https://www.try67.com/tool/acrobat-calculate-time-differences-in-a-worksheet

 

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 ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

Hi, I'm in need of some assistance on a few Forms as well please as I'm not too familiar with javascript.

I'm trying to get the highlighted field to automatically calculate but having trouble doing so..

 

please see attached.

 

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 ,
Mar 04, 2024 Mar 04, 2024

Copy link to clipboard

Copied

What are you trying to calculate?

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 ,
Feb 28, 2023 Feb 28, 2023

Copy link to clipboard

Copied

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.

Theresa28634363cne1_0-1677596428980.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 ,
Feb 28, 2023 Feb 28, 2023

Copy link to clipboard

Copied

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;

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