3 Replies Latest reply on Aug 10, 2015 6:49 PM by stermano

    IF THEN Statement JavaScript

    stermano Level 1

           I am trying to calculate a total number by using the following:

       

      I have 8 rows with three columns that will feed into a calculation depending on what is selected in a collection of drop boxes

       

      Element (value: description), Metric (value: number); FabricType (drop-down values: PW, HLP, HC, UHC)

       

      The fields with calculations are: Total HC, Total PW, Total HLP, and Total UHC.

       

      I need to calculate the totals across all 8 rows IF the Fabric Type selected corresponds to the Total being requested.

       

      So, if:

       

      Element          Metric          FabricType

      A                    4.00               PW

      B                    5.00               PW

      C                    1.00               HLP

      D                    3.00               HC

      E                    9.00               PW

      F                    3.00               UHC

      G                   4.00               UHC

      H                    3.00               HLP

       

      What is the javascript code to calculate each of the following fields:

       

      Total UHC

      Total PW

      Total HLP

      Total HC

       

      based on the FabricType drop-down, and what is entered in the Metric field

        • 1. Re: IF THEN Statement JavaScript
          try67 MVP & Adobe Community Professional

          A good place to start learning about this subject: https://acrobatusers.com/tutorials/conditional-execution

          • 2. Re: IF THEN Statement JavaScript
            stermano Level 1

            Here is what I was able to put together, but it's not calculating correctly.  It is giving me the sum of all of the M column (labeled as V1... in the code) regardless of what is selected in the Fabric Column.  I'm new to javascript so I think I'm way off.  But this is what I've compiled based on what I've read.  Any help is appreciated.

            pdfexample.png

            var v1 = this.getField("gfm_e1_m").value;
            var v2 = this.getField("gfm_e2_m").value;
            var v3 = this.getField("gfm_e3_m").value;
            var v4 = this.getField("gfm_e4_m").value;
            var v5 = this.getField("gfm_e5_m").value;
            var v6 = this.getField("gfm_e6_m").value;
            var v7 = this.getField("gfm_e7_m").value;
            var v8 = this.getField("gfm_e8_m").value;
            var Fabric1 = this.getField("gfm_e1_dd").value;
            var Fabric2 = this.getField("gfm_e2_dd").value;
            var Fabric3 = this.getField("gfm_e3_dd").value;
            var Fabric4 = this.getField("gfm_e4_dd").value;
            var Fabric5 = this.getField("gfm_e5_dd").value;
            var Fabric6 = this.getField("gfm_e6_dd").value;
            var Fabric7 = this.getField("gfm_e7_dd").value;
            var Fabric8 = this.getField("gfm_e8_dd").value;

            if (Fabric1 == HC && Fabric2 == HC && Fabric3 == HC && Fabric4 == HC && Fabric5 == HC && Fabric6 == HC && Fabric7 == HC && Fabric8 == HC) {
                sum = "gmf_e1_m" && "gmf_e2_m" && "gmf_e3_m" && "gmf_e4_m" && "gmf_e5_m" && "gmf_e6_m" && "gmf_e7_m" && "gmf_e8_m";
            }

            • 3. Re: IF THEN Statement JavaScript
              stermano Level 1

              Well I found it on my own from another post in this forum.  Here is the script I used and it works beautifully!!

               

              function SumIfEq(aIndex, aRange, cValue) {

              // function to sum cRange for item in cIndex that equals  cValue;

              // do not change code below;

              var nSum = 0; // variable to sum payment type;

              // verify that there are an equal number of fields;

              if(aIndex.length != aRange.length) {

                 app.alert("The number of m fields and Fabric fields must be equal!", 0, 0);

                 } else {

                 // loop through the fields

                 for(i = 0; i < aIndex.length; i++) {

                 if(this.getField(aIndex[i]).value == cValue) {

                    // add the payment to the sum;

                    nSum += Number(this.getField(aRange[i]).value);

                    } // end match payment type;

                 } // end loop through the fields;

              } // end field length;

              return nSum; // return computed sum;

              } // end SumIfEq function

              // custom calculation script using the SumIfEq function;

              // using arrays of field names;

              var aIndex = new Array("GFM_e1_t", "GFM_e2_t", "GFM_e3_t", "GFM_e4_t", "GFM_e5_t", "GFM_e6_t", "GFM_e7_t", "GFM_e8_t");

              var aRange = new Array("GFM_e1_m", "GFM_e2_m", "GFM_e3_m", "GFM_e4_m", "GFM_e5_m", "GFM_e6_m", "GFM_e7_m", "GFM_e8_m"); 

              // call sum if equal for payment "Type", for "Amount", equal to "HC";

              event.value = SumIfEq(aIndex, aRange, "HC");

              // end custom calculation script;

               

              Thank you @GKaiseril ...even if you didn't know you helped!