9 Replies Latest reply on Mar 17, 2008 7:40 AM by (Sharyn_Zamora)

    Calculate a Column of Figures, Acrobat 7 Pro/XP

      I'm no Javascripter or programmer but I've figured out how to calculate my form using a model of some JavaScript and to my great surprise and joy, it's working! I am wondering if there is a shortcut/better way to sum a column of figures other than the way I'm doing it which follows below.

      I'm convinced there must be a better way but haven't found an answer in forum searches, online Help, or otherwise. I thought maybe an array and came across an example in the Acrobat JavaScript Scripting Guide (p. 101), however I cannot find out anything about how to incorporate the AFSimple_Calculate function that it refers to; I wasn't successful using this model. I'm uncertain if it's even the correct application.

      My script follows--it works!--just wonder if better way:

      //LINE Sched1.12.3
      var a = GF("Sched1.1.3")
      var b = GF("Sched1.2.3")
      var c = GF("Sched1.3.3")
      var d = GF("Sched1.4.3")
      var e = GF("Sched1.5.3")
      var f = GF("Sched1.6.3")
      var g = GF("Sched1.7.3")
      var h = GF("Sched1.8.3")
      var i = GF("Sched1.9.3")
      var j = GF("Sched1.10.3")
      var k = GF("Sched1.11.3")
      var result = a + b + c + d + e + f + g + h + i + j + k
      if(result<=0)result=""
      getField("Sched1.12.3").value = result

      If you have a solution, please "talk" to me like I'm a "baby" as I'm very new to Javascripting and have only a very basic understanding of it. Thanking you in advance if you can help me learn something new!
        • 1. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
          gkaiseril MVP & Adobe Community Professional
          You will not see much documentation on the built-in functions provided with Acrobat/Reader. Unless you are a programer and know how to reverse engineer code you will not know how to list and examine the code.

          The following script will sum a list of fields:

          function PFArray_Sum(aFields) {
          var sum = 0; // variable for the sum
          // for each element in aFields sum the numeric values
          for(i in aFields)
          sum += Number(this.getField(aFields[i]).value);

          return sum;
          } // end PFArray_Sum function

          // make an array of the field names to sum
          var aFieldNames = new Array("Sched.1.1.3", "Sched.1.2.3", "Sched.1.3.3",
          "Sched.1.4.3", "Sched.1.5.3","Sched.1.6.3", "Sched.1.7.3", "Sched.1.8.3",
          "Sched.1.9.3", "Sched.1.10.3","Sched.1.11.3");

          event.value = PFArray_Sum(aFieldNames);
          if (event.value == 0) event.value = "";

          If you have a number of fields that require the summing of a number of fields, you can place the function "PFArray_Sum" in the document level function script area.
          • 2. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
            Level 1
            Geo: Thank you for the quick reply! However, it isn't working for me and I'm unsure why.
            1) I created a document level function "PFArray_Sum" that contains the script you gave above. I got a syntax error without a second ")" on your line 5 just ahead of the semicolon, so I added the ")" and the error doesn't occur.
            2) I placed the script in my Calc function for the specific line that I want to calculate: Your script above, "//make an array..." through the last line "if (event.value == 0)..."
            3) What did I do wrong?
            4) Question: Is "(aFields)" suppose to be different from "FieldNames" as you typed above, or are they supposed to match?
            5) What does "==" mean?
            • 3. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
              MarkWalsh Level 4
              If all you are doing is summing a bunch of fields, you may not need to use Javascript.

              Go to the 'Calculate' tab for the field, and check the 'Value is the' radio button, choose "sum" and then enter the field name. If your fields all start with 'Sched1.X.X' then you could just enter 'Sched1', and it will sum all fields that start with 'Sched1.'

              NOTE - just noticed your sum field is also named 'Sched1.12.3'. You would have to change that field name for this to work. You could name it 'Sched1Sum'
              • 4. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                Level 1
                Mark: Thank you... I knew that and that's actually how I first did the calculation. However, the form is a big file--20 pages--and I've since learned that it's better to calculate with Javascript and so even though I don't know it, I'm trying to do it that way. This form is on our website and needs to be as compact and efficient as possible, so that's my goal. If I can avoid putting formulas into each field, I'd rather. Thank you for your response!
                • 5. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                  MarkWalsh Level 4
                  Check the 'getArray' method of a field in the Acrobat javascript reference. There is this example to loop through all of a field's children:

                  // f has 3 children: f.v1, f.v2, f.v3
                  var f = this.getField("f");
                  var a = f.getArray();
                  var v = 0.0;
                  for (j =0; j < a.length; j++) v += a[j].value;
                  // v contains the sum of all the children of field "f"
                  • 6. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                    Level 1
                    Thank you Mark! I'll give it a try.
                    • 7. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                      Level 1
                      Mark: I put the following code into the field where I want the column total but it doesn't sum:
                      // sums the column e array

                      var f = this.getField("A3e")
                      var a = f.getArray() ;
                      var v = 0.0;
                      for (j =0; j < a.length; j++) v += a[j].value;

                      // v contains the sum of all the children of field "A3e"

                      I guess I'm back to long way. I just thought there would be a better more efficient way.

                      Thanks to you and Geo for trying to help. I appreciate it.
                      • 8. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                        gkaiseril MVP & Adobe Community Professional
                        I just cut and pasted the code into a form created in Acrobat 5 and tested with version 7 without any problems. You should very carefully check the code.

                        Document Level function:

                        function PFArray_Sum(aFields) {
                        var sum = 0; // variable for the sum
                        // for each element in aFields sum the numeric values
                        for(i in aFields)
                        sum += Number(this.getField(aFields[i]).value);

                        return sum;
                        } // end PFArray_Sum function

                        "Custom Calculation sript" for the specific total field:

                        // make an array of the field names to sum
                        var aFieldNames = new Array("Sched.1.1.3", "Sched.1.2.3", "Sched.1.3.3",
                        "Sched.1.4.3", "Sched.1.5.3","Sched.1.6.3", "Sched.1.7.3", "Sched.1.8.3",
                        "Sched.1.9.3", "Sched.1.10.3","Sched.1.11.3");

                        event.value = PFArray_Sum(aFieldNames);
                        if (event.value == 0) event.value = "";

                        >4) Question: Is "(aFields)" suppose to be different from "FieldNames" as you typed above, or are they supposed to match?

                        "aFieldNames" is a variable array of the field names to be added.

                        "aFields" is the parameter for the passed variable to the function. This parameter is assumed to be an array by the function and handled as an array.

                        >5) What does "==" mean?

                        The "==" is the equality operator.

                        This code is designed to work only in Acrobat ArcoForms and not LiveCycle Designer.
                        • 9. Re: Calculate a Column of Figures, Acrobat 7 Pro/XP
                          Level 1
                          Geo: I've been out of the office and now have to put this task on the back burner but I'll pursue your last suggestion. Thank you again!