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:22:56 +00:00
parent 34e319eec3
commit ebb27467cb

View File

@ -1,59 +1,59 @@
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,144 +61,144 @@ 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
* Start of insertion # issue 1134 - Dateretention of cellstyles(issue #139) * Start of insertion # issue 1134 - Dateretention of cellstyles(issue #139)
ME->EXCEL->ADD_STATIC_STYLES( ). me->excel->add_static_styles( ).
* End of insertion # issue 1134 - Dateretention of cellstyles(issue #139) * End of insertion # issue 1134 - Dateretention of cellstyles(issue #139)
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
@ -209,115 +209,115 @@ CLASS ZCL_EXCEL_WRITER_CSV IMPLEMENTATION.
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.