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
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
...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.
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!
Copy link to clipboard
Copied
If you provide the field names I can help you with the code.
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?
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();
Copy link to clipboard
Copied
Thank you so much, try67!! This worked perfectly!!
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!
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;
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!!!
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.
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;
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.
Copy link to clipboard
Copied
Just use the built-in Sum command. It doesn't care if the value is negative or positive...
Copy link to clipboard
Copied
I did do that, but it does not calculate correctly (see below)
Copy link to clipboard
Copied
Can you share the file with us (via Dropbox, Google Drive, Adobe Document Cloud, etc.)?
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?
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,
G
Copy link to clipboard
Copied
Can you share your file?
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;
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.
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.
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.
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.
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.