1 Reply Latest reply: Mar 26, 2014 2:28 PM by George_Johnson RSS

    Need smarter line wrap pls. (example incl)

    BSisson Community Member

      I created a script that will take long comments and break them at 80 characters and wraps them to the next line with a continuation symbol.

      Unfortunately, it breaks things mid-word. So the above line might end up like:

       

      "I created a routine that will take comments and break them at 80 characters and wrap them to the ne

      + xt line with a continuation symbol."

       

      The following script takes the string (f) and puts it out to my GENERAL NOTES page at line (position) .  The page is a series of text fields called GeneralNotes.1-GeneralNotes.xx.  If the string is too long, it breaks it at 80 characters and put the remainder on the next line with a "+" , increments the counters, and keeps going.  This WORKS. It does assume that the longest would be TWO full lines though.  I am sure it could be writen to be smarter, but for now I am working on something else....

       

        {

              if (f.valueAsString.length > 80) {

              var firstpart = f.valueAsString.slice(0,80);        //this -SHOULD- be the first 80 char

              var leftover  = f.valueAsString.slice(80);          //this -SHOULD- be the remainder.

              s.value = firstpart;

              position += 1;

              s = this.getField("GeneralNotes."+ position);

              s.value = " + "+leftover;

              position += 1

             }

             else

             {

              s.value = f.value;                                                //stuff Export value into currnet notes field

             position += 1;                                                      //increment the notes line

             }

            }

       

      HOWEVER, I would like to re-work it to look backwards to the nearest space(sp) so that it doesn't break words apart. (like ne +xt in the above example)

       

      Is there an easy way to do this?  How about the first (sp) after 75 characters? Is there an easy way (other than just replicating the code) to extend this to 3 lines or more?

       


        • 1. Re: Need smarter line wrap pls. (example incl)
          George_Johnson CommunityMVP

          I'm not going to post the code, but you could start by breaking the string apart into an array at the spaces (use the split string method: http://www.w3schools.com/jsref/jsref_split.asp)

           

          Then build a new string by adding concatenating the array elements in a loop, starting with the first and adding a space. When the string will exceed 80 characters, add your continuation character instead of the next array element and continue until you reach the end of the array.

           

          You can insert an actual carriage return in a string using \r: "This is line 1\rThis is line 2"