Link Search Menu Expand Document

Example decomposition of the DOWNLOAD method or thinking in OOP within 1 day


ZCL_EUI_FILE examples

SE38 -> ZEUI_TEST_EXCEL

" Instance of FILE
DATA(lo_file) = NEW zcl_eui_file( ).

" load from xString
" for internal table -> import_from_binary( )
lo_file->import_from_xstring( iv_xstring = lv_xstring ).

" By default download to `SAP GUI\tmp\`
lo_file->download( ).

" Instance of FILE
NEW zcl_eui_file(

" Default encoding UTF-8
" to change -> iv_encoding = zcl_eui_conv=>mc_encoding-utf_16le
)->import_from_string( iv_string = `Text`

" Show SaveAs dialog
)->download( iv_save_dialog  = 'X'
             iv_window_title = `Export text ...`
             
" If saved then open, otherwise exception
)->open( ).

" Instance of FILE
NEW zcl_eui_file(
 " Load from xString in constructor
 iv_xstring     = lv_xstring

 " If not passed
 " -> Ok + Cancel (edit mode)
 " -> Cancel (Read only mode)
* iv_status_prog  = sy-cprog
* iv_status_name  = 'STATUS_MANY_BUTTONS'

" Add to show SCREEN as popup
" Can call any method which returns ME
)->popup( iv_col_end = 200


" Show in new SCREEN
)->show( ).

" Could be existing instance or new one
DATA lv_ole_app	TYPE ole2_object.

" Any method call could raise an exception
TRY .
    NEW zcl_eui_file(
    
    " load Excel data
    )->import_from_binary(
     it_table  = lt_bin_xlsx
     iv_length = lv_len

    " Specify full path in shared path
    )->download(
     iv_full_path = `I:\$secret\2020-06-27.xlsx`

     " Use ole for macro
    )->open_by_ole(
     CHANGING
       cv_ole_app = lv_ole_app ).
  CATCH zcx_eui_exception INTO DATA(lo_error).
    " Show error message
    MESSAGE lo_error TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
ENDTRY.

" If all ok call your macro
CALL METHOD OF lv_ole_app 'Run'
  EXPORTING
    #1 = 'MACRO_NAME'
    #2 = 'MACRO_PARAM'.


Prerequisites for decomposition

If you have a method with a dozen optional parameters you worth start smoking more often divide it into several smaller and more understandable methods

Once upon a time there was a simple DOWNLOAD method for uploading files.
It took 2 parameters IT_TABLE and its length IV_LENGTH.
He was not at all complicated, until unloading from IV_XSTRING was needed.
And then from the usual line IV_STRING + encoding IV_ENCODING (utf-8 by default)

I decided that it would be nice to open the file after downloading IV_OPEN
Yes, and sometimes it was necessary to open it through OLE (+ 1 parameter) to call the macro.
Then the optimization was made for downloading files via FTP for files over 10 megabytes
Everything was fine, but then sometimes the files needed to be downloaded to a folder specified by the user
or have a permanent name
or generate random one to upload to sap_tmp


As a result DOWNLOAD inside it looked like a roll of cheap shuttle paper from the supermarket and I wanted to use it less and less

Gathering the will into a fist and firstly the monitor has been wiped with alcohol fume after a recent corporate party it was decided to finally divide it into smaller parts


Implementation

Firstly holding a file is easiest in one XSTRING attribute (and pass it in the constructor)

image

But you can change MV_XSTRING from different sources

image

each method returns itself as a result

image

This makes it possible to call the following DOWNLOAD (or SHOW) method as a chain

image

If you do not need to do anything after downloading, the call chain breaks, otherwise you can call one of the following to open the file

image


As a result, if you need to download a file from the internal table, call the Save file dialogue and then open it
a chain will look like

 new ZCL_EUI_FILE( )->
    IMPORT_FROM_BINARY( )->
    DOWNLOAD( IV_SAVE_DIALOG = 'X' )->
    OPEN( )

If you need to download data from STRING in UTF-16LE encoding, download the file to a specific folder and open it through Excel

 new ZCL_EUI_FILE( )->
    IMPORT_FROM_STRING(  IV_ENCODING = utf_16be )->
    DOWNLOAD( IV_FULL_PATH = ... )->
    OPEN_BY_OLE( )

If at the end you need to show it inside the SAP GUI inplace, change DOWNLOAD( ) to SHOW( )

 new ZCL_EUI_FILE( )->
    IMPORT_FROM_STRING(  IV_ENCODING = utf_16be )->
    SHOW( )

Each of the steps can throw raise cause an exception, therefore it is better to wrap it in TRY
That is 1 exception handler for the entire call chain

    TRY.
        new ZCL_EUI_FILE( )->IMPORT_FROM_STRING(  IV_ENCODING = utf_16be
            )->DOWNLOAD( IV_FULL_PATH = ... )->OPEN_BY_OLE( ).
    CATCH zcx_eui_exception INTO DATA(lo_error).
        MESSAGE lo_error TYPE 'S' DISPLAY LIKE 'E'.
        RETURN.
    ENDTRY.