[ABAP] Einkaufsbestelltext zum Material auslesen

* https://www.berater-wiki.de/SAPscript-Textbaustein_lesen_mit_Funktionsbaustein_READ_TEXT_und_speichern_mit_SAVE_TEXT
* Texte: Anwendungsobjekt
* siehe SE75
CONSTANTS: co_obj_type TYPE thead-tdobject VALUE 'MATERIAL'. " Materialtexte Einkauf/Lagerung
* Text-ID
* siehe SE75 --> Doppelklick auf Objekt
CONSTANTS: co_obj_id TYPE thead-tdid VALUE 'BEST'. " Einkaufsbestelltext

DATA: it_thead TYPE STANDARD TABLE OF thead WITH DEFAULT KEY.
DATA: it_lines TYPE tline_t.
DATA: it_ascii TYPE tdtab_c132.

* Materialnummer mit führenden Nullen
DATA(lv_matnr) = CONV matnr( '000000001122334455' ).

* erst 'SELECT_TEXT' zur Prüfung aufrufen (ob Text vorhanden)
* 'READ_TEXT' wirft sonst bei Nichtvorhandensein des Textes eine Abbruchmessage
* Texterstellung Text auswählen
CALL FUNCTION 'SELECT_TEXT'
  EXPORTING
    object                  = co_obj_type
    name                    = CONV thead-tdname( lv_matnr )
    id                      = co_obj_id
    language                = sy-langu
  TABLES
    selections              = it_thead   " Textheader der gefundenen Texte
  EXCEPTIONS
    wrong_access_to_archive = 1
    OTHERS                  = 2.

IF sy-subrc = 0.
  IF lines( it_thead ) > 0.
* Texterstellung Text lesen
    CALL FUNCTION 'READ_TEXT'
      EXPORTING
        id                      = co_obj_id
        language                = sy-langu
        name                    = CONV thead-tdname( lv_matnr )
        object                  = co_obj_type
      TABLES
        lines                   = it_lines " Textzeilen des gelesenen Textes
      EXCEPTIONS
        id                      = 1
        language                = 2
        name                    = 3
        not_found               = 4
        object                  = 5
        reference_check         = 6
        wrong_access_to_archive = 7
        OTHERS                  = 8.

    IF sy-subrc = 0.
      IF lines( it_lines ) > 0.
* Textkonvertierung ITF (SAPscript-Format) in ASCII
* Stile und Formatierungen aus SAP-Script-Text entfernen
        CALL FUNCTION 'CONVERT_ITF_TO_ASCII'
          EXPORTING
            formatwidth       = 132
          IMPORTING
            c_datatab         = it_ascii
          TABLES
            itf_lines         = it_lines
          EXCEPTIONS
            invalid_tabletype = 1
            OTHERS            = 2.
        IF sy-subrc = 0.
          cl_demo_output=>write_data( it_thead ).
          cl_demo_output=>write_data( it_lines ).
          cl_demo_output=>write_data( it_ascii ).
          cl_demo_output=>display( ).
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDIF.

[ABAP] Felder im Selektionsbild manuell setzen

DATA: pernr TYPE persno.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE title.

PARAMETERS: pa_bukrs TYPE pa0001-bukrs.
SELECT-OPTIONS: so_pernr FOR pernr.

TYPES: ty_it_screen TYPE STANDARD TABLE OF screen WITH DEFAULT KEY.
DATA: it_screen TYPE ty_it_screen.

SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.

  title = 'Testblock'.

* SCREEN-Felder auslesen
  LOOP AT SCREEN INTO DATA(lv_screen).
    APPEND lv_screen TO it_screen.
  ENDLOOP.

  %_pa_bukrs_%_app_%-text = 'Par'.
  %_so_pernr_%_app_%-text = 'SelOpt'.

START-OF-SELECTION.
*SSCRFIELDS-UCOMM
*TITLE
*%_PA_BUKRS_%_APP_%-TEXT
*PA_BUKRS
*%_SO_PERNR_%_APP_%-TEXT
*%_SO_PERNR_%_APP_%-OPTI_PUSH
*SO_PERNR-LOW
*%_SO_PERNR_%_APP_%-TO_TEXT
*SO_PERNR-HIGH
*%_SO_PERNR_%_APP_%-VALU_PUSH
*%_17SNS0000194656_%_%_%_%_%_%_

* SCREEN-Felder ausgeben
  cl_demo_output=>display_data( it_screen ).

[ABAP] Auslesen der Schnittstelle eines Funktionsbausteins

PARAMETERS: lv_func TYPE rs38l-name DEFAULT 'READ_TEXT'.

