CSV Initial External Date

Added code to set an initial external date other than the value returned by function module 'CONVERT_DATE_TO_EXTERNAL'.
This commit is contained in:
Aditya Waghmare 2024-02-14 23:30:25 +00:00
parent ebb27467cb
commit 93c7970529

View File

@ -1,58 +1,58 @@
class zcl_excel_writer_csv definition CLASS zcl_excel_writer_csv DEFINITION
public PUBLIC
final FINAL
create public . CREATE PUBLIC .
*"* public components of class ZCL_EXCEL_WRITER_CSV *"* public components of class ZCL_EXCEL_WRITER_CSV
*"* do not include other source files here!!! *"* do not include other source files here!!!
*"* protected components of class ZCL_EXCEL_WRITER_2007 *"* protected components of class ZCL_EXCEL_WRITER_2007
*"* do not include other source files here!!! *"* do not include other source files here!!!
public section. PUBLIC SECTION.
interfaces zif_excel_writer . INTERFACES zif_excel_writer .
class-methods set_delimiter CLASS-METHODS set_delimiter
importing IMPORTING
value(ip_value) type c default ';' . VALUE(ip_value) TYPE c DEFAULT ';' .
class-methods set_enclosure CLASS-METHODS set_enclosure
importing IMPORTING
value(ip_value) type c default '"' . VALUE(ip_value) TYPE c DEFAULT '"' .
class-methods set_endofline CLASS-METHODS set_endofline
importing IMPORTING
value(ip_value) type any default cl_abap_char_utilities=>cr_lf . VALUE(ip_value) TYPE any DEFAULT cl_abap_char_utilities=>cr_lf .
class-methods set_active_sheet_index CLASS-METHODS set_active_sheet_index
importing IMPORTING
!i_active_worksheet type zexcel_active_worksheet . !i_active_worksheet TYPE zexcel_active_worksheet .
class-methods set_active_sheet_index_by_name CLASS-METHODS set_active_sheet_index_by_name
importing IMPORTING
!i_worksheet_name type zexcel_worksheets_name . !i_worksheet_name TYPE zexcel_worksheets_name .
class-methods set_initial_ext_date CLASS-METHODS set_initial_ext_date
importing IMPORTING
!ip_value type char10 default 'DEFAULT' . !ip_value TYPE char10 DEFAULT 'DEFAULT' .
protected section. PROTECTED SECTION.
*"* private components of class ZCL_EXCEL_WRITER_CSV *"* private components of class ZCL_EXCEL_WRITER_CSV
*"* do not include other source files here!!! *"* do not include other source files here!!!
private section. PRIVATE SECTION.
data excel type ref to zcl_excel . DATA excel TYPE REF TO zcl_excel .
class-data delimiter type c value ';' ##NO_TEXT. CLASS-DATA delimiter TYPE c VALUE ';' ##NO_TEXT.
class-data enclosure type c value '"' ##NO_TEXT. CLASS-DATA enclosure TYPE c VALUE '"' ##NO_TEXT.
class-data: CLASS-DATA:
eol type c length 2 value cl_abap_char_utilities=>cr_lf ##NO_TEXT. eol TYPE c LENGTH 2 VALUE cl_abap_char_utilities=>cr_lf ##NO_TEXT.
class-data worksheet_name type zexcel_worksheets_name . CLASS-DATA worksheet_name TYPE zexcel_worksheets_name .
class-data worksheet_index type zexcel_active_worksheet . CLASS-DATA worksheet_index TYPE zexcel_active_worksheet .
class-data initial_ext_date type char10 value 'DEFAULT' ##NO_TEXT. CLASS-DATA initial_ext_date TYPE char10 VALUE 'DEFAULT' ##NO_TEXT.
constants c_default type char10 value 'DEFAULT' ##NO_TEXT. CONSTANTS c_default TYPE char10 VALUE 'DEFAULT' ##NO_TEXT.
methods create METHODS create
returning RETURNING
value(ep_excel) type xstring VALUE(ep_excel) TYPE xstring
raising RAISING
zcx_excel . zcx_excel .
methods create_csv METHODS create_csv
returning RETURNING
value(ep_content) type xstring VALUE(ep_content) TYPE xstring
raising RAISING
zcx_excel . zcx_excel .
ENDCLASS. ENDCLASS.
@ -61,7 +61,7 @@ ENDCLASS.
CLASS ZCL_EXCEL_WRITER_CSV IMPLEMENTATION. CLASS ZCL_EXCEL_WRITER_CSV IMPLEMENTATION.
method create. METHOD create.
* .csv format with ; delimiter * .csv format with ; delimiter
@ -71,253 +71,253 @@ CLASS ZCL_EXCEL_WRITER_CSV IMPLEMENTATION.
ep_excel = me->create_csv( ). ep_excel = me->create_csv( ).
endmethod. ENDMETHOD.
method create_csv. METHOD create_csv.
types: begin of lty_format, TYPES: BEGIN OF lty_format,
cmpname type seocmpname, cmpname TYPE seocmpname,
attvalue type seovalue, attvalue TYPE seovalue,
end of lty_format. END OF lty_format.
data: lt_format type standard table of lty_format, DATA: lt_format TYPE STANDARD TABLE OF lty_format,
ls_format like line of lt_format, ls_format LIKE LINE OF lt_format,
lv_date type d, lv_date TYPE d,
lv_tmp type string, lv_tmp TYPE string,
lv_time type c length 8. lv_time TYPE c LENGTH 8.
data: lo_iterator type ref to zcl_excel_collection_iterator, DATA: lo_iterator TYPE REF TO zcl_excel_collection_iterator,
lo_worksheet type ref to zcl_excel_worksheet. lo_worksheet TYPE REF TO zcl_excel_worksheet.
data: lt_cell_data type zexcel_t_cell_data_unsorted, DATA: lt_cell_data TYPE zexcel_t_cell_data_unsorted,
lv_row type i, lv_row TYPE i,
lv_col type i, lv_col TYPE i,
lv_string type string, lv_string TYPE string,
lc_value type string, lc_value TYPE string,
lv_attrname type seocmpname. lv_attrname TYPE seocmpname.
data: ls_numfmt type zexcel_s_style_numfmt, DATA: ls_numfmt TYPE zexcel_s_style_numfmt,
lo_style type ref to zcl_excel_style. lo_style TYPE REF TO zcl_excel_style.
field-symbols: <fs_sheet_content> type zexcel_s_cell_data. FIELD-SYMBOLS: <fs_sheet_content> TYPE zexcel_s_cell_data.
* --- Retrieve supported cell format * --- Retrieve supported cell format
select * into corresponding fields of table lt_format SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_format
from seocompodf FROM seocompodf
where clsname = 'ZCL_EXCEL_STYLE_NUMBER_FORMAT' WHERE clsname = 'ZCL_EXCEL_STYLE_NUMBER_FORMAT'
and typtype = 1 AND typtype = 1
and type = 'ZEXCEL_NUMBER_FORMAT'. AND type = 'ZEXCEL_NUMBER_FORMAT'.
* --- Retrieve SAP date format * --- Retrieve SAP date format
clear ls_format. CLEAR ls_format.
select ddtext into ls_format-attvalue from dd07t where domname = 'XUDATFM' SELECT ddtext INTO ls_format-attvalue FROM dd07t WHERE domname = 'XUDATFM'
and ddlanguage = sy-langu. AND ddlanguage = sy-langu.
ls_format-cmpname = 'DATE'. ls_format-cmpname = 'DATE'.
condense ls_format-attvalue. CONDENSE ls_format-attvalue.
concatenate '''' ls_format-attvalue '''' into ls_format-attvalue. CONCATENATE '''' ls_format-attvalue '''' INTO ls_format-attvalue.
append ls_format to lt_format. APPEND ls_format TO lt_format.
endselect. ENDSELECT.
loop at lt_format into ls_format. LOOP AT lt_format INTO ls_format.
translate ls_format-attvalue to upper case. TRANSLATE ls_format-attvalue TO UPPER CASE.
modify lt_format from ls_format. MODIFY lt_format FROM ls_format.
endloop. ENDLOOP.
* STEP 1: Collect strings from the first worksheet * STEP 1: Collect strings from the first worksheet
lo_iterator = excel->get_worksheets_iterator( ). lo_iterator = excel->get_worksheets_iterator( ).
data: current_worksheet_title type zexcel_sheet_title. DATA: current_worksheet_title TYPE zexcel_sheet_title.
while lo_iterator->has_next( ) eq abap_true. WHILE lo_iterator->has_next( ) EQ abap_true.
lo_worksheet ?= lo_iterator->get_next( ). lo_worksheet ?= lo_iterator->get_next( ).
if worksheet_name is not initial. IF worksheet_name IS NOT INITIAL.
current_worksheet_title = lo_worksheet->get_title( ). current_worksheet_title = lo_worksheet->get_title( ).
check current_worksheet_title = worksheet_name. CHECK current_worksheet_title = worksheet_name.
else. ELSE.
if worksheet_index is initial. IF worksheet_index IS INITIAL.
worksheet_index = 1. worksheet_index = 1.
endif. ENDIF.
check worksheet_index = sy-index. CHECK worksheet_index = sy-index.
endif. ENDIF.
append lines of lo_worksheet->sheet_content to lt_cell_data. APPEND LINES OF lo_worksheet->sheet_content TO lt_cell_data.
exit. " Take first worksheet only EXIT. " Take first worksheet only
endwhile. ENDWHILE.
delete lt_cell_data where cell_formula is not initial. " delete formula content DELETE lt_cell_data WHERE cell_formula IS NOT INITIAL. " delete formula content
sort lt_cell_data by cell_row SORT lt_cell_data BY cell_row
cell_column. cell_column.
lv_row = 1. lv_row = 1.
lv_col = 1. lv_col = 1.
clear lv_string. CLEAR lv_string.
loop at lt_cell_data assigning <fs_sheet_content>. LOOP AT lt_cell_data ASSIGNING <fs_sheet_content>.
* --- Retrieve Cell Style format and data type * --- Retrieve Cell Style format and data type
clear ls_numfmt. CLEAR ls_numfmt.
if <fs_sheet_content>-data_type is initial and <fs_sheet_content>-cell_style is not initial. IF <fs_sheet_content>-data_type IS INITIAL AND <fs_sheet_content>-cell_style IS NOT INITIAL.
lo_iterator = excel->get_styles_iterator( ). lo_iterator = excel->get_styles_iterator( ).
while lo_iterator->has_next( ) eq abap_true. WHILE lo_iterator->has_next( ) EQ abap_true.
lo_style ?= lo_iterator->get_next( ). lo_style ?= lo_iterator->get_next( ).
check lo_style->get_guid( ) = <fs_sheet_content>-cell_style. CHECK lo_style->get_guid( ) = <fs_sheet_content>-cell_style.
ls_numfmt = lo_style->number_format->get_structure( ). ls_numfmt = lo_style->number_format->get_structure( ).
exit. EXIT.
endwhile. ENDWHILE.
endif. ENDIF.
if <fs_sheet_content>-data_type is initial and ls_numfmt is not initial. IF <fs_sheet_content>-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL.
" determine data-type " determine data-type
clear lv_attrname. CLEAR lv_attrname.
concatenate '''' ls_numfmt-numfmt '''' into ls_numfmt-numfmt. CONCATENATE '''' ls_numfmt-numfmt '''' INTO ls_numfmt-numfmt.
translate ls_numfmt-numfmt to upper case. TRANSLATE ls_numfmt-numfmt TO UPPER CASE.
read table lt_format into ls_format with key attvalue = ls_numfmt-numfmt. READ TABLE lt_format INTO ls_format WITH KEY attvalue = ls_numfmt-numfmt.
if sy-subrc = 0. IF sy-subrc = 0.
lv_attrname = ls_format-cmpname. lv_attrname = ls_format-cmpname.
endif. ENDIF.
if lv_attrname is not initial. IF lv_attrname IS NOT INITIAL.
find first occurrence of 'DATETIME' in lv_attrname. FIND FIRST OCCURRENCE OF 'DATETIME' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 'd'. <fs_sheet_content>-data_type = 'd'.
else. ELSE.
find first occurrence of 'TIME' in lv_attrname. FIND FIRST OCCURRENCE OF 'TIME' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 't'. <fs_sheet_content>-data_type = 't'.
else. ELSE.
find first occurrence of 'DATE' in lv_attrname. FIND FIRST OCCURRENCE OF 'DATE' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 'd'. <fs_sheet_content>-data_type = 'd'.
else. ELSE.
find first occurrence of 'CURRENCY' in lv_attrname. FIND FIRST OCCURRENCE OF 'CURRENCY' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 'n'. <fs_sheet_content>-data_type = 'n'.
else. ELSE.
find first occurrence of 'NUMBER' in lv_attrname. FIND FIRST OCCURRENCE OF 'NUMBER' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 'n'. <fs_sheet_content>-data_type = 'n'.
else. ELSE.
find first occurrence of 'PERCENTAGE' in lv_attrname. FIND FIRST OCCURRENCE OF 'PERCENTAGE' IN lv_attrname.
if sy-subrc = 0. IF sy-subrc = 0.
<fs_sheet_content>-data_type = 'n'. <fs_sheet_content>-data_type = 'n'.
endif. " Purcentage ENDIF. " Purcentage
endif. " Number ENDIF. " Number
endif. " Currency ENDIF. " Currency
endif. " Date ENDIF. " Date
endif. " TIME ENDIF. " TIME
endif. " DATETIME ENDIF. " DATETIME
endif. " lv_attrname IS NOT INITIAL. ENDIF. " lv_attrname IS NOT INITIAL.
endif. " <fs_sheet_content>-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL. ENDIF. " <fs_sheet_content>-data_type IS INITIAL AND ls_numfmt IS NOT INITIAL.
* --- Add empty rows * --- Add empty rows
while lv_row < <fs_sheet_content>-cell_row. WHILE lv_row < <fs_sheet_content>-cell_row.
concatenate lv_string zcl_excel_writer_csv=>eol into lv_string. CONCATENATE lv_string zcl_excel_writer_csv=>eol INTO lv_string.
lv_row = lv_row + 1. lv_row = lv_row + 1.
lv_col = 1. lv_col = 1.
endwhile. ENDWHILE.
* --- Add empty columns * --- Add empty columns
while lv_col < <fs_sheet_content>-cell_column. WHILE lv_col < <fs_sheet_content>-cell_column.
concatenate lv_string zcl_excel_writer_csv=>delimiter into lv_string. CONCATENATE lv_string zcl_excel_writer_csv=>delimiter INTO lv_string.
lv_col = lv_col + 1. lv_col = lv_col + 1.
endwhile. ENDWHILE.
* ----- Use format to determine the data type and display format. * ----- Use format to determine the data type and display format.
case <fs_sheet_content>-data_type. CASE <fs_sheet_content>-data_type.
when 'd' or 'D'. WHEN 'd' OR 'D'.
if <fs_sheet_content>-cell_value is initial and initial_ext_date <> c_default. IF <fs_sheet_content>-cell_value IS INITIAL AND initial_ext_date <> c_default.
lc_value = initial_ext_date. lc_value = initial_ext_date.
else. ELSE.
lc_value = zcl_excel_common=>excel_string_to_date( ip_value = <fs_sheet_content>-cell_value ). lc_value = zcl_excel_common=>excel_string_to_date( ip_value = <fs_sheet_content>-cell_value ).
try. TRY.
lv_date = lc_value. lv_date = lc_value.
call function 'CONVERT_DATE_TO_EXTERNAL' CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
exporting EXPORTING
date_internal = lv_date date_internal = lv_date
importing IMPORTING
date_external = lv_tmp date_external = lv_tmp
exceptions EXCEPTIONS
date_internal_is_invalid = 1 date_internal_is_invalid = 1
others = 2. OTHERS = 2.
if sy-subrc = 0. IF sy-subrc = 0.
lc_value = lv_tmp. lc_value = lv_tmp.
endif. ENDIF.
catch cx_sy_conversion_no_number. CATCH cx_sy_conversion_no_number.
endtry. ENDTRY.
endif. ENDIF.
when 't' or 'T'. WHEN 't' OR 'T'.
lc_value = zcl_excel_common=>excel_string_to_time( ip_value = <fs_sheet_content>-cell_value ). lc_value = zcl_excel_common=>excel_string_to_time( ip_value = <fs_sheet_content>-cell_value ).
write lc_value to lv_time using edit mask '__:__:__'. WRITE lc_value TO lv_time USING EDIT MASK '__:__:__'.
lc_value = lv_time. lc_value = lv_time.
when others. WHEN OTHERS.
lc_value = <fs_sheet_content>-cell_value. lc_value = <fs_sheet_content>-cell_value.
endcase. ENDCASE.
concatenate zcl_excel_writer_csv=>enclosure zcl_excel_writer_csv=>enclosure into lv_tmp. CONCATENATE zcl_excel_writer_csv=>enclosure zcl_excel_writer_csv=>enclosure INTO lv_tmp.
condense lv_tmp. CONDENSE lv_tmp.
replace all occurrences of zcl_excel_writer_csv=>enclosure in lc_value with lv_tmp. REPLACE ALL OCCURRENCES OF zcl_excel_writer_csv=>enclosure IN lc_value WITH lv_tmp.
find first occurrence of zcl_excel_writer_csv=>delimiter in lc_value. FIND FIRST OCCURRENCE OF zcl_excel_writer_csv=>delimiter IN lc_value.
if sy-subrc = 0. IF sy-subrc = 0.
concatenate lv_string zcl_excel_writer_csv=>enclosure lc_value zcl_excel_writer_csv=>enclosure into lv_string. CONCATENATE lv_string zcl_excel_writer_csv=>enclosure lc_value zcl_excel_writer_csv=>enclosure INTO lv_string.
else. ELSE.
concatenate lv_string lc_value into lv_string. CONCATENATE lv_string lc_value INTO lv_string.
endif. ENDIF.
endloop. ENDLOOP.
clear ep_content. CLEAR ep_content.
call function 'SCMS_STRING_TO_XSTRING' CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
exporting EXPORTING
text = lv_string text = lv_string
importing IMPORTING
buffer = ep_content buffer = ep_content
exceptions EXCEPTIONS
failed = 1 failed = 1
others = 2. OTHERS = 2.
endmethod. ENDMETHOD.
method set_active_sheet_index. METHOD set_active_sheet_index.
clear worksheet_name. CLEAR worksheet_name.
worksheet_index = i_active_worksheet. worksheet_index = i_active_worksheet.
endmethod. ENDMETHOD.
method set_active_sheet_index_by_name. METHOD set_active_sheet_index_by_name.
clear worksheet_index. CLEAR worksheet_index.
worksheet_name = i_worksheet_name. worksheet_name = i_worksheet_name.
endmethod. ENDMETHOD.
method set_delimiter. METHOD set_delimiter.
delimiter = ip_value. delimiter = ip_value.
endmethod. ENDMETHOD.
method set_enclosure. METHOD set_enclosure.
zcl_excel_writer_csv=>enclosure = ip_value. zcl_excel_writer_csv=>enclosure = ip_value.
endmethod. ENDMETHOD.
method set_endofline. METHOD set_endofline.
zcl_excel_writer_csv=>eol = ip_value. zcl_excel_writer_csv=>eol = ip_value.
endmethod. ENDMETHOD.
method zif_excel_writer~write_file. METHOD zif_excel_writer~write_file.
me->excel = io_excel. me->excel = io_excel.
ep_file = me->create( ). ep_file = me->create( ).
endmethod. ENDMETHOD.
method set_initial_ext_date. METHOD set_initial_ext_date.
initial_ext_date = ip_value. initial_ext_date = ip_value.
endmethod. ENDMETHOD.
ENDCLASS. ENDCLASS.