diff --git a/src/zcl_excel_comment.clas.abap b/src/zcl_excel_comment.clas.abap index d25310e..a285701 100644 --- a/src/zcl_excel_comment.clas.abap +++ b/src/zcl_excel_comment.clas.abap @@ -5,28 +5,42 @@ CLASS zcl_excel_comment DEFINITION PUBLIC SECTION. + CONSTANTS default_right_column TYPE i VALUE 4. "#EC NOTEXT + CONSTANTS default_bottom_row TYPE i VALUE 15. "#EC NOTEXT + METHODS constructor . - METHODS get_name + METHODS get_bottom_row RETURNING - VALUE(r_name) TYPE string . + VALUE(rp_result) TYPE i . METHODS get_index RETURNING VALUE(rp_index) TYPE string . + METHODS get_name + RETURNING + VALUE(r_name) TYPE string . METHODS get_ref RETURNING VALUE(rp_ref) TYPE string . + METHODS get_right_column + RETURNING + VALUE(rp_result) TYPE i . METHODS get_text RETURNING VALUE(rp_text) TYPE string . METHODS set_text IMPORTING - !ip_text TYPE string - !ip_ref TYPE string OPTIONAL . + !ip_text TYPE string + !ip_ref TYPE string OPTIONAL + !ip_right_column TYPE i OPTIONAL + !ip_bottom_row TYPE i OPTIONAL . + PROTECTED SECTION. PRIVATE SECTION. + DATA bottom_row TYPE i . DATA index TYPE string . DATA ref TYPE string . + DATA right_column TYPE i . DATA text TYPE string . ENDCLASS. @@ -40,6 +54,11 @@ CLASS zcl_excel_comment IMPLEMENTATION. ENDMETHOD. + METHOD get_bottom_row. + rp_result = bottom_row. + ENDMETHOD. + + METHOD get_index. rp_index = me->index. ENDMETHOD. @@ -55,6 +74,11 @@ CLASS zcl_excel_comment IMPLEMENTATION. ENDMETHOD. + METHOD get_right_column. + rp_result = right_column. + ENDMETHOD. + + METHOD get_text. rp_text = me->text. ENDMETHOD. @@ -66,5 +90,18 @@ CLASS zcl_excel_comment IMPLEMENTATION. IF ip_ref IS SUPPLIED. me->ref = ip_ref. ENDIF. + + IF ip_right_column IS NOT INITIAL. + me->right_column = ip_right_column. + ELSE. + me->right_column = default_right_column. + ENDIF. + + IF ip_bottom_row IS NOT INITIAL. + me->bottom_row = ip_bottom_row. + ELSE. + me->bottom_row = default_bottom_row. + ENDIF. ENDMETHOD. + ENDCLASS. diff --git a/src/zcl_excel_common.clas.abap b/src/zcl_excel_common.clas.abap index 9d9300a..67a9e6c 100644 --- a/src/zcl_excel_common.clas.abap +++ b/src/zcl_excel_common.clas.abap @@ -102,9 +102,22 @@ CLASS zcl_excel_common DEFINITION VALUE(ev_unescaped_string) TYPE string RAISING zcx_excel . + "!

Convert date from Excel format to SAP