TYPES: ty_it_exeptions TYPE STANDARD TABLE OF rsexc WITH DEFAULT KEY.
TYPES: ty_it_exporting TYPE STANDARD TABLE OF rsexp WITH DEFAULT KEY.
TYPES: ty_it_importing TYPE STANDARD TABLE OF rsimp WITH DEFAULT KEY.
TYPES: ty_it_changing TYPE STANDARD TABLE OF rscha WITH DEFAULT KEY.
TYPES: ty_it_tables TYPE STANDARD TABLE OF rstbl WITH DEFAULT KEY.
TYPES: ty_it_p_docu TYPE STANDARD TABLE OF rsfdo WITH DEFAULT KEY.

DATA(lv_global_flag) = VALUE rs38l-global( ).
DATA(lv_remote_call) = VALUE rs38l-remote( ).
DATA(lv_update_task) = VALUE rs38l-utask( ).

DATA(it_exeptions) = VALUE ty_it_exeptions( ).
DATA(it_exporting) = VALUE ty_it_exporting( ).
DATA(it_importing) = VALUE ty_it_importing( ).
DATA(it_changing) = VALUE ty_it_changing( ).
DATA(it_tables) = VALUE ty_it_tables( ).
DATA(it_p_docu) = VALUE ty_it_p_docu( ).

* Bereitstellung der Schnittstelle eines Funktionsbausteins
CALL FUNCTION 'FUNCTION_IMPORT_INTERFACE'
  EXPORTING
    funcname           = lv_func
  IMPORTING
    global_flag        = lv_global_flag
    remote_call        = lv_remote_call
    update_task        = lv_update_task
  TABLES
    exception_list     = it_exeptions
    export_parameter   = it_exporting
    import_parameter   = it_importing
    changing_parameter = it_changing
    tables_parameter   = it_tables
    p_docu             = it_p_docu
  EXCEPTIONS
    error_message      = 1
    function_not_found = 2
    invalid_name       = 3
    OTHERS             = 4.

IF sy-subrc = 0.
  cl_demo_output=>write_data( lv_global_flag ).
  cl_demo_output=>write_data( lv_remote_call ).
  cl_demo_output=>write_data( lv_update_task ).
  cl_demo_output=>write_data( it_exeptions ).
  cl_demo_output=>write_data( it_exporting ).
  cl_demo_output=>write_data( it_importing ).
  cl_demo_output=>write_data( it_changing ).
  cl_demo_output=>write_data( it_tables ).
  cl_demo_output=>write_data( it_p_docu ).
  cl_demo_output=>display( ).
ENDIF.

[ABAP] Adobe Forms Formular aufrufen und als xstring weiterverarbeiten

* https://www.berater-wiki.de/Aufruf_des_Adobe_Formulars_im_Rahmenprogramm
* https://wiki.scn.sap.com/wiki/display/ABAP/Adobe+Forms+from+Scratch
* Formularname
DATA(lv_form) = CONV fpname( 'ZADOBE_FORM_NAME' ).
* Name generierter Funktionsbaustein
DATA: lv_funcname TYPE funcname.
DATA: lv_interface_type TYPE fpinterfacetype.

* Namen des Funktionsbausteins für ein Formular bestimmen
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
  EXPORTING
    i_name           = lv_form
  IMPORTING
    e_funcname       = lv_funcname
    e_interface_type = lv_interface_type.

IF sy-subrc = 0.

  DATA: lv_outputparams TYPE sfpoutputparams.

* Formularprozessierung: Job beginnen
  CALL FUNCTION 'FP_JOB_OPEN'
    CHANGING
      ie_outputparams = lv_outputparams
    EXCEPTIONS
      cancel          = 1
      usage_error     = 2
      system_error    = 3
      internal_error  = 4
      OTHERS          = 5.

  IF sy-subrc = 0.

    DATA(lv_docparams) = VALUE sfpdocparams( langu = 'D' country  = 'DE' ).
    DATA: lv_formout TYPE fpformoutput.

    CALL FUNCTION lv_funcname
      EXPORTING
        /1bcdwb/docparams  = lv_docparams
* ggf. spez. Parameter für Funktionsaufruf hier aufführen
* siehe Transaktion SFP (Form Builder)
      IMPORTING
        /1bcdwb/formoutput = lv_formout
      EXCEPTIONS
        usage_error        = 1
        system_error       = 2
        internal_error     = 3
        OTHERS             = 4.

    IF sy-subrc = 0.
* xstring mit PDF-Daten
      DATA(lv_pdf_raw) = lv_formout-pdf.
    ENDIF.

    DATA: lv_result TYPE sfpjoboutput.

