5 Replies Latest reply on Apr 12, 2011 2:08 AM by rjlevesque

    How to select a string in a text field and change its format

    wladdy

      Title

      How to select a string in a text field and change its format

      Post

      I have a text field in which all the records have the following format:

      "[string1]
      Key expression: [string2]
      [string 3]"

      80% of the time [string 2] will be a direct extraction from [string1]. For instance, a record could be :

      "Now is the winter of our discontent
      made glorious summer by this sun of York
      Key expression: winter of our discontent
      This is the opening line from Richard III".

      My goal is to write a script that will automatically change the format of [string2] within [string1], say by underlining. In my example, the desired output would be:

      "Now is the winter of our discontent
      made glorious summer by this sun of York
      Key expression: winter of our discontent
      This is the opening line from Richard III".

      I figured out that I could select [string2] by using Step Selection with the following parameters:

      Start Position = Position ( Table::Field ; "Key expression:" ; 1 ; 1 ) + 16
      End Position = Position ( Table::Field ; Char (13)  ; Position ( Table::Field; "(A)" ; 1 ; 1 ) ; 1 ) – 1

      I do not know where to get from here. My intuition is to do the following:

      1) save the selection (that is [string2]) inside a local variable called $KeyExpression
      2) run Step Selection again using $KeyExpression in the parameters to select the first occurrence of [String2] in Table::Field
      3) somehow use the function 'TextStyleAdd' to underline the new selection

      Unfortunately, I cannot figure out how to

      1) save a text selection inside a variable (PhilModJunk wrote in a threat to avoid using the clipboard so I want to follow that recommendation)
      2) change the formatting of a text selection inside a field

      Alternatively, maybe my entire approach is flawed. In any case, suggestions to put me on the right path would be greatly appreciated. W.

        • 1. Re: How to select a string in a text field and change its format
          KevinPfeiffer

          Here might be one way. In your script choose a "Set variable" step (e.g. $$myFormattedOutput) if that is what you want, or set field (e.g. Table::c_myOutputField) and then a calculation such as this one...

          Let(


              [
                  _srcTxt = "Now is the winter of our discontent¶made glorious summer by this sun of York";
                  _underlineTxt = TextStyleAdd( "winter of our discontent"; Underline );
                  _underlineStartPos = Position ( _srcTxt ; _underlineTxt; 1 ; 1 );
                  _startTxt = Left( _srcTxt; _underlineStartPos -1 );
                  _underlineStopPos = _underlineStartPos + Length( _underlineTxt );
                  _endTxt = Right( _srcTxt;  Length( _srcTxt ) - _underlineStopPos + 1 )
              ];


          _startTxt & _underlineTxt & _endTxt


          )

          -K

          You would also want to think through the consequences of (and probably test for) what would happen if underlined term is not found in text (can always happen).

          • 2. Re: How to select a string in a text field and change its format
            raybaudi

            Hi

            I'm assuming here that YourTextField contains ALWAYS 3 paragraphs.

            Set Field [ YourTable::YourTextField ;
                      Let([
                           NoStyles = TextFormatRemove ( YourTable::YourTextField ) ;
                           FirstParagraph = GetValue ( NoStyles ; 1 ) ;
                           SecondParagraph = GetValue ( NoStyles ; 2 ) ;
                           ThirdParagraph =  GetValue ( NoStyles ; 3 ) ;
                           Key = MiddleWords ( SecondParagraph ; 3 ; WordCount ( SecondParagraph ) )
                           ];
                          Substitute ( FirstParagraph ; Key ; TextStyleAdd ( Key ; Underline ) ) & ¶ & SecondParagraph & ¶ & ThirdParagraph
                          )
            Commit Records/Requests [ ]

            • 3. Re: How to select a string in a text field and change its format
              KevinPfeiffer

              Nice approach. If the whole thing literally exists in one single text field (as OP did indeed write), then one might want to consider parsing the already existing data just once using Ray's approach and storing the results (as well as all future entries) in separate fields. Wouldn't  this in the long-term be a more flexible approach? One that would give you a chance to correct any problems (such as those arising from our assumptions about the number of line returns in the source text)?

              • 4. Re: How to select a string in a text field and change its format
                wladdy

                Kevin and Ray,

                Many thanks for your suggestions, that made me aware of the existence, nature and power of the 'Let' function, and that taught me how to use it.

                I ended up creating a solution very similar to Ray's, solving not only my original issue but also a few others that I had not included in the original post for the sake of concision and clarity.

                Again, I appreciate the time you took to help me. W.

                • 5. Re: How to select a string in a text field and change its format
                  rjlevesque

                  Hey Wladdy, can you share what you came up with for the rest of the community to learn from please? =)