9 Replies Latest reply on Dec 12, 2014 10:46 AM by Kevin234@#$

    concatenate fields using commas

    Kevin234@#$

      How do I concatenate fields using proper English separators? I'm using Acrobat Pro X.

       

      I have fields named Field1, Field2, Field3, Field4 and Result.

      Example 1

      In Field1, I enter Apples. In Field2, Oranges. The other two fields are empty (Null).

       

      In the Result field it returns "Apples and Oranges."

       

      Example 2

      I add Grapes in Field3.

       

      The Result field returns, "Apples, Oranges, and Grapes."

       

      Example 3

      I add Pears to Field4,

       

      The Result should be,  "Apples, Oranges, Grapes, and Pears."

       

      Any help is appreciated.

        • 1. Re: concatenate fields using commas
          gkaiseril MVP & Adobe Community Professional


          Only 3 fields or will there be more fields?

           

          I would first look for a way to concatenate the fields without the conjunction.

           

           

          Will the fields be consecutively filled in or could there be an empty field between two filled in fields?

          • 2. Re: concatenate fields using commas
            Kevin234@#$ Level 1

            The script for this will be scaled up to ten total fields once complete and will only contain consecutively filled fields. This is what I have currently. It uses a separate field for the conjunction and obviously leaves the remaining commas and the end of the returned result.

             

            var a=getField(“Field1”).valueAsString

            var b=getField(“Field2”).valueAsString 

            var c=getField(“Field3”).valueAsString 

            var d=getField(“Field4”).valueAsString 

            var e=getField(“Field5”).valueAsString 

            var f=getField(“Field6”).valueAsString 

            var g=getField(“Field7”).valueAsString 

            var h=getField(“Field8”).valueAsString 

            var i=getField(“Field9”).valueAsString 

            var j=getField(“Field10”).valueAsString 

            var sep=getField(“separator”).valueAsString; 

            if(sep.length>=1) 

            {event.value=a+sep+ b+sep+ c+sep+ d+sep+ e+sep+ f+sep+ g+sep+ h+sep+ i+sep+ j+sep}

            Else event.value +””;

             

            • 3. Re: concatenate fields using commas
              formsdoctor Level 1

              var ray=[]

              for (var i=1;i<11;i++)

              {

              if (this.getField("Field"+i).value!="")

              {

              ray.push(" "+this.getField("Field"+i).value)

              }

              }

               

              if (ray.length==0)

              {

              event.value= "";

              }

               

              else if (ray.length==1)

              {

              event.value= ray[0].replace(/\s/,"")+".";;

              }

              else if

              (ray.length==2)

              {event.value=ray[0].replace(/\s/,"")+" and"+ray[1]+".";}

              else

              {

              event.value=ray.toString().replace(/\s/,"").replace(/\,(?=[^,]*$)/, " and")+".";

              }

              • 4. Re: concatenate fields using commas
                gkaiseril MVP & Adobe Community Professional

                Using hierarchical fields:

                 

                // document level functions;
                function NotNull(element) {
                // filter out null stirngs;
                return String(element).length > 0;
                }

                function Array2String(aValues) {
                // convert an array of values to a comma delemited string with "and" as needed;
                aValues = aValues.filter(NotNull); // remove null items;
                if(aValues.length > 1) {
                // add "and" to last element if more than 1 item in array;
                aValues[aValues.length - 1] = "and " + aValues[aValues.length - 1];
                }
                var cString;
                switch(true) {
                // set value depending upon number of items;
                case (aValues.length == 1):
                // 1 item - item only;
                cString = aValues[0]  + ".";
                break;
                case (aValues.length == 2):
                // 2 items - no comas;
                cString = aValues.join(" ") + ".";
                break;
                case (aValues.length > 2):
                // 3 or more comas;
                cString = aValues.join(", ") + ".";
                break;
                defualt:
                // for no items;
                cString = "";
                break;
                }
                return cString;
                }
                // end document level functions;

                 

                 

                // custom calculation script for result field;
                var oField = this.getField("item"); // get top field;
                var aChildren = oField.getArray(); // get children fields;
                var aValues = new Array(); // array for values;
                for(var i = 0; i < aChildren.length; i++) {
                aValues.push(aChildren[i].valueAsString); //fill array with values;
                }
                event.value = Array2String(aValues);
                // end custom calculation script;

                 

                The script does not require contiguous entries and is scalable for more or less fields without editing the script. The document level functions could be reused in other fields within the form as needed.

                 

                Link to sample file Concatenate List  Shared Files - Acrobat.com

                • 5. Re: concatenate fields using commas
                  Kevin234@#$ Level 1

                  @GKaiseril - First, Thank you for the response. Second, I think I'm missing something here. I've been trying to utilize the code in the linked example and I had it working for a moment, then I've spent the last two days to try to get it to work again. I even created a new document with exactly the identical fields, copied and pasted the code but received the error: "array2String is not defined". It works in the linked example just not in anything the code is pasted to.

                   

                  I know the code will work. It's just baffling me right know.

                  • 6. Re: concatenate fields using commas
                    gkaiseril MVP & Adobe Community Professional

                    There are document level functions that need to be added to the form.

                     

                    You can insert the sample form into your form and the document level functions will be added to your form.

                     

                    I am using hierarchical from field names to make coding a little simpler. One could use regular fields and a for loop to populate the array to be processed.

                    • 7. Re: concatenate fields using commas
                      Kevin234@#$ Level 1

                      I feel like such an idiot...I was looking at the page level vs document level scripts. I found it after your post. thanks for your help.

                      • 8. Re: concatenate fields using commas
                        gkaiseril MVP & Adobe Community Professional

                        One could include the document level functions into the field or even page open, but the code will repeatedly be unnecessarily reprocessed each time the page script runs or the page is opened. Of course one could add some code not process the functions if the functions are already defined.

                        • 9. Re: concatenate fields using commas
                          Kevin234@#$ Level 1

                          Having successfully implemented the above code, I was able to return the exact result I originally asked for however, after I did his, I realized I needed control over the separator (comma) and this code wasn't able to fulfill my needs with my level of experience. I was able to take what I learned here and create a form that fits the bill. Thanks for your help, I definitely appreciate it.