* Formularprozessierung: Job beenden
    CALL FUNCTION 'FP_JOB_CLOSE'
      IMPORTING
        e_result       = lv_result
      EXCEPTIONS
        usage_error    = 1
        system_error   = 2
        internal_error = 3
        OTHERS         = 4.

    IF sy-subrc = 0.

    ENDIF.
  ENDIF.
ENDIF.

[ABAP] ABAP2XLSX: Excel-Daten (*.xlsx) einlesen und Inhalt anzeigen

**********************************************************************
* Selektionsbild
**********************************************************************
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.

* File-Tabelle leeren, da hier noch alte Einträge von vorherigen Aufrufen drin stehen können
  CLEAR it_files.

* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter    = |xlsx (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    multiselection = abap_false
                                                  CHANGING
                                                    file_table  = it_files
                                                    rc          = lv_rc
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_fname = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'I'.
  ENDTRY.

**********************************************************************
* START-OF-SELECTION
**********************************************************************
START-OF-SELECTION.

  TRY.
* Reader-Objekt erzeugen
      DATA(o_reader) = CAST zif_excel_reader( NEW zcl_excel_reader_2007( ) ).
      DATA(o_excel) = o_reader->load_file( p_fname ).

* Worksheet
      DATA(o_worksheet) = o_excel->get_active_worksheet( ).
* alternativer Zugriff über Worksheet-Iterator
*      DATA(o_worksheet) = CAST zcl_excel_worksheet( o_excel->get_worksheets_iterator( )->get_next( ) ).

* Inhalt der Worksheet
      LOOP AT o_worksheet->sheet_content ASSIGNING FIELD-SYMBOL(<cell>) GROUP BY <cell>-cell_row ASSIGNING FIELD-SYMBOL(<row>).
* Zeile
        LOOP AT GROUP <row> ASSIGNING FIELD-SYMBOL(<cell_data>).
* Zellinfos
          WRITE: / <cell_data>-cell_coords, <cell_data>-cell_column, <cell_data>-cell_row, <cell_data>-data_type, <cell_data>-cell_value, <cell_data>-cell_formula, <cell_data>-cell_style.
        ENDLOOP.
      ENDLOOP.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Binärdaten (HEX) in eine lokale Datei speichern

* HEX-Daten (Beispiel)
DATA(it_solix) = VALUE solix_tab(
                   ( line = '7639BD' )
                   ( line = '67326F' )
                 ).

TRY.
    DATA: lv_action TYPE i.
    DATA: lv_filename TYPE string.
    DATA: lv_fullpath TYPE string.
    DATA: lv_path TYPE string.

* Save-Dialog
    cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                  default_file_name = 'Data.bin'
                                                  default_extension = 'bin'
                                                  file_filter       = |Binärdaten (*.bin)\|*.bin\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING
                                                  filename          = lv_filename
                                                  path              = lv_path
                                                  fullpath          = lv_fullpath
                                                  user_action       = lv_action ).

    IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING für Längenbestimung erzeugen
      DATA(lv_xstring) = cl_bcs_convert=>solix_to_xstring( it_solix ).

* Datei lokal speichern
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename     = lv_fullpath
                                                filetype     = 'BIN'
                                                bin_filesize = xstrlen( lv_xstring )
                                              CHANGING
                                                data_tab     = it_solix ).

    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] Variablen vom Typ XSTRING zusammenfügen (addieren)

Variante 1 (einzelne XSTRINGS)

* HEX-Daten (Beispiel)
DATA(lv_xstring) = CONV xstring( 'A1B1C1' ).
DATA(lv_xstring2) = CONV xstring( 'A2B2C2' ).

* XSTRING + XSTRING
CONCATENATE lv_xstring lv_xstring2 INTO lv_xstring IN BYTE MODE.

* Testausgabe
WRITE: lv_xstring.

Variante 2 (eine Tabelle mit XSTRINGS)

TYPES: ty_it_hex TYPE STANDARD TABLE OF xstring WITH DEFAULT KEY.

* Tabelle mit binären Beispieldaten
DATA(it_hex_data) = VALUE ty_it_hex(
                                     ( CONV xstring( 'AABBCC' ) )
                                     ( CONV xstring( 'DDEEFF' ) )
                                     ( CONV xstring( '001122' ) )
                                     ( CONV xstring( '334455' ) )
                                     ( CONV xstring( '667788' ) )
                                   ).

* Zeilen der Tabelle zu einem XSTRING zusammenfügen
CONCATENATE LINES OF it_hex_data INTO DATA(lv_xstring) IN BYTE MODE.

* Testausgabe
WRITE: lv_xstring.

[ABAP] Breakpoints im Code setzen

