mirror of
https://github.com/abap2xlsx/abap2xlsx.git
synced 2025-05-05 19:16:10 +08:00
Merge branch 'main' into patch-2
This commit is contained in:
commit
d34f1e80c0
|
@ -102,9 +102,22 @@ CLASS zcl_excel_common DEFINITION
|
||||||
VALUE(ev_unescaped_string) TYPE string
|
VALUE(ev_unescaped_string) TYPE string
|
||||||
RAISING
|
RAISING
|
||||||
zcx_excel .
|
zcx_excel .
|
||||||
|
"! <p class="shorttext synchronized" lang="en">Convert date from Excel format to SAP</p>
|
||||||
|
"! @parameter ip_value | String being an Excel number representing a date (e.g. 45141 means 2023/08/03,
|
||||||
|
"! 45141.58832 means 2023/08/03 14:07:11). Important: if the input is date +
|
||||||
|
"! time, use the additional parameter IP_EXACT = 'X'.
|
||||||
|
"! @parameter ip_exact | If the input value also contains the time i.e. a fractional part exists
|
||||||
|
"! (e.g. 45141.58832 means 2023/08/03 14:07:11), ip_exact = 'X' will
|
||||||
|
"! return the exact date (e.g. 2023/08/03), while ip_exact = ' ' (default) will
|
||||||
|
"! return the rounded-up date (e.g. 2023/08/04). NB: this rounding-up doesn't
|
||||||
|
"! happen if the time is before 12:00:00.
|
||||||
|
"! @parameter ep_value | Date corresponding to the input Excel number. It returns a null date if
|
||||||
|
"! the input value contains non-numeric characters.
|
||||||
|
"! @raising zcx_excel | The numeric input corresponds to a date before 1900/1/1 or after 9999/12/31.
|
||||||
CLASS-METHODS excel_string_to_date
|
CLASS-METHODS excel_string_to_date
|
||||||
IMPORTING
|
IMPORTING
|
||||||
!ip_value TYPE zexcel_cell_value
|
!ip_value TYPE zexcel_cell_value
|
||||||
|
!ip_exact TYPE abap_bool DEFAULT abap_false
|
||||||
RETURNING
|
RETURNING
|
||||||
VALUE(ep_value) TYPE d
|
VALUE(ep_value) TYPE d
|
||||||
RAISING
|
RAISING
|
||||||
|
@ -860,11 +873,16 @@ CLASS zcl_excel_common IMPLEMENTATION.
|
||||||
|
|
||||||
METHOD excel_string_to_date.
|
METHOD excel_string_to_date.
|
||||||
DATA: lv_date_int TYPE i.
|
DATA: lv_date_int TYPE i.
|
||||||
|
DATA lv_error_text TYPE string.
|
||||||
|
|
||||||
CHECK ip_value IS NOT INITIAL AND ip_value CN ' 0'.
|
CHECK ip_value IS NOT INITIAL AND ip_value CN ' 0'.
|
||||||
|
|
||||||
TRY.
|
TRY.
|
||||||
|
IF ip_exact = abap_false.
|
||||||
lv_date_int = ip_value.
|
lv_date_int = ip_value.
|
||||||
|
ELSE.
|
||||||
|
lv_date_int = trunc( ip_value ).
|
||||||
|
ENDIF.
|
||||||
IF lv_date_int NOT BETWEEN 1 AND 2958465.
|
IF lv_date_int NOT BETWEEN 1 AND 2958465.
|
||||||
zcx_excel=>raise_text( 'Unable to interpret date' ).
|
zcx_excel=>raise_text( 'Unable to interpret date' ).
|
||||||
ENDIF.
|
ENDIF.
|
||||||
|
@ -876,7 +894,8 @@ CLASS zcl_excel_common IMPLEMENTATION.
|
||||||
ep_value = ep_value + 1.
|
ep_value = ep_value + 1.
|
||||||
ENDIF.
|
ENDIF.
|
||||||
CATCH cx_sy_conversion_error.
|
CATCH cx_sy_conversion_error.
|
||||||
zcx_excel=>raise_text( 'Index out of bounds' ).
|
lv_error_text = |String "{ ip_value }" is not a valid Excel date|.
|
||||||
|
zcx_excel=>raise_text( lv_error_text ).
|
||||||
ENDTRY.
|
ENDTRY.
|
||||||
ENDMETHOD.
|
ENDMETHOD.
|
||||||
|
|
||||||
|
@ -898,7 +917,7 @@ CLASS zcl_excel_common IMPLEMENTATION.
|
||||||
|
|
||||||
TRY.
|
TRY.
|
||||||
|
|
||||||
lv_day_fraction = ip_value.
|
lv_day_fraction = frac( ip_value ).
|
||||||
lv_seconds_in_day = lv_day_fraction * lc_seconds_in_day.
|
lv_seconds_in_day = lv_day_fraction * lc_seconds_in_day.
|
||||||
|
|
||||||
ep_value = lv_seconds_in_day.
|
ep_value = lv_seconds_in_day.
|
||||||
|
|
|
@ -55,6 +55,7 @@ CLASS lcl_excel_common_test DEFINITION FOR TESTING
|
||||||
METHODS excel_string_to_time3 FOR TESTING RAISING cx_static_check.
|
METHODS excel_string_to_time3 FOR TESTING RAISING cx_static_check.
|
||||||
METHODS excel_string_to_time4 FOR TESTING RAISING cx_static_check.
|
METHODS excel_string_to_time4 FOR TESTING RAISING cx_static_check.
|
||||||
METHODS excel_string_to_time5 FOR TESTING RAISING cx_static_check.
|
METHODS excel_string_to_time5 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS excel_string_to_time6 FOR TESTING RAISING cx_static_check.
|
||||||
METHODS time_to_excel_string1 FOR TESTING RAISING cx_static_check.
|
METHODS time_to_excel_string1 FOR TESTING RAISING cx_static_check.
|
||||||
METHODS time_to_excel_string2 FOR TESTING RAISING cx_static_check.
|
METHODS time_to_excel_string2 FOR TESTING RAISING cx_static_check.
|
||||||
METHODS time_to_excel_string3 FOR TESTING RAISING cx_static_check.
|
METHODS time_to_excel_string3 FOR TESTING RAISING cx_static_check.
|
||||||
|
@ -688,6 +689,45 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
|
||||||
level = if_aunit_constants=>fatal
|
level = if_aunit_constants=>fatal
|
||||||
).
|
).
|
||||||
ENDTRY.
|
ENDTRY.
|
||||||
|
|
||||||
|
* 45141.58832 (2023/08/03 14:07:11) ip_exact = abap_false -> 2023/08/04
|
||||||
|
TRY.
|
||||||
|
ep_value = zcl_excel_common=>excel_string_to_date( ip_value = '45141.58832'
|
||||||
|
ip_exact = abap_false ).
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = ep_value
|
||||||
|
exp = '20230804' ).
|
||||||
|
CATCH zcx_excel INTO lx_excel.
|
||||||
|
cl_abap_unit_assert=>fail(
|
||||||
|
msg = 'unexpected exception'
|
||||||
|
level = if_aunit_constants=>critical ).
|
||||||
|
ENDTRY.
|
||||||
|
|
||||||
|
* 45141.58832 (2023/08/03 14:07:11) ip_exact = abap_true -> 2023/08/03
|
||||||
|
TRY.
|
||||||
|
ep_value = zcl_excel_common=>excel_string_to_date( ip_value = '45141.58832'
|
||||||
|
ip_exact = abap_true ).
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = ep_value
|
||||||
|
exp = '20230803' ).
|
||||||
|
CATCH zcx_excel INTO lx_excel.
|
||||||
|
cl_abap_unit_assert=>fail(
|
||||||
|
msg = 'unexpected exception'
|
||||||
|
level = if_aunit_constants=>critical ).
|
||||||
|
ENDTRY.
|
||||||
|
|
||||||
|
* 45141.48832 (2023/08/03 11:43:11) ip_exact = abap_false -> 2023/08/03
|
||||||
|
TRY.
|
||||||
|
ep_value = zcl_excel_common=>excel_string_to_date( ip_value = '45141.48832'
|
||||||
|
ip_exact = abap_false ).
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = ep_value
|
||||||
|
exp = '20230803' ).
|
||||||
|
CATCH zcx_excel INTO lx_excel.
|
||||||
|
cl_abap_unit_assert=>fail(
|
||||||
|
msg = 'unexpected exception'
|
||||||
|
level = if_aunit_constants=>critical ).
|
||||||
|
ENDTRY.
|
||||||
ENDMETHOD. "excel_String_To_Date
|
ENDMETHOD. "excel_String_To_Date
|
||||||
|
|
||||||
|
|
||||||
|
@ -767,6 +807,21 @@ CLASS lcl_excel_common_test IMPLEMENTATION.
|
||||||
ENDTRY.
|
ENDTRY.
|
||||||
ENDMETHOD.
|
ENDMETHOD.
|
||||||
|
|
||||||
|
METHOD excel_string_to_time6.
|
||||||
|
DATA ep_value TYPE t.
|
||||||
|
* 45141.58832 (2023/08/03 14:07:11) -> 14:07:11
|
||||||
|
TRY.
|
||||||
|
ep_value = zcl_excel_common=>excel_string_to_time( ip_value = '45141.58832' ).
|
||||||
|
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = ep_value
|
||||||
|
exp = '140711' ).
|
||||||
|
|
||||||
|
CATCH zcx_excel INTO lx_excel.
|
||||||
|
cl_abap_unit_assert=>fail( lx_excel->get_text( ) ).
|
||||||
|
ENDTRY.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
METHOD time_to_excel_string1.
|
METHOD time_to_excel_string1.
|
||||||
DATA ep_value TYPE zexcel_cell_value.
|
DATA ep_value TYPE zexcel_cell_value.
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,10 @@ CLASS zcl_excel_style_number_format DEFINITION
|
||||||
CONSTANTS c_format_currency_simple2 TYPE zexcel_number_format VALUE '$#,##0.00_);($#,##0.00)'. "#EC NOTEXT
|
CONSTANTS c_format_currency_simple2 TYPE zexcel_number_format VALUE '$#,##0.00_);($#,##0.00)'. "#EC NOTEXT
|
||||||
CONSTANTS c_format_currency_simple_red2 TYPE zexcel_number_format VALUE '$#,##0.00_);[Red]($#,##0.00)'. "#EC NOTEXT
|
CONSTANTS c_format_currency_simple_red2 TYPE zexcel_number_format VALUE '$#,##0.00_);[Red]($#,##0.00)'. "#EC NOTEXT
|
||||||
CONSTANTS c_format_date_datetime TYPE zexcel_number_format VALUE 'd/m/y h:mm'. "#EC NOTEXT
|
CONSTANTS c_format_date_datetime TYPE zexcel_number_format VALUE 'd/m/y h:mm'. "#EC NOTEXT
|
||||||
|
"! Deprecated. Do not use this one, its value is dd/mm/yy, instead use the constant *_ddmmyyyy_new
|
||||||
CONSTANTS c_format_date_ddmmyyyy TYPE zexcel_number_format VALUE 'dd/mm/yy'. "#EC NOTEXT
|
CONSTANTS c_format_date_ddmmyyyy TYPE zexcel_number_format VALUE 'dd/mm/yy'. "#EC NOTEXT
|
||||||
|
CONSTANTS c_format_date_ddmmyyyy_new TYPE zexcel_number_format VALUE 'dd/mm/yyyy'. "#EC NOTEXT
|
||||||
|
CONSTANTS c_format_date_ddmmyy TYPE zexcel_number_format VALUE 'dd/mm/yy'. "#EC NOTEXT
|
||||||
CONSTANTS c_format_date_ddmmyyyydot TYPE zexcel_number_format VALUE 'dd\.mm\.yyyy'. "#EC NOTEXT
|
CONSTANTS c_format_date_ddmmyyyydot TYPE zexcel_number_format VALUE 'dd\.mm\.yyyy'. "#EC NOTEXT
|
||||||
CONSTANTS c_format_date_dmminus TYPE zexcel_number_format VALUE 'd-m'. "#EC NOTEXT
|
CONSTANTS c_format_date_dmminus TYPE zexcel_number_format VALUE 'd-m'. "#EC NOTEXT
|
||||||
CONSTANTS c_format_date_dmyminus TYPE zexcel_number_format VALUE 'd-m-y'. "#EC NOTEXT
|
CONSTANTS c_format_date_dmyminus TYPE zexcel_number_format VALUE 'd-m-y'. "#EC NOTEXT
|
||||||
|
|
|
@ -2328,7 +2328,8 @@ CLASS zcl_excel_worksheet IMPLEMENTATION.
|
||||||
IF sy-subrc EQ 0.
|
IF sy-subrc EQ 0.
|
||||||
CASE ls_style_conv-abap_type.
|
CASE ls_style_conv-abap_type.
|
||||||
WHEN cl_abap_typedescr=>typekind_date.
|
WHEN cl_abap_typedescr=>typekind_date.
|
||||||
<lv_data> = zcl_excel_common=>excel_string_to_date( <ls_sheet_content>-cell_value ).
|
<lv_data> = zcl_excel_common=>excel_string_to_date( ip_value = <ls_sheet_content>-cell_value
|
||||||
|
ip_exact = abap_true ).
|
||||||
WHEN cl_abap_typedescr=>typekind_time.
|
WHEN cl_abap_typedescr=>typekind_time.
|
||||||
<lv_data> = zcl_excel_common=>excel_string_to_time( <ls_sheet_content>-cell_value ).
|
<lv_data> = zcl_excel_common=>excel_string_to_time( <ls_sheet_content>-cell_value ).
|
||||||
ENDCASE.
|
ENDCASE.
|
||||||
|
@ -4433,7 +4434,7 @@ CLASS zcl_excel_worksheet IMPLEMENTATION.
|
||||||
|
|
||||||
METHOD set_table.
|
METHOD set_table.
|
||||||
|
|
||||||
DATA: lo_tabdescr TYPE REF TO cl_abap_structdescr,
|
DATA: lo_structdescr TYPE REF TO cl_abap_structdescr,
|
||||||
lr_data TYPE REF TO data,
|
lr_data TYPE REF TO data,
|
||||||
lt_dfies TYPE ddfields,
|
lt_dfies TYPE ddfields,
|
||||||
lv_row_int TYPE zexcel_cell_row,
|
lv_row_int TYPE zexcel_cell_row,
|
||||||
|
@ -4451,9 +4452,9 @@ CLASS zcl_excel_worksheet IMPLEMENTATION.
|
||||||
|
|
||||||
CREATE DATA lr_data LIKE LINE OF ip_table.
|
CREATE DATA lr_data LIKE LINE OF ip_table.
|
||||||
|
|
||||||
lo_tabdescr ?= cl_abap_structdescr=>describe_by_data_ref( lr_data ).
|
lo_structdescr ?= cl_abap_structdescr=>describe_by_data_ref( lr_data ).
|
||||||
|
|
||||||
lt_dfies = lo_tabdescr->get_ddic_field_list( ).
|
lt_dfies = zcl_excel_common=>describe_structure( io_struct = lo_structdescr ).
|
||||||
|
|
||||||
* It is better to loop column by column
|
* It is better to loop column by column
|
||||||
LOOP AT lt_dfies ASSIGNING <fs_dfies>.
|
LOOP AT lt_dfies ASSIGNING <fs_dfies>.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user