7 Replies Latest reply on Sep 6, 2011 3:01 PM by George_Johnson

    Using field names in custom script?

    Akexis

      I have a form where a user inputs values into some text fields and it averages out a score.  I need to write a custom script in Calculate to check two fields and see if they're zeros and if so exclude them.

       

      I've checked the code in an html page which includes the javascript and know it works, but I'm not sure how to work with the field names.

       

      var scores=[BM1Score.0,BM2Score.0];

       

      if(BM3Score.0 > 0) scores.push(BM3Score.0);

       

      if(BM4Score.0 > 0) scores.push(BM4Score.0);

       

      var average = scores.avg();

       

      Basically, I made an array and if the two fields I'm checking are greater than 0, I add them to the array and then average the scores.

       

      I get this error though.

       

      missing ] after element list
      1: at line 2

       

      I tried to convert the field names to variables.

       

      var score1 = BM1Score.0;
      var score2 = BM2Score.0;
      var score3 = BM3Score.0;
      var score4 = BM4Score.0;

       

      var scores=[score1,score2];

       

      if(score3 > 0) scores.push(score3);

       

      if(score4 > 0) scores.push(score4);

       

      var average = scores.avg();

       

      But get this error.

       

      missing ; before statement
      1: at line 2

       

      Any insight as to how to get this working would be greatly appreciated!

       

      Cheers!

        • 1. Re: Using field names in custom script?
          George_Johnson MVP & Adobe Community Professional

          In Acrobat JavaScript, that first bit of code would have to be something like:

           

          var scores = [+getField("BM1Score.0").value, +getField("BM2Score.0").value];
          
          if (+getField("BM3Score.0").value > 0) scores.push(+getField("BM3Score.0").value);
          if (+getField("BM4Score.0").value > 0) scores.push(+getField("BM4Score.0").value);
          
          var average = scores.avg();
          
          // Set this field value to the average
          event.value = average;
          

           

          You need to work with the field values, which you get through the value property of a field object.

          1 person found this helpful
          • 2. Re: Using field names in custom script?
            Akexis Level 1

            var score1 = this.getField("BM1Score.0");
            var score2 = this.getField("BM2Score.0");
            var score3 = this.getField("BM3Score.0");
            var score4 = this.getField("BM4Score.0");

            var scores=[score1,score2];

            if(score3 > 0) scores.push(score3);

            if(score4 > 0) scores.push(score4);

            var average = scores.avg();

            document.write(average);

             

            I'm kind of a crappy programmer, so bear with me.  Once I decided to get the user input and convert it to a variable, I wasn't getting an error.  I left out the print statement at the end, but it wasn't showing an average with my test values, but with the print statement it's still not showing a final output value.

             

            I've been at this for hours and will still be going for a while, so any interim help would be appreciated.  Thanks!

            • 3. Re: Using field names in custom script?
              George_Johnson MVP & Adobe Community Professional

              Also, JavaScript arrays do not have a built-in "avg" method that averages the values in the array. It may be that the code you're copying is relying on the array prototype being extended with a custom avg method. You'll have to do the same type of thing or create your own averaging function.

               

              If you want to create your own, you'll have to decide what you want to do if a field is blank. Should it be included in the average calculation or not?

              1 person found this helpful
              • 4. Re: Using field names in custom script?
                gkaiseril MVP & Adobe Community Professional

                Have you opened the Acrobat JavaScript console to see if there are any errors?

                 

                You are coding Acrobat JavaScript and not web JavaScript. Three are some differences between them. There is no "document.write(average);" in Acrobat JavaScript.

                 

                You are calling a function "scores.avg();" , where is the code for this function. Averages include a division and Acrobat JavaScript does not create a number number for this calculation.

                • 5. Re: Using field names in custom script?
                  Akexis Level 1

                  First of all, you guys are amazing and thanks for the help so far!

                   

                  My brain has turned to mush.  It seems to be adding a value to the array?  When I only have one or two values, it seems to be adding an extra zero value to the array.  When I add a third value, it works.  With a fourth value it works as well.  It's putting in a zero for some reason which is screwing up the average.

                   

                  var scores = [+getField("BM1Score.0").value, +getField("BM2Score.0").value];

                  if (+getField("BM3Score.0").value > 0) scores.push(+getField("BM3Score.0").value);
                  if (+getField("BM4Score.0").value > 0) scores.push(+getField("BM4Score.0").value);

                  avgNum = scores.length;
                  avgTotal = 0;

                  for(var i = 0; i < scores.length; i++)
                    {
                    avgTotal += scores[i];
                    }

                  var average = avgTotal / avgNum;

                  event.value = average;

                  • 6. Re: Using field names in custom script?
                    George_Johnson MVP & Adobe Community Professional

                    The code that I posted will convert a blank value to zero. If you want to ignore blank values, the script would have to be modified.

                     

                    It would help if you provided more information about how things are set up and how you want it to behave. It seems like you always want to include the first two fields. Is that what you want and will those fields always have a value (i.e., not blank)? Do you want to ignore blank fields?

                    • 7. Re: Using field names in custom script?
                      Akexis Level 1

                      OMG, nevermind.

                       

                      I said my brain turned to mush.  The code above works perfectly - it's solved.

                       

                      Thank you all so much!

                       

                      And yes, the first two fields always have a value, but the last two might not, which is why they weren't part of the array in the beginning.  I need sleep.  Thanks again.