18 Replies Latest reply on May 10, 2011 12:24 PM by gkaiseril

    Addition Script

    burlingtonnow

      Hi, I am trying to add the totals of 13 seperate text boxes.

       

      My totaling text boxes are named AverageDiv  to AverageDiv13

      In a different text box called "TADO" want to add the totals for AverageDiv thru AverageDiv13

       

      Can some give me a script that will do this effectively?

        • 1. Re: Addition Script
          MarkWalsh Level 4

          No script is necessary, just go to the 'Calculate' tab in the field properties, and click the second radio button (Value is the 'Sum' of the following fields) and then select the fields you want to sum.

           

          Also, if you rename the fields 'AverageDiv.1' through 'AverageDiv.13' (Note the addition of the 'dot'), then you only need to do a sum of 'AverageDiv' (the part before the dot) instead of adding each field.

          • 2. Re: Addition Script
            George_Johnson MVP & Adobe Community Professional

            Is the first field "AverageDiv" or "AverageDiv1"? If the former, what are the other field names?

            • 3. Re: Addition Script
              burlingtonnow Level 1

              Hi George,

               

              The first text field is called AverageDiv. Here are all of the text field that I want to add.

               

              AverageDiv

              AverageDiv1

              AverageDiv2

              AverageDiv3

              AverageDiv4

              AverageDiv5

              AverageDiv6

              AverageDiv7

              AverageDiv8

              AverageDiv9

              AverageDiv10

              AverageDiv11

              AverageDiv12

              AverageDiv13

               

               

              I am trying to work with this script, that I did research on with my particulat problem in mind.....not saying this will work but I am trying to learn from my mistakes.

               

              function doTotal (form)  {

               

              var a = (form.AverageDiv.value ! = ") ;

              var b = (form.AverageDiv1.value ! = ") ;

              var c = (form.AverageDiv2.value ! = ") ;

              var d = (form.AverageDiv3.value ! = ") ;

              var e = (form.AverageDiv4.value ! = ") ;

              var f = (form.AverageDiv5.value ! = ") ;

              var g = (form.AverageDiv6.value ! = ") ;

              var h = (form.AverageDiv7.value ! = ") ;

              var i = (form.AverageDiv8.value ! = ") ;

              var j = (form.AverageDiv9.value ! = ") ;

              var k = (form.AverageDiv10.value ! = ") ;

              var l = (form.AverageDiv11.value ! = ") ;

              var m= (form.AverageDiv12.value ! = ") ;

              var n = (form.AverageDiv13.value ! = ") ;

               

              form.Total.value = a+b+c+d+e+f+g+h+i+j+k+l+m+n ;

              }

              • 4. Re: Addition Script
                George_Johnson MVP & Adobe Community Professional

                Mark made some good suggestions.

                 

                Assuming this form was created in Acrobat and not LiveCycle Designer, you'd have to change the code you have to something like:

                 

                function doTotal () {
                
                    // Get the field values, as numbers
                    var a = +getField("AverageDiv").value;
                    var b = +getField("AverageDiv1").value;
                    // etc.
                
                    // Set this field value to the sum of field values
                    event.value = a+b+c+d+e+f+g+h+i+j+k+l+m+n;
                
                }
                
                • 5. Re: Addition Script
                  burlingtonnow Level 1

                  Hi Mark,

                   

                   

                  I tried that approach, which seems easiest but the problem is this. If I change the calculation in lets say AverageDiv1  well the overall text total box will not change thus making my total wrong. and as for the "Dot"   my text field read like this ....AverageDiv, AverageDiv1, AverageDiv2  ect. I believe I just mistyped on here to explain my problem....

                  • 6. Re: Addition Script
                    gkaiseril MVP & Adobe Community Professional

                    With a form created in Acrobat, not LiveCycle Designer, you can use any of the 3 calculation options. With the "Field is the ___ of fields:" just check the field names. You can use the 'simplified field notation' and just enter the field names and the operation.

                     

                    If you want to use the "Custom JavaScript calculation" you need to be aware that the '+' operator is not only the addition operator for numbers but also the concatenation operator for non-numeric strings. JavaScript will make the determination unless you use a special constrictor or operation to force a value to a number or string. Both the other 2 calculation methods force the values of the identified fields to a numeric value or exclude the field from the calculation. You can do the same with a function. It is best to make a function independent of field names so it can be used in many more applications. I would also look at using hierarchical field names.

                     

                    I would make an array variable of the field names and then using that array to make an array of the values and then process the values.

                     

                    function Field2Value(aFields) {
                    var aValues = new Array(aFields.length);
                    for(i = 0; i < aFields.length; i++) {
                    aValues[i] = this.getField(aFields[i]).value;
                    }

                    return aValues;
                    } // end Field2Value

                    function SumArray(aValues) {
                    var sum = 0;
                    for(i = 0; i < aValues.length; i++) {
                    if(isNaN(aValues[i])) aValues[i] = 0;
                    sum += Number(aValues[i]);
                    }

                    return sum;
                    } // end SumArray

                     

                    var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",

                    "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6," "AverageDiv7",

                    "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",

                    "AverageDiv12", "AverageDiv13");

                     

                    event.value = SumArray( Field2Value(myFields) );

                     

                     

                    With hierarchical field names (AverageDiv.0, AverageDiv.1, etc) the scripts can become:

                     

                    function Hfield2Value(oField) {
                    var aFields = oField.getArray();
                    var aValues = new Array(aFields.length);
                    for(i = 0; i < aFields.length; i++) {
                    aValues[i] = aFields[i].value;
                    }
                    return aValues;
                    } // end Hfield2Value

                     

                    function SumArray(aValues) {
                    var sum = 0;
                    for(i = 0; i < aValues.length; i++) {
                    if(isNaN(aValues[i])) aValues[i] = 0;
                    sum += Number(aValues[i]);
                    }

                    return sum;
                    } // end SumArray

                     

                    event.value = SumArray( Hfield2Value( this.getField('AverageDiv') ) );

                    • 7. Re: Addition Script
                      burlingtonnow Level 1

                      Hey George,

                       

                      For this script:

                       

                      function Field2Value(aFields) {
                      var aValues = new Array(aFields.length);
                      for(i = 0; i < aFields.length; i++) {
                      aValues[i] = this.getField(aFields[i]).value;
                      }

                      return aValues;
                      } // end Field2Value

                      function SumArray(aValues) {
                      var sum = 0;
                      for(i = 0; i < aValues.length; i++) {
                      if(isNaN(aValues[i])) aValues[i] = 0;
                      sum += Number(aValues[i]);
                      }

                      return sum;
                      } // end SumArray

                       

                      var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",

                      "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6," "AverageDiv7",

                      "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",

                      "AverageDiv12", "AverageDiv13");

                       

                      event.value = SumArray( Field2Value(myFields) );

                       

                      I am getting this message: Missing ) 24; at line 25

                       

                       

                      Note I am using new Array ("AverageDiv","AverageDiv1....ect)

                      • 8. Re: Addition Script
                        gkaiseril MVP & Adobe Community Professional

                        The quotation mark for the field name 'AverageDiv6" is in the wrong location.

                         

                        The declaration for the array should read:


                        var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",
                        "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6", "AverageDiv7",
                        "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",
                        "AverageDiv12", "AverageDiv13");

                        • 9. Re: Addition Script
                          burlingtonnow Level 1

                          George that seem to work. I have one more question.  Will the script that you gave me round up whole numbers when they are suppose to round ? Or can you script that factor in the script you gave me.

                           

                           

                          Thanks

                          • 10. Re: Addition Script
                            gkaiseril MVP & Adobe Community Professional

                            A simple function:

                             

                            function Round(nValue, nDec)
                            {
                            return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
                            }

                             

                            Now you need to decide where to apply it. If you want each average dividend field rounded, I would use the function in that field. If it for the sum of the average dividends,  then use it that field.

                            • 11. Re: Addition Script
                              burlingtonnow Level 1

                              Hello George,

                               

                              I think I want it in the sum of the average dividends but I don't have a clue where to place this script...

                               

                               

                              function Round(nValue, nDec)
                              {
                              return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
                              }

                              • 12. Re: Addition Script
                                burlingtonnow Level 1

                                George, one other thing.   I have this script in the Custom Calculation Script:

                                 

                                function Field2Value(aFields) {
                                var aValues = new Array(aFields.length);
                                for(i = 0; i < aFields.length; i++) {
                                aValues[i] = this.getField(aFields[i]).value;
                                }

                                return aValues;
                                } // end Field2Value

                                function SumArray(aValues) {
                                var sum = 0;
                                for(i = 0; i < aValues.length; i++) {
                                if(isNaN(aValues[i])) aValues[i] = 0;
                                sum += Number(aValues[i]);
                                }

                                return sum;
                                } // end SumArray

                                 

                                var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",
                                "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6", "AverageDiv7",
                                "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",
                                "AverageDiv12", "AverageDiv13");

                                 

                                event.value = SumArray( Field2Value(myFields) );

                                function Round(nValue, nDec)
                                {
                                return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
                                }

                                 

                                 

                                I know I can't place this script in the Simplified Field Notation

                                 

                                function Round(nValue, nDec)
                                {
                                return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
                                }

                                 

                                 

                                Please help

                                • 13. Re: Addition Script
                                  gkaiseril MVP & Adobe Community Professional

                                  You can continue to nest the calls.


                                  // define the functions - can be placed in the document level functions section;
                                  function Field2Value(aFields) {
                                  var aValues = new Array(aFields.length);
                                  for(i = 0; i < aFields.length; i++) {
                                  aValues[i] = this.getField(aFields[i]).value;
                                  }

                                  return aValues;
                                  } // end Field2Value;

                                  function SumArray(aValues) {
                                  var sum = 0;
                                  for(i = 0; i < aValues.length; i++) {
                                  if(isNaN(aValues[i])) aValues[i] = 0;
                                  sum += Number(aValues[i]);
                                  }

                                  return sum;
                                  } // end SumArray;

                                  function Round(nValue, nDec)
                                  {
                                  return Number(util.printf( "%,0 ." + Number(nDec) + "f", Number(nValue))) ;
                                  } // end Round function;


                                  // custom calculation javascript
                                  var myFields = new Array("AverageDiv", "AverageDiv1", "AverageDiv2",
                                  "AverageDiv3", "AverageDiv4", "AverageDiv5", "AverageDiv6", "AverageDiv7",
                                  "AverageDiv8", "AverageDiv9", "AverageDiv10", "AverageDiv11",
                                  "AverageDiv12", "AverageDiv13");

                                   

                                  // nest the calls to get the values or the field, sum the values, and round the result;
                                  event.value = Round( SumArray( Field2Value(myFields) ), 2);

                                   

                                  Or for the event.value:

                                   

                                  // get the values of the fields;

                                  var aValues =   Field2Value(myFields);

                                  // sum the values;

                                  var nSum = SumArray(aValues);

                                  // round the values;

                                  nSum = Round(nSum, 2);

                                  // set the field value;

                                  event.value = nSum;

                                  • 14. Re: Addition Script
                                    gkaiseril MVP & Adobe Community Professional

                                    duplicate post.

                                    • 15. Re: Addition Script
                                      gkaiseril MVP & Adobe Community Professional

                                      duplicate post.

                                      • 16. Re: Addition Script
                                        burlingtonnow Level 1

                                        Hi, the script you gave me I placed it in the Custom Calculation Script

                                        I don't believe it is working right.

                                         

                                        I am just adding 2 of the 13 AverageDiv as a test.  AverageDiv12 field adds up to 77and AverageDiv13 field adds up to 92  when you add these two fields up it should = 169 but with this script I am getting a total of 168. I am not using decimals but when you click on the total field it reads 168.4

                                        I know that 77+92 =169 what am I doing wrong.

                                        • 17. Re: Addition Script
                                          burlingtonnow Level 1

                                          Again Thanks for all of you help!!!

                                          • 18. Re: Addition Script
                                            gkaiseril MVP & Adobe Community Professional

                                            For the call to the Round function, change the 2 to 0.