cancel
Showing results for 
Search instead for 
Did you mean: 

Convert FormattedTextEdit to TextEdit

Former Member
0 Kudos

Hi All,

I would like to take a string formatted for a FormattedTextEdit and convert it for use in a TextEdit. This should have the same effect as if I highlighted all the contents of the FormattedTextEdit and pasted it into the TextEdit element (line breaks are preserved and other formatting is not).

My overall goal is to get SO10 texts into a TextEdit. Right now I've managed to get them into a FormattedTextEdit, so if there's a way to get the TextEdit formatting directly from SapScript texts, that would also be useful.

Thanks,

Scott

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member450736
Active Participant
0 Kudos

I don't think that is possible with TextEdit.

Former Member
0 Kudos

I didn't find a standard way. This function covers most situations:

Note: add a semicolon ( to the end of each instance of &nbsp, &quot, and &amp. They won't display correctly here if left in.


FUNCTION Z_TEXT_FROM_FORMATTED_TEXT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(FORMATTEDTEXT) TYPE  STRING
*"  EXPORTING
*"     REFERENCE(TEXT) TYPE  STRING
*"----------------------------------------------------------------------

  DATA: wa_text_in TYPE STRING,
        wa_text TYPE STRING,
        wa_inputlength TYPE I,
        wa_strlen TYPE I,
        wa_specialctr TYPE I,
        wa_int TYPE I.

  wa_text_in = FORMATTEDTEXT.

  wa_inputlength = STRLEN( wa_text_in ).

  WHILE wa_text_in IS NOT INITIAL.
    IF SY-INDEX > wa_inputlength. "Emergency termination condition
      EXIT.
    ENDIF.

    SPLIT wa_text_in AT '<' INTO wa_text wa_text_in.

    "Find special characters
    IF ( wa_text CS '&nbsp' ) OR ( wa_text CS '&quot' ) OR ( wa_text CS '&amp' ).
      wa_strlen = STRLEN( wa_text ).
      wa_specialctr = 0.
      WHILE wa_specialctr < wa_strlen.
        ADD 1 TO wa_specialctr.

        IF wa_text(wa_specialctr) CS '&nbsp'. "non-breaking space
          wa_int = wa_specialctr - 6. "special character indicator length
          CONCATENATE TEXT wa_text(wa_int) ' ' INTO TEXT.
          wa_text = wa_text+wa_specialctr.
        ELSEIF wa_text(wa_specialctr) CS '&quot'. "quote
          wa_int = wa_specialctr - 6. "special character indicator length
          CONCATENATE TEXT wa_text(wa_int) '"' INTO TEXT.
          wa_text = wa_text+wa_specialctr.
        ELSEIF wa_text(wa_specialctr) CS '&amp'. "ampersand
          wa_int = wa_specialctr - 5. "special character indicator length
          CONCATENATE TEXT wa_text(wa_int) '&' INTO TEXT.
          wa_text = wa_text+wa_specialctr.
        ENDIF.
        IF wa_strlen <> STRLEN( wa_text ). "Reset for remainder of string when special character is found
          wa_strlen = STRLEN( wa_text ).
          wa_specialctr = 0.
        ENDIF.

      ENDWHILE.
    ENDIF.

    CONCATENATE TEXT wa_text INTO TEXT.

    "Find HTML tags
    wa_strlen = STRLEN( wa_text_in ).
    IF wa_strlen >= 1. "Prevent crash
      IF ( wa_text_in(1) CS 'p' ). "2 NewLines
        CONCATENATE TEXT CL_ABAP_CHAR_UTILITIES=>NEWLINE CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO TEXT.
      ENDIF.
    ENDIF.

    IF wa_strlen >= 2. "Prevent crash
      IF wa_text_in(2) CS 'br'. "Newline
        CONCATENATE TEXT CL_ABAP_CHAR_UTILITIES=>NEWLINE INTO TEXT.
      ENDIF.
    ENDIF.

    SPLIT wa_text_in AT '>' INTO wa_text wa_text_in. "Throw away remainder of tag

  ENDWHILE.

  "Remove leading linebreaks
  WHILE TEXT IS NOT INITIAL. "Prevent crash (out-of-bounds)
    IF TEXT(1) = CL_ABAP_CHAR_UTILITIES=>NEWLINE.
      TEXT = TEXT+1.
    ELSE.
      EXIT.
    ENDIF.
  ENDWHILE.

ENDFUNCTION.