17 Replies Latest reply on Nov 14, 2013 1:25 PM by The Riot

    How to calculate only visible fields

    squareitup1313

      I am totaling up a group of fields and each field has a checkbox next to it, of which I have the field being hidden if the box is not checked.  So I want my bottom "grandtotal" to calculate the sum of only the fields that are visible and not the ones that are hidded.  Can anyone help me with this?

        • 1. Re: How to calculate only visible fields
          George_Johnson MVP & Adobe Community Professional

          The following code checks to see if a field is visible before adding its value to the sum. It assumes the text fields are named "text.1", "text.2", ..., "text.10":

           

           

          // Custom calculate script

          (function () {

           

              var f, sum = 0;

           

              // Loop through the text fields

              for (var i = 1; i < 11; i += 1) {

           

                  // Get a reference to the current text field

                  f = getField("text." + i);

           

                  // If the current field is visible (and printable), numerically add it's value to the running sum

                  if (f.display === display.visible) {

                      sum += +f.value;

                  }

              }

           

              // Set this field's value to the sum

              event.value = sum;

           

          })();

          • 2. Re: How to calculate only visible fields
            squareitup1313 Level 1

            Thanks George.  I am so new to Javascript.  If I give you the name of the fields that need to be totaled, can you put them into this script as they need to be so I can just copy and past the script right into the custom calculation area?

             

            Field names that need to be totaled only if they are visible:

            "grandtotal1"

            "grandtotal2"

            "grandtotal3"

            "grandtotal4"

            "grandtotal5"

            "grandtotal6"

            "grandtotal7"

            "grandtotal8"

            "grandtotal9"

            "grandtotal10"

            "grandtotal11"

            "grandtotal12"

             

            Need these totaled in the bottom field that is labeld "grandtotal"

             

            Thanks so much!!

            • 3. Re: How to calculate only visible fields
              George_Johnson MVP & Adobe Community Professional

              Try this:

               

              // Custom calculate script

              (function () {

               

                  var f, sum = 0;

               

                  // Loop through the text fields

                  for (var i = 1; i < 13; i += 1) {

               

                      // Get a reference to the current text field

                      f = getField("grandtotal" + i);

               

                      // If the current field is visible (and printable), numerically add it's value to the running sum

                      if (f.display === display.visible) {

                          sum += +f.value;

                      }

                  }

               

                  // Set this field's value to the sum

                  event.value = sum;

               

              })();

              • 4. Re: How to calculate only visible fields
                squareitup1313 Level 1

                That didn't work. It's doing something funky.  If I have all the field hidden, which they are hidded because the checkbox netx to each of them is unchecked.  When I start at the top and click the first check box next to field name "grandtotal1" it doesn't total the amount at the bottom, but then when I uncheck it and hide the field it totals it at the bottom.

                • 5. Re: How to calculate only visible fields
                  George_Johnson MVP & Adobe Community Professional

                  That is almost certainly because the field calculation order is incorrect. Set it to whatever makes sense for your form and try again.

                  • 6. Re: How to calculate only visible fields
                    squareitup1313 Level 1

                    Not sure what you mean.  I have the fields in order from "grandtotal1" - 12.  The calculation that I have is what you gave me in the script and I just plugged that into the custom calculation calculation script in the "Calculations" tab in the "grandtotatl" field that is totalling everything.  What exactly do I need to do.  Sorry I know, super beginner on this.

                     

                    Thanks.

                    • 7. Re: How to calculate only visible fields
                      George_Johnson MVP & Adobe Community Professional

                      Exactly how you set the field calculation order depends on what version of Acrobat you're using.

                       

                      Acrobat 9: Forms > Add or Edit Fields    and then: Forms > Edit Fields > Set Field Calculation Order

                       

                      Acrobat 10: Tools > Forms > Edit    and then: Tasks > Other Tasks > Edit Fields > Set Field Calculation Order

                       

                      Just arrange the fields in the list in the order that makes sense. Some people confuse the calculation order with the tab order, but they are entirely different.

                      • 8. Re: How to calculate only visible fields
                        squareitup1313 Level 1

                        Ok I have gone into where you mention for the filed calculation order in Acrobat 10.  I have many other fields that are calculated fields on the document that are in other areas, but these fields that we are talking about are in order as I typed them in the earlier post.  Do they have to be above all the other calculated fields that are shown in the window? 

                        • 9. Re: How to calculate only visible fields
                          George_Johnson MVP & Adobe Community Professional

                          The field that you attached the script to should be below the grandtotal1-12 fields. In re-reading your post #4, it could be something else. I'd be happy to look at the form if you're free to email it to me at: acroscript at gmail dot com

                          • 10. Re: How to calculate only visible fields
                            squareitup1313 Level 1

                            Ya I'm going to email it over to you.  It is not what you are indicating as they are all in order. The issue is that when I check the box and show the field it is not totaling, but when I uncheck it, it does total.  I'll email it over to you now.  Please look for an email to come from jeff at squareitup dot com.  I'll tell you what page to look at in the email.

                             

                            Thank you.

                            • 11. Re: How to calculate only visible fields
                              George_Johnson MVP & Adobe Community Professional

                              I assumed you were setting text fields to readonly differently than you are. Here is a revised script that will work with the way you have it set up:

                               

                              // Custom calculate script

                              (function () {

                               

                                  var sum = 0;

                               

                                  // Loop through the fields

                                  for (var i = 1; i < 13; i += 1) {

                               

                                      // If the current field's check box is selected, add the corresponding text field value to the running sum

                                      if (getField("totalcheck" + i).value !== "Off") {

                                          sum += +getField("grandtotal" + i).value;

                                      }

                               

                                  }

                               

                                  // Set this field's value to the sum

                                  event.value = sum;

                               

                              })();

                               

                               

                              Also, calculated fields should normally be set to read-only so the user is prevented from interacting with them.

                              • 12. Re: How to calculate only visible fields
                                squareitup1313 Level 1

                                George you are a saint!  It works perfectly.  Thank you so much!!

                                • 13. Re: How to calculate only visible fields
                                  squareitup1313 Level 1

                                  Hey George I just sent you another email with an updated doc.  I have another item I need help with if you don't mind?

                                  • 14. Re: How to calculate only visible fields
                                    squareitup1313 Level 1

                                    Hey George,

                                     

                                    Any luck with that document update we were talking about.  Thanks again so much for your help.  This is the last piece for us with this document.

                                    • 15. Re: How to calculate only visible fields
                                      The Riot

                                      Hi George,

                                       

                                      I am happy to see this thread since I am working with the same challenges.

                                      I will try to use the script, and will rename my fields properly so I can do so.

                                       

                                      First, I have a question: Can I put the amounts for each field in InDesgn, or do all of the calcuation numbers need to be added/entered in Acrobat.

                                       

                                      Thank you sir!

                                      • 16. Re: How to calculate only visible fields
                                        George_Johnson MVP & Adobe Community Professional

                                         

                                        First, I have a question: Can I put the amounts for each field in InDesgn, or do all of the calcuation numbers need to be added/entered in Acrobat.

                                         

                                         

                                        You have to do it in Acrobat. You can create fields in InDesign, but you can't add scripts or use the fields interactively.

                                        • 17. Re: How to calculate only visible fields
                                          The Riot Level 1

                                          George,

                                           

                                          I am able to set the hide/show for the fields in InDesign true? Seems ike

                                          its workingŠ

                                           

                                          I read the thread where the first gentleman needed a different code since he

                                          was using 'read only' in a different manner than normal.

                                           

                                          I tried both scripts and could not get the functionality.

                                          For my project, the first field on page one gets entered by the salesman,

                                          then we add the options on the following pages.

                                          So I need the sum of 'read only' and interactive.

                                           

                                          Could you please take a loo at my doc. I feel like I am so closeŠ.

                                          I am only using the first few fields until I am confident to do the rest (in

                                          case something changes in the techniqure)

                                           

                                          Thank you,

                                           

                                          Eric