Variante 1 (nutzerspezifischer Breakpoint)

* BREAK ist ein vordefiniertes Makro aus der Tabelle TRMAC
* Breakpoint wird nur getriggert, wenn Benutzer eingeloggt und Debugger-Rechte besitzt
BREAK <username>.

Variante 2 (Checkpoint-Gruppe mit Transaktion SAAB)

* flexibel in der Transaktion SAAB aktivierbar oder deaktivierbar
* Entwickler muss Berechtigung für Transaktion SAAB haben
BREAK-POINT ID <checkpoint>

Links

[ABAP] ABAP2XLSX: Excel-Daten (*.xlsx) einlesen und anzeigen

**********************************************************************
* Types
**********************************************************************
TYPES: BEGIN OF ty_xl_line,
         col1 TYPE string,
         col2 TYPE string,
       END OF ty_xl_line.

TYPES: ty_it_xl_lines TYPE STANDARD TABLE OF ty_xl_line WITH DEFAULT KEY.

**********************************************************************
* Selektionsbild
**********************************************************************
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.

* File-Tabelle leeren, da hier noch alte Einträge von vorherigen Aufrufen drin stehen können
  CLEAR it_files.

* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter    = |xlsx (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    multiselection = abap_false
                                                  CHANGING
                                                    file_table  = it_files
                                                    rc          = lv_rc
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_fname = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'I'.
  ENDTRY.

START-OF-SELECTION.

  DATA(it_xl) = VALUE ty_it_xl_lines( ).

  TRY.
* Reader-Objekt erzeugen
      DATA(o_reader) = CAST zif_excel_reader( NEW zcl_excel_reader_2007( ) ).
      DATA(o_excel) = o_reader->load_file( p_fname ).

* Worksheet
      DATA(o_worksheet) = o_excel->get_active_worksheet( ).
* max. Zeile und Spalte holen
      DATA(lv_max_col) = o_worksheet->get_highest_column( ).
      DATA(lv_max_row)    = o_worksheet->get_highest_row( ).

      WRITE: / lv_max_col, ',', lv_max_row.

      DATA(lv_row) = 1.

* Worksheet zeilenweise durchlaufen
      WHILE lv_row <= lv_max_row.
* Spaltennummer (1) in Excel-Spalten-Bezeichner (A) umwandeln
        DATA(lv_col_str) = zcl_excel_common=>convert_column2alpha( 1 ).

* Zellinhalt Spalte 1 holen
        o_worksheet->get_cell( EXPORTING
                                 ip_column = lv_col_str
                                 ip_row    = lv_row
                               IMPORTING
                                 ep_value  = DATA(lv_value)
                             ).

* Spaltennummer (2) in Excel-Spalten-Bezeichner (B) umwandeln
        DATA(lv_col_str2) = zcl_excel_common=>convert_column2alpha( 2 ).
* Zellinhalt Spalte 2 holen
        o_worksheet->get_cell( EXPORTING
                                 ip_column = lv_col_str2
                                 ip_row    = lv_row
                               IMPORTING
                                 ep_value  = DATA(lv_value2)
                             ).

* Werte an interne Tabelle anfügen
        APPEND VALUE #(
                        col1 = lv_value
                        col2 = lv_value2
                      ) TO it_xl.

        lv_row = lv_row + 1.
      ENDWHILE.

* Daten in SALV-Grid anzeigen
      TRY.
* SALV-Table
          DATA: o_salv TYPE REF TO cl_salv_table.

          cl_salv_table=>factory( IMPORTING
                                    r_salv_table = o_salv
                                  CHANGING
                                    t_table      = it_xl ).

* Grundeinstellungen
          o_salv->get_functions( )->set_all( abap_true ).
          o_salv->get_columns( )->set_optimize( abap_true ).
          o_salv->get_display_settings( )->set_list_header( |Excel-Werte ({ lines( it_xl ) })| ).
          o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
          o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte, Short Text und Medium Text leer lassen für Autosize
          LOOP AT o_salv->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<c>).
            DATA(o_col) = <c>-r_column.
            o_col->set_short_text( || ).
            o_col->set_medium_text( || ).
            o_col->set_long_text( |{ o_col->get_columnname( ) } [{ o_col->get_long_text( ) }]| ).
          ENDLOOP.

          o_salv->display( ).
        CATCH cx_root INTO DATA(e_txt).
          WRITE: / e_txt->get_text( ).
      ENDTRY.
    CATCH cx_root INTO DATA(ex).    " Exceptions for ABAP2XLSX
      WRITE: / ex->get_text( ).
      WRITE: / ex->get_longtext( ).
  ENDTRY.