From d6aa122ea1c684090c6d546057a142c36a2c68b1 Mon Sep 17 00:00:00 2001 From: sandraros Date: Mon, 5 Aug 2024 15:26:34 +0000 Subject: [PATCH] Code to handle the UTCLONG type added in ABAP 7.54 Fix #1252 --- src/zcl_excel_common.clas.abap | 20 +++++++++++ src/zcl_excel_common.clas.testclasses.abap | 41 ++++++++++++++++++++++ src/zcl_excel_worksheet.clas.abap | 36 +++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/src/zcl_excel_common.clas.abap b/src/zcl_excel_common.clas.abap index 67a9e6c..917486b 100644 --- a/src/zcl_excel_common.clas.abap +++ b/src/zcl_excel_common.clas.abap @@ -166,6 +166,11 @@ CLASS zcl_excel_common DEFINITION !ip_value TYPE t RETURNING VALUE(ep_value) TYPE zexcel_cell_value . + CLASS-METHODS utclong_to_excel_string + IMPORTING + !ip_utclong TYPE any + RETURNING + VALUE(ep_value) TYPE zexcel_cell_value . TYPES: t_char10 TYPE c LENGTH 10. TYPES: t_char255 TYPE c LENGTH 255. CLASS-METHODS split_file @@ -1725,4 +1730,19 @@ CLASS zcl_excel_common IMPLEMENTATION. ENDMETHOD. + METHOD utclong_to_excel_string. + DATA lv_timestamp TYPE timestamp. + DATA lv_date TYPE d. + DATA lv_time TYPE t. + + " The data type UTCLONG and the method UTCLONG2TSTMP_SHORT are not available before ABAP 7.54 + " -> Need of a dynamic call to avoid compilation error before ABAP 7.54 + + CALL METHOD cl_abap_tstmp=>('UTCLONG2TSTMP_SHORT') + EXPORTING utclong = ip_utclong + RECEIVING timestamp = lv_timestamp. + CONVERT TIME STAMP lv_timestamp TIME ZONE 'UTC ' INTO DATE lv_date TIME lv_time. + ep_value = |{ date_to_excel_string( lv_date ) + time_to_excel_string( lv_time ) }|. + ENDMETHOD. + ENDCLASS. diff --git a/src/zcl_excel_common.clas.testclasses.abap b/src/zcl_excel_common.clas.testclasses.abap index bf5f471..a6e2970 100644 --- a/src/zcl_excel_common.clas.testclasses.abap +++ b/src/zcl_excel_common.clas.testclasses.abap @@ -136,6 +136,17 @@ CLASS lcl_excel_common_test DEFINITION FOR TESTING ENDCLASS. +CLASS ltc_utclong_to_excel_string DEFINITION + FOR TESTING + RISK LEVEL HARMLESS + DURATION SHORT. + + PRIVATE SECTION. + + METHODS simple FOR TESTING. + +ENDCLASS. + *----------------------------------------------------------------------* * CLASS lcl_Excel_Common_Test IMPLEMENTATION @@ -1812,3 +1823,33 @@ CLASS lcl_excel_common_test IMPLEMENTATION. ENDMETHOD. ENDCLASS. + + +CLASS ltc_utclong_to_excel_string IMPLEMENTATION. + METHOD simple. + FIELD-SYMBOLS TYPE abap_typekind. + FIELD-SYMBOLS TYPE simple. + DATA lo_rtti_utclong TYPE REF TO cl_abap_datadescr. + DATA lv_variable_utclong TYPE REF TO data. + DATA lv_excel_string TYPE zexcel_cell_value. + + " Skip this test before ABAP 7.54 (UTCLONG does not exist). + " Need of dynamic referencing and dynamic call to avoid compilation error before ABAP 7.54. + + ASSIGN ('CL_ABAP_TYPEDESCR=>TYPEKIND_UTCLONG') TO . + IF sy-subrc <> 0. + RETURN. + ENDIF. + + CALL METHOD cl_abap_elemdescr=>('GET_UTCLONG') + RECEIVING p_result = lo_rtti_utclong. + CREATE DATA lv_variable_utclong TYPE HANDLE lo_rtti_utclong. + ASSIGN lv_variable_utclong->* TO . + + = '2024-08-04 19:47:00.9999999'. + lv_excel_string = zcl_excel_common=>utclong_to_excel_string( ). + + cl_abap_unit_assert=>assert_equals( exp = '45508.82430555555556' + act = lv_excel_string ). + ENDMETHOD. +ENDCLASS. diff --git a/src/zcl_excel_worksheet.clas.abap b/src/zcl_excel_worksheet.clas.abap index ec2e692..169e293 100644 --- a/src/zcl_excel_worksheet.clas.abap +++ b/src/zcl_excel_worksheet.clas.abap @@ -708,6 +708,10 @@ CLASS zcl_excel_worksheet DEFINITION *"* private components of class ZCL_EXCEL_WORKSHEET *"* do not include other source files here!!! TYPES ty_table_settings TYPE STANDARD TABLE OF zexcel_s_table_settings WITH DEFAULT KEY. + + CLASS-DATA typekind_utclong TYPE abap_typekind. + CLASS-DATA variable_utclong TYPE REF TO data. + DATA active_cell TYPE zexcel_s_cell_data . DATA charts TYPE REF TO zcl_excel_drawings . DATA columns TYPE REF TO zcl_excel_columns . @@ -2023,12 +2027,21 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. METHOD class_constructor. + FIELD-SYMBOLS TYPE abap_typekind. + DATA lo_rtti TYPE REF TO cl_abap_datadescr. c_messages-formula_id_only_is_possible = |{ 'If Formula ID is used, value and formula must be empty'(008) }|. c_messages-column_formula_id_not_found = |{ 'The Column Formula does not exist'(009) }|. c_messages-formula_not_in_this_table = |{ 'The cell uses a Column Formula which should be part of the same table'(010) }|. c_messages-formula_in_other_column = |{ 'The cell uses a Column Formula which is in a different column'(011) }|. + ASSIGN ('CL_ABAP_TYPEDESCR=>TYPEKIND_UTCLONG') TO . + IF sy-subrc = 0. + typekind_utclong = . + CALL METHOD cl_abap_elemdescr=>('GET_UTCLONG') RECEIVING p_result = lo_rtti. + CREATE DATA variable_utclong TYPE HANDLE lo_rtti. + ENDIF. + ENDMETHOD. @@ -3847,6 +3860,7 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. TYPE abap_typekind. FIELD-SYMBOLS: TYPE mty_s_column_formula. FIELD-SYMBOLS: TYPE zexcel_s_fieldcatalog. + FIELD-SYMBOLS TYPE simple. IF ip_value IS NOT SUPPLIED AND ip_formula IS NOT SUPPLIED @@ -3990,6 +4004,13 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. * ENDIF. * End of change issue #152 - don't touch exisiting style if only value is passed + WHEN typekind_utclong. + ASSIGN variable_utclong->* TO . + IF sy-subrc = 0. + = . + lv_value = zcl_excel_common=>utclong_to_excel_string( ). + ENDIF. + WHEN OTHERS. zcx_excel=>raise_text( 'Invalid data type of input value' ). ENDCASE. @@ -4095,6 +4116,21 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. ip_row = lv_row ip_number_format_format_code = lo_format_code_datetime ). + WHEN typekind_utclong. + TRY. + stylemapping = me->excel->get_style_to_guid( -cell_style ). + CATCH zcx_excel . + ENDTRY. + IF stylemapping-complete_stylex-number_format-format_code IS INITIAL OR + stylemapping-complete_style-number_format-format_code IS INITIAL. + lo_format_code_datetime = zcl_excel_style_number_format=>c_format_date_datetime. + ELSE. + lo_format_code_datetime = stylemapping-complete_style-number_format-format_code. + ENDIF. + me->change_cell_style( ip_column = lv_column + ip_row = lv_row + ip_number_format_format_code = lo_format_code_datetime ). + ENDCASE. * End of change issue #152 - don't touch exisiting style if only value is passed