From 37ad2fd68aa59a25761dbd0b4f9d8420711fcc13 Mon Sep 17 00:00:00 2001 From: Domi Bigl Date: Tue, 21 Jul 2020 22:22:20 +0200 Subject: [PATCH] improve handling of multiple CF rules and ranges (#682) --- src/zcl_excel_reader_2007.clas.abap | 17 ++++++------- src/zcl_excel_style_cond.clas.abap | 9 ++++--- src/zcl_excel_worksheet.clas.abap | 6 +++-- src/zcl_excel_writer_2007.clas.abap | 39 ++++++++++++++++++++++------- src/zdemo_excel15.prog.abap | 4 +++ 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/zcl_excel_reader_2007.clas.abap b/src/zcl_excel_reader_2007.clas.abap index b53393b..04d8eab 100644 --- a/src/zcl_excel_reader_2007.clas.abap +++ b/src/zcl_excel_reader_2007.clas.abap @@ -2912,11 +2912,10 @@ METHOD load_worksheet_cond_format. lo_ixml_rule, lo_style_cond. - *--------------------------------------------------------------------* * Get type of rule *--------------------------------------------------------------------* - lo_ixml_rules = io_ixml_worksheet->get_elements_by_tag_name( name = 'cfRule' ). + lo_ixml_rules = lo_ixml_cond_format->get_elements_by_tag_name( name = 'cfRule' ). lo_ixml_iterator2 = lo_ixml_rules->create_iterator( ). lo_ixml_rule ?= lo_ixml_iterator2->get_next( ). @@ -2930,37 +2929,37 @@ METHOD load_worksheet_cond_format. CASE lv_rule. WHEN zcl_excel_style_cond=>c_rule_cellis. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_ci( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_databar. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_db( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_expression. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_ex( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_iconset. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_is( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_colorscale. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_cs( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_top10. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_t10( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN zcl_excel_style_cond=>c_rule_above_average. - lo_style_cond = io_worksheet->add_new_style_cond( ). + lo_style_cond = io_worksheet->add_new_style_cond( '' ). load_worksheet_cond_format_aa( io_ixml_rule = lo_ixml_rule io_style_cond = lo_style_cond ). WHEN OTHERS. diff --git a/src/zcl_excel_style_cond.clas.abap b/src/zcl_excel_style_cond.clas.abap index 0fbe31a..01edada 100644 --- a/src/zcl_excel_style_cond.clas.abap +++ b/src/zcl_excel_style_cond.clas.abap @@ -64,9 +64,10 @@ public section. data PRIORITY type ZEXCEL_STYLE_PRIORITY value 1. "#EC NOTEXT . . . . . . . . . . . . . . . . . . . . " . data RULE type ZEXCEL_CONDITION_RULE . - methods CONSTRUCTOR - importing - !IP_GUID type ZEXCEL_CELL_STYLE optional . + METHODS constructor + IMPORTING + !ip_guid TYPE zexcel_cell_style OPTIONAL + !ip_dimension_range TYPE string. methods GET_DIMENSION_RANGE returning value(EP_DIMENSION_RANGE) type STRING . @@ -191,7 +192,7 @@ METHOD constructor. me->priority = 1. * inizialize dimension range - me->mv_rule_range = 'A1'. + me->mv_rule_range = ip_dimension_range. IF ip_guid IS NOT INITIAL. me->guid = ip_guid. diff --git a/src/zcl_excel_worksheet.clas.abap b/src/zcl_excel_worksheet.clas.abap index 4d6ae03..e63d1d6 100644 --- a/src/zcl_excel_worksheet.clas.abap +++ b/src/zcl_excel_worksheet.clas.abap @@ -56,8 +56,10 @@ CLASS zcl_excel_worksheet DEFINITION RETURNING VALUE(eo_column) TYPE REF TO zcl_excel_column . METHODS add_new_style_cond + IMPORTING + !ip_dimension_range TYPE string DEFAULT 'A1' RETURNING - VALUE(eo_style_cond) TYPE REF TO zcl_excel_style_cond . + VALUE(eo_style_cond) TYPE REF TO zcl_excel_style_cond. METHODS add_new_data_validation RETURNING VALUE(eo_data_validation) TYPE REF TO zcl_excel_data_validation . @@ -691,7 +693,7 @@ CLASS zcl_excel_worksheet IMPLEMENTATION. METHOD add_new_style_cond. - CREATE OBJECT eo_style_cond. + CREATE OBJECT eo_style_cond EXPORTING ip_dimension_range = ip_dimension_range. styles_cond->add( eo_style_cond ). ENDMETHOD. "ADD_NEW_STYLE_COND diff --git a/src/zcl_excel_writer_2007.clas.abap b/src/zcl_excel_writer_2007.clas.abap index 04119a7..84a1404 100644 --- a/src/zcl_excel_writer_2007.clas.abap +++ b/src/zcl_excel_writer_2007.clas.abap @@ -3644,6 +3644,12 @@ METHOD create_xl_sheet. * issue #220 - If cell in tables-area don't use default from row or column or sheet - Declarations 1 - end *--------------------------------------------------------------------* + TYPES: BEGIN OF ty_condformating_range, + dimension_range TYPE string, + condformatting_node TYPE REF TO if_ixml_element, + END OF ty_condformating_range, + ty_condformating_ranges TYPE STANDARD TABLE OF ty_condformating_range. + ** Constant node name DATA: lc_xml_node_worksheet TYPE string VALUE 'worksheet', lc_xml_node_sheetpr TYPE string VALUE 'sheetPr', @@ -3838,11 +3844,14 @@ METHOD create_xl_sheet. ls_values TYPE zexcel_s_autofilter_values, lo_autofilters TYPE REF TO zcl_excel_autofilters, lo_autofilter TYPE REF TO zcl_excel_autofilter, - lv_ref TYPE string. + lv_ref TYPE string, + lt_condformating_ranges TYPE ty_condformating_ranges, + ls_condformating_range TYPE ty_condformating_range. - FIELD-SYMBOLS: TYPE zexcel_s_cell_data, - LIKE LINE OF lt_range_merge, - LIKE LINE OF lts_row_outlines. + FIELD-SYMBOLS: TYPE zexcel_s_cell_data, + LIKE LINE OF lt_range_merge, + LIKE LINE OF lts_row_outlines, + TYPE ty_condformating_range. *--------------------------------------------------------------------* * issue #220 - If cell in tables-area don't use default from row or column or sheet - Declarations 2 - start @@ -4514,11 +4523,23 @@ METHOD create_xl_sheet. IF lo_style_cond->rule IS INITIAL. CONTINUE. ENDIF. - lo_element = lo_document->create_simple_element( name = lc_xml_node_condformatting - parent = lo_document ). - lv_value = lo_style_cond->get_dimension_range( ) . - lo_element->set_attribute_ns( name = lc_xml_attr_sqref - value = lv_value ). + + lv_value = lo_style_cond->get_dimension_range( ). + + READ TABLE lt_condformating_ranges WITH KEY dimension_range = lv_value ASSIGNING . + IF sy-subrc = 0. + lo_element = -condformatting_node. + ELSE. + lo_element = lo_document->create_simple_element( name = lc_xml_node_condformatting + parent = lo_document ). + lo_element->set_attribute_ns( name = lc_xml_attr_sqref + value = lv_value ). + + ls_condformating_range-dimension_range = lv_value. + ls_condformating_range-condformatting_node = lo_element. + INSERT ls_condformating_range INTO TABLE lt_condformating_ranges. + + ENDIF. " cfRule node lo_element_2 = lo_document->create_simple_element( name = lc_xml_node_cfrule diff --git a/src/zdemo_excel15.prog.abap b/src/zdemo_excel15.prog.abap index ec1b21a..f220c8f 100644 --- a/src/zdemo_excel15.prog.abap +++ b/src/zdemo_excel15.prog.abap @@ -72,6 +72,10 @@ INITIALIZATION. APPEND INITIAL LINE TO lt_files ASSIGNING . -input = '04_Sheets.xlsx'. APPEND INITIAL LINE TO lt_files ASSIGNING . + -input = '05_Conditional.xlsx'. + APPEND INITIAL LINE TO lt_files ASSIGNING . + -input = '07_ConditionalAll.xlsx'. + APPEND INITIAL LINE TO lt_files ASSIGNING . -input = '08_Range.xlsx'. APPEND INITIAL LINE TO lt_files ASSIGNING . -input = '13_MergedCells.xlsx'.