8 Replies Latest reply on Jun 3, 2008 9:30 AM by gkaiseril

    Using Javascript to create concatenated string from checkbox fields to one text field

      Hi. I have a PDF form that I am trying to have output to a spreadsheet that matches my database schema. Here is the dilemna:
      * I have a set of checkboxes for available languages (LANGUAGE_ENGLISH, LANGUAGE_SPANISH, etc.) When they export to spreadsheet, the value is TRUE.
      * I need to take values from checked boxes and create a single string in a text field called LANGUAGE_DISPLAY (so my UI will not need to do the concatenation). If LANGUAGE_ENGLISH is TRUE (checked), append "English, " to LANGUAGE_DISPLAY, else append "". Then, if LANGUAGE_SPANISH is TRUE (checked), append "Spanish, " to LANGUAGE_DISPLAY, else append "". And on and on

      In the LANGUAGE_DISPLAY text field properties, I am inserting a Custom Calculation script to try to achieve this, but am not getting any results. I tried teh following even trying to pull the checkboxes default values and string them together:
      box1 = this.getField("LANGUAGE_ENGLISH").value.toSrting();
      box2 = this.getField("LANGUAGE_FARSI").value.toSrting();
      box3 = this.getField("LANGUAGE_MANDARIN").value.toSrting();
      event.value = box1 + ', ' + box2 + ', ' + box3;

      I also played with this to get the desired strings output...but to no avail:
      if ( LANGUAGE_ENGLISH.rawValue == true )
      {
      box1.rawValue = "English, ";
      }
      if ( LANGUAGE_FARSI.rawValue == true )
      {
      box1.rawValue = "Farsi, ";
      }
      if ( LANGUAGE_HEBREW.rawValue == true )
      {
      box1.rawValue = "Hebrew, ";
      }
      event.value = box1 + box2 + box3;

      Then I tried to simplify to see one field output so used this script...still no results:
      event.value = "";
      var f = this.getField("LANGUAGE_ENGLISH");
      if ( f.isBoxChecked() == true) {
      event.value = "English";
      }

      Couple questions:
      1) Am I on the right track with any of these scripts?
      2) Is there something else I need to do to get the script to run before running the Create Spreadsheet with Data Files comman in Acrobat to get my csv file output? Maybe there needs to be some event to get the checkbox values read by that field in order to calculate/create the string.

      Appreciate any help you can provide.
        • 1. Re: Using Javascript to create concatenated string from checkbox fields to one text field
          gkaiseril MVP & Adobe Community Professional
          If you are using LiveCycle Desinger, use FormCalc, set the export values to the language and use the 'Concat()' funciton to combine the strings.
          • 2. Re: Using Javascript to create concatenated string from checkbox fields to one text field
            Level 1
            Thanks for your comment, Geo. Unfortunately, I am using Acrobat 8 and do not have LifeCycle Designer. Any thoughts about how this can be done using the default Acrobat Pro application?
            • 3. Re: Using Javascript to create concatenated string from checkbox fields to one text field
              gkaiseril MVP & Adobe Community Professional
              LiveCycle Designer has shipped with all Acrobat Professional versions since the "Professional" version was introduced with version 6.

              You do not let us know want results you get in the field or the JavaScript console.

              Using:

              box1 = this.getField("LANGUAGE_ENGLISH").value.toString();
              box2 = this.getField("LANGUAGE_FARSI").value.toString();
              box3 = this.getField("LANGUAGE_MANDARIN").value.toString();
              event.value = box1 + ', ' + box2 + ', ' + box3;

              returns "Off, Off, Off", when no box is checked and returns "Yes" for the appropriate box being checked when the default value is used for the creation of the check box. So if one would make the 'Export Value' of the box from the default value of 'Yes" to the appropriate language, one would get a more desirable result. But for each unchecked box the value would appear as "Off". So one needs to change the 'Off' value to a null string. But one is still left with the separator when there is an unchecked option.

              Using the following document level function:

              // Concatenate 3 strings with separators where needed
              function fillin(s1, s2, s3, sep) {

              /*
              Purpose: concatenate up to 3 strings with an optional separator

              inputs:
              s1: required input string text or empty string
              s2: required input string text or empty string
              s3: required input string text or empty string
              sep: optional separator sting

              returns:
              sResult concatenated string
              */

              // variable to determine how to concatenate the strings
              var test = 0; // all strings null
              var sResult; // re slut string to return

              // force any number string to a character string for input variables
              s1 = s1.toString();
              s2 = s2.toString();
              s3 = s3.toString();
              if(sep.toString() == undefined) sep = ''; // if sep is undefined force to null

              /*
              assign a binary value for each string present
              so the computed value of the strings will indicate which strings are present
              when converted to a binary value
              */
              if (s1 != "") test += 1; // string 1 present add binary value: 001
              if (s2 != "") test += 2; // string 2 present add binary value: 010
              if (s3 != "") test += 4; // string 3 present add binary value: 100

              /* return appropriate string combination based on
              calculated test value as a binary value
              */
              switch (test.toString(2)) {

              case "0": // no non-empty strings passed - binary 0
              sResult = "";
              break;

              case "1": // only string 1 present - binary 1
              sResult = s1;
              break;

              case "10": // only string 2 present - binary 10
              sResult = s2;
              break;

              case "11": // string 1 and 2 present - binary 10 + 1
              sResult = s1 + sep + s2;
              break;

              case "100": // only string 3 present - binary 100
              sResult = s3;
              break;

              case "101": // string 1 and 3 - binary 100 + 001
              sResult = s1 + sep + s3;
              break;

              case "110": // string 2 and 3 - binary 100 + 010
              sResult = s2 + sep + s3;
              break;

              case "111": // all 3 strings - binary 100 + 010 + 001
              sResult = s1 + sep + s2 + sep + s3;
              break;

              default: // any missed combinations
              sResult = "";
              break;
              }

              return sResult;
              }

              And the following cleaned up custom calculation script:

              box1 = this.getField("LANGUAGE_ENGLISH").value;
              box2 = this.getField("LANGUAGE_FARSI").value;
              box3 = this.getField("LANGUAGE_MANDARIN").value;
              if (box1 == 'Off') box1 = '';
              if (box2 == 'Off') box2 = '';
              if (box3 == 'Off') box3 = '';
              event.value = fillin(box1, box2, box3, ', ');

              One will get the list of languages with the optional separator for 2 or more language selections.
              • 4. Re: Using Javascript to create concatenated string from checkbox fields to one text field
                Level 1
                Thanks for the detailed response. That is extremely helpful...the only question is since I have 16 language fields/checkboxes (not only 3), is there a simpler way than converting each field to a binary value and then having to interpret the summed value to determine which values were true and therefore should be present?
                • 5. Re: Using Javascript to create concatenated string from checkbox fields to one text field
                  gkaiseril MVP & Adobe Community Professional
                  You repeat using the "fillin()" funciton adding 2 more strings to the result of the previous call.
                  • 6. Re: Using Javascript to create concatenated string from checkbox fields to one text field
                    MarkWalsh Level 4
                    One suggestion I'd give you would be to change your naming convention to use dots instead of underscores. If all of your fields were named 'LANGUAGE.ENGLISH', 'LANGUAGE.FARSI' etc. you could just loop through all of the fields of 'LANGUAGE' concatenating the results (from the export value for that field). The benefit would be that this would still work without changing the code even if you had to add 100 new language fields.

                    WIth the fields named as such, then you can use the following for your concatenation calculation:

                    var fieldValue
                    var newFieldValue
                    var delimiter

                    newFieldValue = ""
                    delimiter = ", "

                    var oField = this.getField("LANGUAGE");
                    if (oField != null) {
                    var a = oField.getArray();
                    for (j =0; j < a.length; j++) {
                    var FieldName = a[j].name
                    if (j == a.length - 1) {
                    delimiter = ""
                    }
                    if (this.getField(FieldName) != null) {
                    fieldValue = this.getField(FieldName).valueAsString
                    if (fieldValue != "Off" ) {
                    newFieldValue = newFieldValue + fieldValue + delimiter
                    }
                    }
                    }
                    }
                    event.value = newFieldValue
                    • 7. Re: Using Javascript to create concatenated string from checkbox fields to one text field
                      MarkWalsh Level 4
                      Sorry, my first reply didn't always remove the ending delimiter properly. The following code should work:

                      var fieldValue
                      var newFieldValue
                      var delimiter

                      newFieldValue = ""
                      delimiter = ", "

                      var oField = this.getField("LANGUAGE");
                      if (oField != null) {
                      var a = oField.getArray();
                      for (j =0; j < a.length; j++) {
                      var FieldName = a[j].name
                      if (this.getField(FieldName) != null) {
                      fieldValue = this.getField(FieldName).valueAsString
                      if (fieldValue != "Off" ) {
                      newFieldValue = newFieldValue + fieldValue + delimiter
                      }
                      }
                      }
                      }
                      newFieldValue = newFieldValue.substring(0, newFieldValue.length - delimiter.length)
                      event.value = newFieldValue
                      • 8. Re: Using Javascript to create concatenated string from checkbox fields to one text field
                        gkaiseril MVP & Adobe Community Professional
                        A simple way that includes the generalized 'fillin()' funciton:

                        function BuildString(sFieldName) {
                        var aField = this.getField(sFieldName).getArray(); // get array of field object
                        var sResult = ''; // define result string as empty
                        if (aField.length != 0) {
                        for(i = 0; i < aField.length; i++) {

                        if(aField[i].valueAsString != 'Off') { // concatenate if item not 'Off'
                        sResult = fillin(sResult, aField[i].valueAsString, '', ', ');
                        } // if not 'Off'

                        } // end for length of aFeild
                        } // end aField not 0
                        return sResult; // return result string
                        } // end function

                        event.value = BuildString('Language');

                        This approach also limits the number and scope of variables and variable space by the use of the function definition to create variables as needed and then releases them and their associated space when the funciton ends. Because the 'fillin()' fucntion will automatically adjust for the number of passed stirngs from 1 - 3, one is able to go through the "Language" fileds one at a time.