13 Replies Latest reply on Nov 10, 2016 12:47 PM by gkaiseril

    How to Sum Percentage fields

    COSMonty

      Hello,

      I haven't played with JavaScript in a bit and I'm a novice at that.

       

      I have 9 fields that have a manual entry which is a percentage (I have the field set to 'NONE' format because the percentage format spits out too many zeros).

      Score1=15%

      Score2=10%

      etc.

       

      All fields must add up to 100% and display my TotalScore field.

       

      I can get any of my scripts to work.

       

      Please assist.

      Thank you

        • 1. Re: How to Sum Percentage fields
          gkaiseril MVP & Adobe Community Professional

          If you are going to add a set fields by either the "the field is the sum of the following fields" or the "simplified field notation" the fields in question must have a "Number" or "Percent" format not the "None" or any other option.

           

          You should be aware that the "Format" of a field is the displayed value only. So if a number field has a currency symbol that symbol is only present in the displayed value and the not the actual value of the field. For percentages the actual value of the field is multiplied by 100 and has the "%" post fixed to the displayed value. This lets one use the decimal value of the percentage for mathematical calculations without any addition changes to the fields actual value. If you sum a series of percentage fields that add up to 100% the actual sum of the fields will be 1.00.

           

          If you want to suppress the zeros when using the Percentage format one needs to use scripting to hide the field or dynamically set the format of the field to the Number format or Percentage format as necessary. How to use the special built-in functions for dynamically setting the format for fields was firs documented in the Acrobat JavaScript Reference API and noted in version 6 and 7 documentation.

           

          If you do not use the "Field is the ..." or the "Simplified Field Notation" methods, you need to be careful for field with a null value since for some actions the null value is treated as a zero number and others it is treated as a null string and in JavaScript the "+" operator is the additional operator and the concatenation operator depending upon how JavaScript assume the value to be a number of a string. You need to force the empty or null values to be a the number zero.

          • 2. Re: How to Sum Percentage fields
            George_Johnson MVP & Adobe Community Professional

            What is the latest script you tried?

            • 3. Re: How to Sum Percentage fields
              COSMonty Level 1

              Alright! So I decided to approach it from a different direction. Instead I used a Dropdown list and then tried to use a script to grab the value selected and give either a '.##' or reference another cell with the actual number.  Noting is working and I've tried several.  Once I get this script to work, I can then just use the simple 'Value is the...Sum' option.

               

              Here is one version.

               

              var dd1 = this.getField("Dropdown1.0").value;

              var max1 = this.getField("Max%").value;

               

              if (dd1 = “MET-100%”); {

              event.value = (".2");

              } else {

              if (dd1 = “NOT MET-0%”); {

              event.value = ".00";

              }

               

              Instead of this//

              event.value = (".2");

              I used this//

              event.value = ("max1");

              • 4. Re: How to Sum Percentage fields
                try67 MVP & Adobe Community Professional

                There are several issues with your code:

                1. The comparison operator in JS is "==" (or "==="), not "=". That is the value assignment operator.

                2. You must only use "straight" single or double-quotes in your code. Not this kind of quotes: “NOT MET-0%” ...

                3. If you want to refer to a variable don't put quotes around its name. Otherwise it's just a text string.

                • 5. Re: How to Sum Percentage fields
                  gkaiseril MVP & Adobe Community Professional

                  Not so simple if you want to suppress the error result when the total of the "A" column is zero and one wants to suppress the "DIV/0!" error and suppress "0" for calculated cells.

                   

                  Which calculation option did you use?

                   

                  Have you looked at the JavaScript console for any errors, key combination <CTRL> + "J"?

                   

                  Are you getting any pop-up errors?

                   

                  I use the "Custom JavaScript". Just like in Excel division by zero causes an error condition the same happens with calculations in PDF forms, so one needs to adjust the script for this condition.

                   

                  Portfolio Compute Percentages

                  has working PDF forms without errors and with errors along with an Excel file

                  • 6. Re: How to Sum Percentage fields
                    COSMonty Level 1

                    Ok, so I made the following corrections and am still getting this error at the 'bold' location:

                    SyntaxError: missing ) after argument list 1: at line 2

                     

                     

                    var dd1 = this.getField(Dropdown1.0).value;
                    var m1 = this.getField(Max1).value;

                    If dd1 == "MET-100%" {
                    Event.value = "m1";
                    } else {
                    if dd1 == "NOT MET-0%" {
                    event.value = .00;
                    }}

                     

                     

                     

                    I've tried several things and it won't clear.

                    • 7. Re: How to Sum Percentage fields
                      gkaiseril MVP & Adobe Community Professional

                      Are your field names in the "getField" method the names of the fields or variable whose value is the field's name?

                       

                      var dd1 = this.getField("Dropdown1.0").value; // using field name;
                      var m1 = this.getField("Max1").value; // using field name;

                      If dd1 == "MET-100%" {
                      Event.value = m1; // using variable's value;
                      } else {
                      if dd1 == "NOT MET-0%" {
                      event.value = .00;
                      }

                       

                      It might help to see your form or a sample form with the error issue. You will need to place the form file in a web file sharing service like dropbox, google drive, etc.

                      • 8. Re: How to Sum Percentage fields
                        COSMonty Level 1

                        Unfortunately I am unable to upload my form (I work for a federal agency).

                         

                        The quotes around the var field name fixed the first error. But now I get this:

                        SyntaxError: Missing ; before statement 4: at line 5

                         

                        var dd1 = this.getField("Dropdown1.0").value;
                        var m1 = this.getField("Max1").value;

                        If dd1 == "MET-100%" {
                        event.value = m1;
                        } else {
                        if dd1 == "NOT MET-0%" {
                        event.value = .00;
                        }}

                         

                        This is the format of the form:

                         

                        Criteria                              Max Score               Met/Not Met (Drop Down Options)          Score

                        1. Timely completion               20%                    Met-100%                                                I want to populate this field

                        • 9. Re: How to Sum Percentage fields
                          George_Johnson MVP & Adobe Community Professional

                          You need to change this line:

                           

                          If dd1 == "MET-100%" {

                           

                          to this:

                           

                          if (dd1 == "MET-100%") {

                           

                          and do the same to the other if statement. Note that "If" is not valid, but "if" is.

                          1 person found this helpful
                          • 10. Re: How to Sum Percentage fields
                            try67 MVP & Adobe Community Professional

                            - "if" must be in lower-case. JS is case-sensitive, remember!

                            - There must be parentheses around then entire if-condition.

                            • 11. Re: How to Sum Percentage fields
                              COSMonty Level 1

                              So, I fiddled around with it and got this to work but instead of the ".2" I want it to be "m1".  "m1" wont work and when I close out of the custom script, an error states that 'the value entered is not the same format of the field [score1].

                               

                              var dd1 = this.getField("Dropdown1.0").value;
                              var m1 = this.getField("Max1").value;
                              if(dd1 == " "){
                              event.value = " ";}
                              else if
                              (dd1 == "MET-100%"){
                              event.value = ".2";
                              }else if
                              (dd1 == "NOT MET-0%"){
                              event.value = ".0"
                              }

                              • 12. Re: How to Sum Percentage fields
                                COSMonty Level 1

                                FYI, I verified the formats of the 'm1' and 'score1' are the same and the entry into m1 is .2

                                • 13. Re: How to Sum Percentage fields
                                  gkaiseril MVP & Adobe Community Professional

                                  That is why I suggested you supply a sample of the form not the actual form. The sample could consist of only the portion of the form not working not the entire form.