+ "! @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 IMPORTING !ip_value TYPE zexcel_cell_value + !ip_exact TYPE abap_bool DEFAULT abap_false RETURNING VALUE(ep_value) TYPE d RAISING @@ -860,11 +873,16 @@ CLASS zcl_excel_common IMPLEMENTATION. METHOD excel_string_to_date. DATA: lv_date_int TYPE i. + DATA lv_error_text TYPE string. CHECK ip_value IS NOT INITIAL AND ip_value CN ' 0'. TRY. - lv_date_int = ip_value. + IF ip_exact = abap_false. + lv_date_int = ip_value. + ELSE. + lv_date_int = trunc( ip_value ). + ENDIF. IF lv_date_int NOT BETWEEN 1 AND 2958465. zcx_excel=>raise_text( 'Unable to interpret date' ). ENDIF. @@ -876,7 +894,8 @@ CLASS zcl_excel_common IMPLEMENTATION. ep_value = ep_value + 1. ENDIF. 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. ENDMETHOD. @@ -898,7 +917,7 @@ CLASS zcl_excel_common IMPLEMENTATION. TRY. - lv_day_fraction = ip_value. + lv_day_fraction = frac( ip_value ). lv_seconds_in_day = lv_day_fraction * lc_seconds_in_day. ep_value = lv_seconds_in_day. diff --git a/src/zcl_excel_common.clas.testclasses.abap b/src/zcl_excel_common.clas.testclasses.abap index 011dc9d..bf5f471 100644 --- a/src/zcl_excel_common.clas.testclasses.abap +++ b/src/zcl_excel_common.clas.testclasses.abap @@ -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_time4 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_string2 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 ). 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 @@ -767,6 +807,21 @@ CLASS lcl_excel_common_test IMPLEMENTATION. ENDTRY. 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. DATA ep_value TYPE zexcel_cell_value. diff --git a/src/zcl_excel_reader_2007.clas.abap b/src/zcl_excel_reader_2007.clas.abap index c130620..0731548 100644 --- a/src/zcl_excel_reader_2007.clas.abap +++ b/src/zcl_excel_reader_2007.clas.abap @@ -2778,7 +2778,8 @@ CLASS zcl_excel_reader_2007 IMPLEMENTATION. * issue #367 - hide columns from IF ls_column-max = zcl_excel_common=>c_excel_sheet_max_col. " Max = very right column - IF ls_column-hidden = 1 " all hidden + IF ( ls_column-hidden = lc_xml_attr_true + OR ls_column-hidden = lc_xml_attr_true_int ) " all hidden AND ls_column-min > 0. io_worksheet->zif_excel_sheet_properties~hide_columns_from = zcl_excel_common=>convert_column2alpha( ls_column-min ). ELSEIF ls_column-style > ''. diff --git a/src/zcl_excel_worksheet.clas.abap b/src/zcl_excel_worksheet.clas.abap index 81b24f0..cfa86a3 100644 --- a/src/zcl_excel_worksheet.clas.abap +++ b/src/zcl_excel_worksheet.clas.abap @@ -2328,7 +2328,8 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. IF sy-subrc EQ 0. CASE ls_style_conv-abap_type. WHEN cl_abap_typedescr=>typekind_date. - = zcl_excel_common=>excel_string_to_date( -cell_value ). + = zcl_excel_common=>excel_string_to_date( ip_value = -cell_value + ip_exact = abap_true ). WHEN cl_abap_typedescr=>typekind_time. = zcl_excel_common=>excel_string_to_time( -cell_value ). ENDCASE. diff --git a/src/zcl_excel_writer_2007.clas.abap b/src/zcl_excel_writer_2007.clas.abap index c08567b..958e639 100644 --- a/src/zcl_excel_writer_2007.clas.abap +++ b/src/zcl_excel_writer_2007.clas.abap @@ -145,8 +145,10 @@ CLASS zcl_excel_writer_2007 DEFINITION METHODS create_xl_sheet_rels IMPORTING !io_worksheet TYPE REF TO zcl_excel_worksheet - !iv_drawing_index TYPE i - !iv_comment_index TYPE i + !iv_drawing_index TYPE i OPTIONAL + !iv_comment_index TYPE i OPTIONAL + !iv_cmnt_vmlindex TYPE i OPTIONAL + !iv_hdft_vmlindex TYPE i OPTIONAL RETURNING VALUE(ep_content) TYPE xstring . METHODS create_xl_styles @@ -323,10 +325,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lo_iterator TYPE REF TO zcl_excel_collection_iterator, lo_nested_iterator TYPE REF TO zcl_excel_collection_iterator, lo_table TYPE REF TO zcl_excel_table, - lo_drawing TYPE REF TO zcl_excel_drawing, - lo_drawings TYPE REF TO zcl_excel_drawings, - lo_comment TYPE REF TO zcl_excel_comment, " (+) Issue #180 - lo_comments TYPE REF TO zcl_excel_comments. " (+) Issue #180 + lo_drawing TYPE REF TO zcl_excel_drawing. DATA: lv_content TYPE xstring, lv_active TYPE flag, @@ -339,8 +338,13 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lv_index_str TYPE string, lv_value TYPE string, lv_sheet_index TYPE i, + lv_drawing_counter TYPE i, + lv_comment_counter TYPE i, + lv_vml_counter TYPE i, lv_drawing_index TYPE i, - lv_comment_index TYPE i. " (+) Issue #180 + lv_comment_index TYPE i, " (+) Issue #180 + lv_cmnt_vmlindex TYPE i, + lv_hdft_vmlindex TYPE i. ********************************************************************** @@ -433,37 +437,44 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. * Begin - Add - Issue #180 * Add comments ********************************** - lo_comments = lo_worksheet->get_comments( ). - IF lo_comments->is_empty( ) = abap_false. - lv_comment_index = lv_comment_index + 1. - + IF lo_worksheet->get_comments( )->is_empty( ) = abap_false. " Create comment itself - lv_content = me->create_xl_comments( lo_worksheet ). - lv_xl_comment = me->c_xl_comments. + ADD 1 TO lv_comment_counter. + lv_comment_index = lv_comment_counter. lv_index_str = lv_comment_index. CONDENSE lv_index_str NO-GAPS. + + lv_content = me->create_xl_comments( lo_worksheet ). + lv_xl_comment = me->c_xl_comments. REPLACE ALL OCCURRENCES OF '#' IN lv_xl_comment WITH lv_index_str. lo_zip->add( name = lv_xl_comment content = lv_content ). " Create vmlDrawing that will host the comment + ADD 1 TO lv_vml_counter. + lv_cmnt_vmlindex = lv_vml_counter. + lv_index_str = lv_cmnt_vmlindex. + CONDENSE lv_index_str NO-GAPS. + lv_content = me->create_xl_drawing_for_comments( lo_worksheet ). lv_xl_drawing_for_comment = me->cl_xl_drawing_for_comments. REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing_for_comment WITH lv_index_str. lo_zip->add( name = lv_xl_drawing_for_comment content = lv_content ). + ELSE. + CLEAR: lv_comment_index, lv_cmnt_vmlindex. ENDIF. * End - Add - Issue #180 * Add drawings ********************************** - lo_drawings = lo_worksheet->get_drawings( ). - IF lo_drawings->is_empty( ) = abap_false. - lv_drawing_index = lv_drawing_index + 1. + IF lo_worksheet->get_drawings( )->is_empty( ) = abap_false. + ADD 1 TO lv_drawing_counter. + lv_drawing_index = lv_drawing_counter. + lv_index_str = lv_drawing_index. + CONDENSE lv_index_str NO-GAPS. lv_content = me->create_xl_drawings( lo_worksheet ). lv_xl_drawing = me->c_xl_drawings. - lv_index_str = lv_drawing_index. - CONDENSE lv_index_str NO-GAPS. REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing WITH lv_index_str. lo_zip->add( name = lv_xl_drawing content = lv_content ). @@ -473,15 +484,15 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing_rels WITH lv_index_str. lo_zip->add( name = lv_xl_drawing_rels content = lv_content ). + ELSE. + CLEAR lv_drawing_index. ENDIF. * Add Header/Footer image - DATA: lt_drawings TYPE zexcel_t_drawings. - lt_drawings = lo_worksheet->get_header_footer_drawings( ). - IF lines( lt_drawings ) > 0. "Header or footer image exist - - lv_comment_index = lv_comment_index + 1. - lv_index_str = lv_comment_index. + IF lines( lo_worksheet->get_header_footer_drawings( ) ) > 0. "Header or footer image exist + ADD 1 TO lv_vml_counter. + lv_hdft_vmlindex = lv_vml_counter. + lv_index_str = lv_hdft_vmlindex. CONDENSE lv_index_str NO-GAPS. " Create vmlDrawing that will host the image @@ -497,13 +508,17 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. REPLACE ALL OCCURRENCES OF '#' IN lv_xl_drawing_rels WITH lv_index_str. lo_zip->add( name = lv_xl_drawing_rels content = lv_content ). + ELSE. + CLEAR lv_hdft_vmlindex. ENDIF. lv_xl_sheet_rels = me->c_xl_sheet_rels. lv_content = me->create_xl_sheet_rels( io_worksheet = lo_worksheet iv_drawing_index = lv_drawing_index - iv_comment_index = lv_comment_index ). " (+) Issue #180 + iv_comment_index = lv_comment_index " (+) Issue #180 + iv_cmnt_vmlindex = lv_cmnt_vmlindex + iv_hdft_vmlindex = lv_hdft_vmlindex ). lv_index_str = lv_sheet_index. CONDENSE lv_index_str NO-GAPS. @@ -3124,6 +3139,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lc_xml_attr_val_none TYPE string VALUE 'none', lc_xml_attr_val_msodir TYPE string VALUE 'mso-direction-alt:auto', lc_xml_attr_val_note TYPE string VALUE 'Note'. + CONSTANTS lc_anchor_init TYPE string VALUE '2, 15, 11, 10, &right_column&, 31, &bottom_row&, 9'. DATA: lo_document TYPE REF TO if_ixml_document, @@ -3160,6 +3176,11 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lv_int_value TYPE i, lv_int_value_string TYPE string. DATA: lv_rel_id TYPE i. + DATA lv_anchor TYPE string. + DATA lv_bottom_row TYPE i. + DATA lv_right_column TYPE i. + DATA lv_bottom_row_str TYPE string. + DATA lv_right_column_str TYPE string. ********************************************************************** @@ -3282,7 +3303,26 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lo_element_clientdata->append_child( new_child = lo_element_sizewithcells ). lo_element_anchor = lo_document->create_simple_element( name = lc_xml_node_anchor parent = lo_document ). - lo_element_anchor->set_value( '2, 15, 11, 10, 4, 31, 15, 9' ). + + " Anchor represents 4 pairs of numbers: + " ( left column, left offset ), ( top row, top offset ), + " ( right column, right offset ), ( bottom row, botton offset ) + " Offsets are a number of pixels. + " Reference: Anchor Class at + " https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.vml.spreadsheet.anchor?view=openxml-3.0.1 + lv_right_column = lo_comment->get_right_column( ). + lv_bottom_row = lo_comment->get_bottom_row( ). + + lv_right_column_str = lv_right_column. + CONDENSE lv_right_column_str. + lv_bottom_row_str = lv_bottom_row. + CONDENSE lv_bottom_row_str. + + lv_anchor = lc_anchor_init. + REPLACE '&right_column&' WITH lv_right_column_str INTO lv_anchor. + REPLACE '&bottom_row&' WITH lv_bottom_row_str INTO lv_anchor. + lo_element_anchor->set_value( lv_anchor ). + lo_element_clientdata->append_child( new_child = lo_element_anchor ). lo_element_autofill = lo_document->create_simple_element( name = lc_xml_node_autofill parent = lo_document ). @@ -3990,8 +4030,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. DATA: lv_value TYPE string, lv_relation_id TYPE i, - lv_index_str TYPE string, - lv_comment_index TYPE i. + lv_index_str TYPE string. ********************************************************************** * STEP 1: Create [Content_Types].xml into the root of the ZIP @@ -4035,10 +4074,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. ENDWHILE. * drawing - DATA: lo_drawings TYPE REF TO zcl_excel_drawings. - - lo_drawings = io_worksheet->get_drawings( ). - IF lo_drawings->is_empty( ) = abap_false. + IF iv_drawing_index > 0. lo_element = lo_document->create_simple_element( name = lc_xml_node_relationship parent = lo_document ). ADD 1 TO lv_relation_id. @@ -4062,18 +4098,11 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. ENDIF. * Begin - Add - Issue #180 - DATA: lo_comments TYPE REF TO zcl_excel_comments. - - lv_comment_index = iv_comment_index. - - lo_comments = io_worksheet->get_comments( ). - IF lo_comments->is_empty( ) = abap_false. + IF iv_cmnt_vmlindex > 0 AND iv_comment_index > 0. " Drawing for comment lo_element = lo_document->create_simple_element( name = lc_xml_node_relationship parent = lo_document ). - ADD 1 TO lv_relation_id. - ADD 1 TO lv_comment_index. lv_value = lv_relation_id. CONDENSE lv_value. @@ -4083,7 +4112,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lo_element->set_attribute_ns( name = lc_xml_attr_type value = lc_xml_node_rid_drawing_cmt_tp ). - lv_index_str = iv_comment_index. + lv_index_str = iv_cmnt_vmlindex. CONDENSE lv_index_str NO-GAPS. lv_value = me->cl_xl_drawing_for_comments. REPLACE 'xl' WITH '..' INTO lv_value. @@ -4118,13 +4147,12 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. ********************************************************************** * header footer image - DATA: lt_drawings TYPE zexcel_t_drawings. - lt_drawings = io_worksheet->get_header_footer_drawings( ). - IF lines( lt_drawings ) > 0. "Header or footer image exist - ADD 1 TO lv_relation_id. + IF iv_hdft_vmlindex > 0. "Header or footer image exist " Drawing for comment/header/footer lo_element = lo_document->create_simple_element( name = lc_xml_node_relationship parent = lo_document ). + ADD 1 TO lv_relation_id. + lv_value = lv_relation_id. CONDENSE lv_value. CONCATENATE 'rId' lv_value INTO lv_value. @@ -4133,7 +4161,7 @@ CLASS zcl_excel_writer_2007 IMPLEMENTATION. lo_element->set_attribute_ns( name = lc_xml_attr_type value = lc_xml_node_rid_drawing_cmt_tp ). - lv_index_str = lv_comment_index. + lv_index_str = iv_hdft_vmlindex. CONDENSE lv_index_str NO-GAPS. lv_value = me->cl_xl_drawing_for_comments. REPLACE 'xl' WITH '..' INTO lv_value. diff --git a/src/zcl_excel_writer_2007.clas.locals_imp.abap b/src/zcl_excel_writer_2007.clas.locals_imp.abap index 0ae8e20..ae9def7 100644 --- a/src/zcl_excel_writer_2007.clas.locals_imp.abap +++ b/src/zcl_excel_writer_2007.clas.locals_imp.abap @@ -1730,7 +1730,7 @@ CLASS lcl_create_xl_sheet IMPLEMENTATION. value = lv_value ). o_element_root->append_child( new_child = lo_element ). - ADD 1 TO v_relation_id. " +1 for comments (not referenced in XL sheet but let's reserve the rId) + ADD 1 TO v_relation_id. "for xl/comments.xml (not referenced in XL sheet but counted in sheet rels) ENDIF. * End - Add - Issue #180 ENDMETHOD. @@ -1753,7 +1753,6 @@ CLASS lcl_create_xl_sheet IMPLEMENTATION. lo_element->set_attribute( name = 'r:id' value = lv_value ). o_element_root->append_child( new_child = lo_element ). - ADD 1 TO v_relation_id. " +1 for comments (not referenced in XL sheet but let's reserve the rId) ENDIF. * ENDMETHOD.