20 Replies Latest reply: Apr 8, 2012 11:01 PM by enomail RSS

    Desperate for help to create a script!

    JBinReno Community Member

      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?

        • 1. Re: Desperate for help to create a script!
          George_Johnson CommunityMVP

          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?

          • 2. Re: Desperate for help to create a script!
            JBinReno Community Member

            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!

            • 3. Re: Desperate for help to create a script!
              enomail Community Member

              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

              • 4. Re: Desperate for help to create a script!
                George_Johnson CommunityMVP

                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

                • 5. Re: Desperate for help to create a script!
                  JBinReno Community Member

                  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.

                  • 6. Re: Desperate for help to create a script!
                    enomail Community Member

                    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

                    • 7. Re: Desperate for help to create a script!
                      JBinReno Community Member

                      Hi Ron,

                       

                      Thank you so much for your help. How do I send you my file?

                      • 8. Re: Desperate for help to create a script!
                        enomail Community Member

                        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

                        • 9. Re: Desperate for help to create a script!
                          George_Johnson CommunityMVP

                          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.

                          • 10. Re: Desperate for help to create a script!
                            JBinReno Community Member

                            Thanks, guys, for your suggestions and help. I'm going to try George's script on my form tomorrow and see if I can get it to work. This is very new to me so I hope I can make sense of it all! George: Where would I put the script for the "document level" function?

                             

                            Thanks!

                            JB

                            • 11. Re: Desperate for help to create a script!
                              enomail Community Member

                              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

                              • 12. Re: Desperate for help to create a script!
                                George_Johnson CommunityMVP

                                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.

                                • 13. Re: Desperate for help to create a script!
                                  enomail Community Member

                                  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

                                  • 14. Re: Desperate for help to create a script!
                                    JBinReno Community Member

                                    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!

                                    • 15. Re: Desperate for help to create a script!
                                      JBinReno Community Member

                                      HELP!

                                       

                                      Ron and George,

                                       

                                      I saved Ron's file to my computer and copied and pasted the custom script he had in the field that needs to calculate the percentages, but when I typed some numbers in the first two fields, nothing happened in the third field. What am I doing wrong??

                                      • 16. Re: Desperate for help to create a script!
                                        enomail Community Member

                                        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

                                        • 17. Re: Desperate for help to create a script!
                                          JBinReno Community Member

                                          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

                                          • 18. Re: Desperate for help to create a script!
                                            enomail Community Member

                                            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

                                            • 19. Re: Desperate for help to create a script!
                                              JBinReno Community Member

                                              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

                                              • 20. Re: Desperate for help to create a script!
                                                enomail Community Member

                                                Hi JBin

                                                Send it here

                                                enomail@aol.com