Skip navigation
Currently Being Moderated

Desperate for help to create a script!

Mar 31, 2012 6:51 PM

Tags: #acrobat-forms #calculations

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?

 
Replies
  • George Johnson
    11,664 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 31, 2012 7:10 PM   in reply to JBinReno

    What are the names of the fields for crimes solved and crimes committed?

     

    Do you have the input fields set up with a format category of Number?

     

    What do you want displayed if either of the two input fields is blank?

     

    Do you want the % symbol to appear in the field?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 31, 2012 9:55 PM   in reply to JBinReno

    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

     
    |
    Mark as:
  • George Johnson
    11,664 posts
    Aug 11, 2002
    Currently Being Moderated
    Mar 31, 2012 10:14 PM   in reply to JBinReno

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 1, 2012 2:26 PM   in reply to JBinReno

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 1:52 PM   in reply to George Johnson

    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

     
    |
    Mark as:
  • George Johnson
    11,664 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 2, 2012 2:00 PM   in reply to enomail

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 10:27 PM   in reply to George Johnson

    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

     
    |
    Mark as:
  • George Johnson
    11,664 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 2, 2012 10:45 PM   in reply to enomail

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 11:12 PM   in reply to George Johnson

    Hi George

     

    I just put that together to see if I could.

    Have no idea how many fields, rows, columns JBinReno needs.

    The small name like "an.1 is good when you have to type it 31 times per column.

     

    Ron A

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 3, 2012 10:54 PM   in reply to JBinReno

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2012 5:15 PM   in reply to JBinReno

    Hi JBin

     

    Just send email, but before you click send, choose attach file, then find the file and attach it to the email.

     

    Ron A

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2012 11:01 PM   in reply to JBinReno

    Hi JBin

    Send it here

    enomail@aol.com

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points