12 Replies Latest reply on Aug 25, 2017 6:59 AM by esterk42897401

    count number of specific answers

    esterk42897401

      Hi,

       

      I've created a PDF form with yes/no questions. In the last field I want to see the summary of the amount of 'ja' (= yes) answers.

      The field ID's range from Tekstveld 1 (= Text Field) to Tekstveld 37, with no. 38 as the 'total' field.

      I've found a Java code and tried to edit it to my needs, but I'm not getting it...

       

       

      // Initialize counter variable

      var sum = 0;

      // Loop through the fields

      for (var i = 1; i < 37; i += 1) {

      // Add one if Teksveld is set to ja

          if (getField("Tekstveld" + i).value !== "ja") {sum += 1;}

      }

      // Set this field's value to the sum

      event.value = sum;

       

       

      Can someone help me to get it right?

      Thanks!

        • 1. Re: count number of specific answers
          try67 MVP & Adobe Community Professional

          - You need to change i<37 to i<=37

          - You need to change value !== "ja" to value=="ja"

          The rest seems fine.

          • 2. Re: count number of specific answers
            esterk42897401 Level 1

            Hi try67,

             

            Thanks for responding so quickly

            Still having issues though, I still must be doing something wrong. This is the new code:

             

             

            // Initialize counter variable
            var sum = 0;
            // Loop through the fields
            for (var i = 1; i <= 37; i += 1) {
            // Add one if Tekstveld is set to ja
                if (getField("Tekstveld" + i).value== "ja") {sum += 1;}
            }
            // Set this field's value to the sum
            event.value = sum;

             

            If this code is correct, maybe I'm adding the code to wrong place? This is where I added the code: Properties -> Calculate -> Custom calculation script

            • 3. Re: count number of specific answers
              try67 MVP & Adobe Community Professional

              The code seems correct and the location is also correct.

              Change the value of one of the fields in the file and then press Ctrl+J and check if there are any error messages in the JS Console window that opens.

              • 4. Re: count number of specific answers
                esterk42897401 Level 1

                I changed 37 to 64. So new code is:

                 

                // Initialize counter variable

                var sum = 0;

                // Loop through the fields

                for (var i = 1; i <= 64; i += 1) {

                // Add one if Tekstveld is set to ja

                    if (getField("Tekstveld" + i).value== "ja") {sum += 1;}

                }

                // Set this field's value to the sum

                event.value = sum;

                 

                This is the error log:

                 

                Acrobat EScript Built-in Functions Version 10.0
                Acrobat SOAP 10.0

                SyntaxError: syntax error
                4:
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                SyntaxError: syntax error
                4:
                SyntaxError: syntax error
                4:
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate
                TypeError: getField("Tekstveld" + i) is null
                6:Field:Calculate

                • 5. Re: count number of specific answers
                  try67 MVP & Adobe Community Professional

                  That means that some of the field names you're trying to access don't exist. Use this code to get a better idea of which ones they are:

                   

                  for (var i = 1; i <= 64; i += 1) {

                      var fname = "Tekstveld" + i;

                      var f = this.getField(fname);

                      if (f==null) {console.println(fname + " doesn't exist.");}

                      else if (f.value== "ja") {sum += 1;}

                  }

                  • 6. Re: count number of specific answers
                    esterk42897401 Level 1

                    wow, all text fields don't seem to exist...?

                    Isn't the code referring to the names of the fields?

                    Do Ihave to change something in the properties?

                     

                    Acrobat EScript Built-in Functions Version 10.0
                    Acrobat SOAP 10.0

                    SyntaxError: syntax error
                    4:
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    SyntaxError: syntax error
                    4:
                    SyntaxError: syntax error
                    4:
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:Calculate
                    TypeError: getField("Tekstveld" + i) is null
                    6:Field:CalculateTekstveld1 doesn't exist.
                    Tekstveld2 doesn't exist.
                    Tekstveld3 doesn't exist.
                    Tekstveld4 doesn't exist.
                    Tekstveld5 doesn't exist.
                    Tekstveld6 doesn't exist.
                    Tekstveld7 doesn't exist.
                    Tekstveld8 doesn't exist.
                    Tekstveld9 doesn't exist.
                    Tekstveld10 doesn't exist.
                    Tekstveld11 doesn't exist.
                    Tekstveld12 doesn't exist.
                    Tekstveld13 doesn't exist.
                    Tekstveld14 doesn't exist.
                    Tekstveld15 doesn't exist.
                    Tekstveld16 doesn't exist.
                    Tekstveld17 doesn't exist.
                    Tekstveld18 doesn't exist.
                    Tekstveld19 doesn't exist.
                    Tekstveld20 doesn't exist.
                    Tekstveld21 doesn't exist.
                    Tekstveld22 doesn't exist.
                    Tekstveld23 doesn't exist.
                    Tekstveld24 doesn't exist.
                    Tekstveld25 doesn't exist.
                    Tekstveld26 doesn't exist.
                    Tekstveld27 doesn't exist.
                    Tekstveld28 doesn't exist.
                    Tekstveld29 doesn't exist.
                    Tekstveld30 doesn't exist.
                    Tekstveld31 doesn't exist.
                    Tekstveld32 doesn't exist.
                    Tekstveld33 doesn't exist.
                    Tekstveld34 doesn't exist.
                    Tekstveld35 doesn't exist.
                    Tekstveld36 doesn't exist.
                    Tekstveld37 doesn't exist.

                    • 7. Re: count number of specific answers
                      Bernd Alheit Adobe Community Professional & MVP

                      What are the exact names of the fields?

                      • 8. Re: count number of specific answers
                        esterk42897401 Level 1

                        The names are exactly as written in the error log....

                        So: Tekstveld1, Tekstveld2 etc.

                        • 9. Re: count number of specific answers
                          try67 MVP & Adobe Community Professional

                          Are you sure there isn't a space or a period between the text and the

                          number? Can you share the file with us (via Dropbox, Google Drive, Adobe

                          Cloud, etc.)?

                           

                          On Fri, Aug 25, 2017 at 1:00 PM, esterk42897401 <forums_noreply@adobe.com>

                          • 10. Re: count number of specific answers
                            esterk42897401 Level 1

                            I've tried different ways, none of them seem to be working.

                            this is the file: Dropbox - Website-effectiviteitstest.pdf

                            • 11. Re: count number of specific answers
                              try67 MVP & Adobe Community Professional

                              As I suggested, there's a space between the text and the number in all of the field names... Use this code:

                              // Initialize counter variable
                              var sum = 0; 
                              // Loop through the fields
                              for (var i = 1; i <= 37; i += 1) {
                              // Add one if Tekstveld is set to ja
                                  if (getField("Tekstveld " + i).valueAsString.toLowerCase()=="ja") {sum += 1;}
                              } 
                              // Set this field's value to the sum
                              event.value = sum;
                              

                               

                              Also, for some reason you've set the Format of the total field as "Zip code". Change it to None or to Number.

                              I would also recommend that you use a check-box or at least a drop-down instead of open text fields.

                              If someone enters "JA", for example, or "Ja", or "X", or anything else instead of "ja", the script above will not count it, which is not good.

                               

                              Edit: added the code, and fixed it so that it will recognize all versions of "ja", regarding of upper/lower-case letters.

                              • 12. Re: count number of specific answers
                                esterk42897401 Level 1

                                Yes!! It works

                                That zipcode setting must have come up when I was testing locations... silly of me to have left it there.

                                Thanks a lot, I really appreciate your help!!