I know nothing about scripting, but I'm hoping Acrobat has a script that would work to calculate the percentage of two numbers in form fields. I work for a police dept. and need to calculate the percentage of crimes solved vs. crimes committed (ex: 5 robberies, 2 of these are solved, so I need the third form field to calculate the solved percentage of the crimes). Can anyone SAVE me, please?
Hi George,
Thank you for your reply. The headings on the fields are "Actual Number," "Offenses Cleared," and "% Cleared." Then there is a list of the different crimes down the left side. I need to be able to plug in the numbers for actual number of crimes committed and actual number of those crimes solved, and then the percentage calculated in the third field.
Yes, I have the form fields set up as numbers, with 0 decimal places.
We always put a zero in either or both of the fields, so they would never be blank.
Yes, the boss wants the percentage to appear as this: 35.4%
Can you help me? I would be soooo grateful if you have a solution for me! ![]()
Hi JBinReno
Try this script, it seems to be working here in Acrobat 9 on iMac.
Use this as a Custom Calculation Script.
var a =this.getField("OffensesCleared");
var b =this.getField("ActualNumber");
event.value=(a.value/b.value)
How are you doing the calculation?
On a daily, weekly or monthly basis?
You can total it on a daily, weekly or monthly basis and then do the calculation.
Ron A
Here's a custom calculate script you can use for the % cleared field. It assumes the actual number field is named "an.1" and the offenses cleared field is named "oc.1". It sets the value to "0.0%" if either of the input is zero or blank, which you might want to change to something else like an empty string ("").
// Custom Calculate script for "% Cleared" field
(function () {
// Get the values of the inputs, as numbers
var actual_number = +getField("an.1").value;
var offenses_cleared = +getField("oc.1").value;
// Set this field's value. Round to 1 decimal point and add % sign
if (actual_number !== 0) {
event.value = util.printf("%.1f", offenses_cleared / actual_number * 100) + "%";
} else {
event.value = "0.0%";
}
})();
Since it sounds like you will want to be doing the same calculation for several rows, this really should be made into a document-level function that can be called by all of the % cleared fields. If you continue the field naming scheme and named the fields in the rows like "an.2", "oc.2", "an.3", "oc.3". etc. the function could be:
// Function in a document-level JavaScript
function percent_cleared() {
// Get the terminal field number of the field that called this function
var fn = event.target.name.split(".")[1];
// Get the values of the inputs, as numbers
var actual_number = +getField("an." + fn).value;
var offenses_cleared = +getField("oc." + fn).value;
if (actual_number !== 0) {
event.value = util.printf("%.1f", offenses_cleared / actual_number * 100) + "%";
} else {
event.value = "0.0%";
}
}
and then called it like this in the calculate event of each "% cleared" field:
// Custom Calculate script
percent_cleared();
If you're unfamiliar with creating document-level JavaScripts, post again.
Edit: fixed typo
enomail:
Thank you for your response. I am putting in monthly totals for each field. So do I just need to copy your script into the custom calculation script field for each field that needs to do the calculation for the preceeding two fields? Don't mean to sound dumb about this, but I've never done any custom scripting myself, so I greatly appreciate the knowledge people like you are willing to share here.
Hi JBinReno
Sorry but that script seemed to work great until I tried to use it several times, then I got a pop up saying the field format doesn't match the %Cleared.
Let me see if I can use the one George posted.
If I can make that work here I'll let you know.
If you want you can send me the form and I will try it on that.
Ron A
Hi George
I tried your document level script, but I can't get it to work.
Only made 4 rows for the calculations and just tried calling it on the first row.
Here is the form I made.
http://www.nixmailmac.com/crimestats.pdf
Ron A
You need to name the calculated fields with the same hierarchical convention as the other fields, so name the field in the first row "ClearedRow.1", and continue with "ClearedRow.2", etc. Also, remove the % formatting from the field (set formatting to None) since the script takes care of the % symbol.
Hi George and JBinReno
I think I got it working now.
The script is great.
Go here to check the latest form.
http://www.nixmailmac.com/crimestats.pdf
Ron A
Nice job, it looks good. Just a few suggestions:
1. Make all of the % cleared fields and the total fields at the bottom to read-only
2. Make all of the actual number and number cleared fields formatted to show no digits to the right of the decimal.
3. Set the tab order. It will be easiest to just set it to row order, as opposed to manual.
Ron and George:
You two are truly WONDERFUL!! I can't tell you how much I appreciate both of you taking the time to help me out. I'm so excited to have a solution to my little problem. I will be able to use this formula on several different forms (I'm trying to get the boss away from using Excel spreadsheets for this stuff... mostly because I'm more comfortable with working with PDFs than Excel. You two are my heroes! Thank you, thank you, thank you so very much! ![]()
Hi JBin
First you need to place the document level script.
To do that open my form.
Click on Advanced at the top of Acrobat.
Then under Document Processing
Choose Document Java Scripts.
Then click Edit.
Then copy and paste that script in your document following the same process.
If you can send me the form.
Ron A
Hi again, Ron!
Thank you so much for your patience with this Acrobat forms novice. I'd be happy to send you my form... I just don't know how. I don't see any way to attach a file in here. Am I missing something, or could you guide me in how I can send it to you (even if it's to your email)?
Thanks again!
JB
Hi again, Ron,
I hope you had a nice Easter weekend! I understand how to attach files to an email... I just don't know what your email address is or, if it's contained somewhere on this page, I don't know how to find it. Help! I really want to send you my file as I feel we're getting really close to getting this done.
Thanks again!
JB
North America
Europe, Middle East and Africa
Asia Pacific