A partir de un fichero Excel, donde a una unidad organizativa se le asocia un texto multilínea (String) se hace una carga en la tabla NORG del campo de texto, y una carga de Texto Standard con el String, adaptandolo al formato SapScript. Se hacen los saltos de linea manualmente y el ajuste del texto a 72 caracteres/linea para una mejor visualización en la transacción NB21 – Modificar Unidad Organizativa.
*&---------------------------------------------------------------------*
*& Report Z_CARGA_OBS_NORG
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_carga_obs_norg.
TABLES: norg, stxh, stxl.
DATA: BEGIN OF tab_fich OCCURS 0,
txt_linea TYPE string,
END OF tab_fich.
TYPES: BEGIN OF tyitab,
orgid TYPE orgid,
text TYPE string,
END OF tyitab.
DATA: itab TYPE TABLE OF tyitab WITH HEADER LINE.
DATA: ok_proc TYPE xfeld.
DATA fich TYPE string.
PARAMETERS: p_file1 LIKE rlgrap-filename OBLIGATORY,
p_sep TYPE c DEFAULT '¥'.
SELECT-OPTIONS: so_orgty FOR norg-orgty NO INTERVALS.
INITIALIZATION.
so_orgty-low = 'GD'.
so_orgty-option = 'EQ'.
so_orgty-sign = 'I'.
APPEND so_orgty.
so_orgty-low = 'GE'.
so_orgty-option = 'EQ'.
so_orgty-sign = 'I'.
APPEND so_orgty.
so_orgty-low = 'GG'.
so_orgty-option = 'EQ'.
so_orgty-sign = 'I'.
APPEND so_orgty.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file1.
PERFORM buscar_fichero USING p_file1.
START-OF-SELECTION.
PERFORM leer_fichero.
PERFORM tratar_datos.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form BUSCAR_FICHERO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_FILE1 text
*----------------------------------------------------------------------*
FORM buscar_fichero USING p_file1.
CALL FUNCTION 'ISH_N2_FILENAME_GET'
CHANGING
ss_filename = p_file1
EXCEPTIONS
OTHERS = 2.
IF sy-subrc NE 0.
CLEAR p_file1.
ENDIF.
ENDFORM. " BUSCAR_FICHERO
*&---------------------------------------------------------------------*
*& Form leer_fichero
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM leer_fichero.
fich = p_file1.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = fich
filetype = 'ASC'
has_field_separator = p_sep
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = tab_fich
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
REFRESH: itab.
LOOP AT tab_fich.
CLEAR itab.
SPLIT tab_fich-txt_linea AT p_sep
INTO itab-orgid
itab-text.
APPEND itab.
ENDLOOP.
ENDFORM. "leer_fichero_excel
*&---------------------------------------------------------------------*
*& Form TRATAR_DATOS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM tratar_datos .
DATA: lt_norg TYPE TABLE OF norg,
ls_norg TYPE norg.
"Seleccionamos las unidades organizativas
SELECT * INTO TABLE lt_norg FROM norg WHERE orgty IN so_orgty
AND loekz = ' '.
SORT itab BY orgid ASCENDING.
LOOP AT itab.
READ TABLE lt_norg INTO ls_norg WITH KEY orgid = itab-orgid.
IF sy-subrc = 0.
CLEAR ok_proc.
PERFORM grabar_datos.
IF ok_proc IS NOT INITIAL.
WRITE: / itab-orgid , 'Ok'.
ELSE.
WRITE: / itab-orgid, 'Error'.
ENDIF.
ELSE.
WRITE: / itab-orgid , ' No procesada'.
ENDIF.
ENDLOOP.
ENDFORM. " TRATAR_DATOS
*&---------------------------------------------------------------------*
*& Form GRABAR_DATOS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM grabar_datos.
DATA l_key LIKE thead-tdname.
DATA: gt_lines TYPE n2_stext_tab,
gt_texto TYPE n2_stext_tab,
ls_lines TYPE tline.
CALL METHOD cl_ishmed_pmd_tools=>convert_pmd_type
EXPORTING
SOURCE = itab-text
CHANGING
RESULT = gt_lines
EXCEPTIONS
conversion_forbidden = 1
information_loss = 2
bad_information = 3
OTHERS = 4.
CALL FUNCTION 'FORMAT_TEXTLINES'
EXPORTING
cursor_column = 0
cursor_line = 0
endline = 99999
formatwidth = 72
linewidth = 72
startline = 1
language = sy-langu
TABLES
lines = gt_lines
EXCEPTIONS
bound_error = 1
OTHERS = 2.
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline
IN TABLE gt_lines
WITH '
.
REPLACE ALL OCCURRENCES OF '¥'
IN TABLE gt_lines
WITH space.
LOOP AT gt_lines INTO ls_lines.
PERFORM busca_saltos_linea TABLES gt_texto
USING ls_lines.
ENDLOOP.
CLEAR l_key.
l_key = itab-orgid.
PERFORM graba_texto TABLES gt_texto
USING l_key.
ENDFORM. " GRABAR_DATOS
*&---------------------------------------------------------------------*
*& Form busca_saltos_linea
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM busca_saltos_linea TABLES p_texto
USING p_lines STRUCTURE tline.
DATA: result_tab TYPE match_result_tab,
ls_restab TYPE match_result.
DATA: result_tmp TYPE match_result_tab,
ls_restmp TYPE match_result.
DATA: n_reg TYPE i,
n_ant TYPE i,
l_ini TYPE i,
l_long TYPE i,
l_fin TYPE i.
DATA: ls_lines TYPE tline.
FIND ALL OCCURRENCES OF '
IN p_lines-tdline
RESULTS result_tab.
ls_restab-line = 0.
ls_restab-offset = 72.
ls_restab-length = 1.
APPEND ls_restab TO result_tab.
DESCRIBE TABLE result_tab LINES n_reg.
CASE n_reg.
WHEN 0.
APPEND p_lines TO p_texto.
WHEN OTHERS.
result_tmp[] = result_tab[].
LOOP AT result_tab INTO ls_restab.
CLEAR: l_ini, l_fin, l_long, n_ant.
CASE sy-tabix.
WHEN 1.
l_ini = 0.
l_fin = ls_restab-offset.
WHEN OTHERS.
n_ant = sy-tabix - 1.
READ TABLE result_tmp INTO ls_restmp INDEX n_ant.
l_ini = ls_restmp-offset.
l_fin = ls_restab-offset.
ENDCASE.
l_long = l_fin - l_ini.
ls_lines-tdformat = '*'.
IF sy-tabix NE 1.
CHECK l_ini > 0.
ENDIF.
CHECK l_long > 0.
ls_lines-tdline = p_lines-tdline+l_ini(l_long).
REPLACE ALL OCCURRENCES OF '
IN ls_lines
WITH space.
CONDENSE ls_lines.
APPEND ls_lines TO p_texto.
ENDLOOP.
ENDCASE.
ENDFORM. "busca_saltos_linea
*&---------------------------------------------------------------------*
*& Form graba_texto
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_KEY text
*----------------------------------------------------------------------*
FORM graba_texto TABLES p_lt_lines
USING p_key.
DATA gs_header TYPE thead.
DATA ls_norg TYPE norg.
DATA: ls_line TYPE tline.
*Determinar Clave
CLEAR gs_header.
gs_header-tdobject = 'NORG'.
gs_header-tdname = p_key.
gs_header-tdid = '0001'.
gs_header-tdspras = sy-langu.
* Borrar primero para volver a cargar
CALL FUNCTION 'DELETE_TEXT'
EXPORTING
id = gs_header-tdid
language = gs_header-tdspras
name = gs_header-tdname
object = gs_header-tdobject
EXCEPTIONS
not_found = 1.
CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = gs_header
savemode_direct = 'X'
* IMPORTING
* newheader = gs_header
* function = l_update
TABLES
lines = p_lt_lines
EXCEPTIONS
OTHERS = 5.
IF sy-subrc = 0.
ls_norg-orgid = p_key.
"Actualizar la tabla NORG
SELECT SINGLE * INTO ls_norg FROM norg WHERE orgid = ls_norg-orgid.
READ TABLE p_lt_lines INTO ls_line INDEX 1.
ls_norg-kztxt = ls_line-tdline(72).
ls_norg-lgtxt = 'X'.
UPDATE norg FROM ls_norg.
COMMIT WORK.
ok_proc = 'X'.
ELSE.
CLEAR ok_proc.
ENDIF.
ENDFORM. "graba_texto