From 65bb19e575fcc56ab185125d05fd31c20b27dabc Mon Sep 17 00:00:00 2001 From: Tomek Mackowski Date: Mon, 11 Jul 2011 21:51:08 +0000 Subject: [PATCH] Fix #92 - Enable changing legacy color palette (to change colors on Excel ver <= 2003) git-svn-id: https://subversion.assembla.com/svn/abap2xlsx/trunk@206 b7d68dce-7c3c-4a99-8ce0-9ea847f5d049 --- ZA2X/CLAS/ZCL_EXCEL.slnk | 15 +- ZA2X/CLAS/ZCL_EXCEL_LEGACY_PALETTE.slnk | 187 +++++++++++++++++++++++ ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk | 27 +++- ZA2X/PROG/ZDEMO_EXCEL2.slnk | 14 +- ZA2X/TTYP/ZEXCEL_T_STYLE_COLOR_ARGB.slnk | 2 + 5 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 ZA2X/CLAS/ZCL_EXCEL_LEGACY_PALETTE.slnk create mode 100644 ZA2X/TTYP/ZEXCEL_T_STYLE_COLOR_ARGB.slnk diff --git a/ZA2X/CLAS/ZCL_EXCEL.slnk b/ZA2X/CLAS/ZCL_EXCEL.slnk index 3cb1be8..5a4c6fa 100644 --- a/ZA2X/CLAS/ZCL_EXCEL.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL.slnk @@ -26,6 +26,7 @@ public section. TITLE = 'abap2xlsx' . data SECURITY type ref to ZCL_EXCEL_SECURITY . + data LEGACY_PALETTE type ref to ZCL_EXCEL_LEGACY_PALETTE read-only . methods ADD_NEW_DRAWING importing @@ -96,11 +97,14 @@ protected section. *"* implementation or private method's signature *"* use this source file for any macro definitions you need *"* in the implementation part of the class - - - - - + + + + + + + + method ZIF_EXCEL_BOOK_PROTECTION~INITIALIZE. me->zif_excel_book_protection~protected = zif_excel_book_protection=>c_unprotected. @@ -189,6 +193,7 @@ endmethod. CREATE OBJECT ranges. CREATE OBJECT styles. CREATE OBJECT drawings. + CREATE OBJECT legacy_palette. me->zif_excel_book_protection~initialize( ). me->zif_excel_book_properties~initialize( ). diff --git a/ZA2X/CLAS/ZCL_EXCEL_LEGACY_PALETTE.slnk b/ZA2X/CLAS/ZCL_EXCEL_LEGACY_PALETTE.slnk new file mode 100644 index 0000000..5b43d04 --- /dev/null +++ b/ZA2X/CLAS/ZCL_EXCEL_LEGACY_PALETTE.slnk @@ -0,0 +1,187 @@ + + + class ZCL_EXCEL_LEGACY_PALETTE definition + public + create public . + +public section. +*"* public components of class ZCL_EXCEL_LEGACY_PALETTE +*"* do not include other source files here!!! + type-pools ABAP . + + methods CONSTRUCTOR . + methods IS_MODIFIED + returning + value(EP_MODIFIED) type ABAP_BOOL . + methods GET_COLOR + importing + !IP_INDEX type I + returning + value(EP_COLOR) type ZEXCEL_STYLE_COLOR_ARGB + raising + ZCX_EXCEL . + methods GET_COLORS + returning + value(EP_COLORS) type ZEXCEL_T_STYLE_COLOR_ARGB . + methods SET_COLOR + importing + !IP_INDEX type I + !IP_COLOR type ZEXCEL_STYLE_COLOR_ARGB . + protected section. +*"* protected components of class ZCL_EXCEL_LEGACY_PALETTE +*"* do not include other source files here!!! + private section. +*"* private components of class ZCL_EXCEL_LEGACY_PALETTE +*"* do not include other source files here!!! + + data MODIFIED type ABAP_BOOL value ABAP_FALSE. "#EC NOTEXT . + data COLORS type ZEXCEL_T_STYLE_COLOR_ARGB . + *"* use this source file for the definition and implementation of +*"* local helper classes, interface definitions and type +*"* declarations + *"* use this source file for any type of declarations (class +*"* definitions, interfaces or type declarations) you need for +*"* components in the private section + *"* use this source file for any macro definitions you need +*"* in the implementation part of the class + + + + ABAP + + + + METHOD constructor. + " default Excel palette based on + " http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.indexedcolors.aspx + + APPEND '00000000' TO colors. + APPEND '00FFFFFF' TO colors. + APPEND '00FF0000' TO colors. + APPEND '0000FF00' TO colors. + APPEND '000000FF' TO colors. + APPEND '00FFFF00' TO colors. + APPEND '00FF00FF' TO colors. + APPEND '0000FFFF' TO colors. + APPEND '00000000' TO colors. + APPEND '00FFFFFF' TO colors. + + APPEND '00FF0000' TO colors. + APPEND '0000FF00' TO colors. + APPEND '000000FF' TO colors. + APPEND '00FFFF00' TO colors. + APPEND '00FF00FF' TO colors. + APPEND '0000FFFF' TO colors. + APPEND '00800000' TO colors. + APPEND '00008000' TO colors. + APPEND '00000080' TO colors. + APPEND '00808000' TO colors. + + APPEND '00800080' TO colors. + APPEND '00008080' TO colors. + APPEND '00C0C0C0' TO colors. + APPEND '00808080' TO colors. + APPEND '009999FF' TO colors. + APPEND '00993366' TO colors. + APPEND '00FFFFCC' TO colors. + APPEND '00CCFFFF' TO colors. + APPEND '00660066' TO colors. + APPEND '00FF8080' TO colors. + + APPEND '000066CC' TO colors. + APPEND '00CCCCFF' TO colors. + APPEND '00000080' TO colors. + APPEND '00FF00FF' TO colors. + APPEND '00FFFF00' TO colors. + APPEND '0000FFFF' TO colors. + APPEND '00800080' TO colors. + APPEND '00800000' TO colors. + APPEND '00008080' TO colors. + APPEND '000000FF' TO colors. + + APPEND '0000CCFF' TO colors. + APPEND '00CCFFFF' TO colors. + APPEND '00CCFFCC' TO colors. + APPEND '00FFFF99' TO colors. + APPEND '0099CCFF' TO colors. + APPEND '00FF99CC' TO colors. + APPEND '00CC99FF' TO colors. + APPEND '00FFCC99' TO colors. + APPEND '003366FF' TO colors. + APPEND '0033CCCC' TO colors. + + APPEND '0099CC00' TO colors. + APPEND '00FFCC00' TO colors. + APPEND '00FF9900' TO colors. + APPEND '00FF6600' TO colors. + APPEND '00666699' TO colors. + APPEND '00969696' TO colors. + APPEND '00003366' TO colors. + APPEND '00339966' TO colors. + APPEND '00003300' TO colors. + APPEND '00333300' TO colors. + + APPEND '00993300' TO colors. + APPEND '00993366' TO colors. + APPEND '00333399' TO colors. + APPEND '00333333' TO colors. + +ENDMETHOD. + + + + + + + METHOD get_color. + DATA: lv_index type i. + + lv_index = ip_index + 1. + READ TABLE colors INTO ep_color INDEX lv_index. + IF sy-subrc <> 0. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = 'Invalid color index'. + ENDIF. +ENDMETHOD. + + + + + METHOD get_colors. + ep_colors = colors. +ENDMETHOD. + + + + + METHOD is_modified. + ep_modified = modified. +ENDMETHOD. + + + + + + METHOD set_color. + DATA: lv_index TYPE i. + + FIELD-SYMBOLS: <lv_color> LIKE LINE OF colors. + + lv_index = ip_index + 1. + READ TABLE colors ASSIGNING <lv_color> INDEX lv_index. + IF sy-subrc <> 0. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = 'Invalid color index'. + ENDIF. + + IF <lv_color> <> ip_color. + modified = abap_true. + <lv_color> = ip_color. + ENDIF. + +ENDMETHOD. + + + diff --git a/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk b/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk index 744252d..9c37cc9 100644 --- a/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk +++ b/ZA2X/CLAS/ZCL_EXCEL_WRITER_2007.slnk @@ -3037,6 +3037,8 @@ endmethod. lc_xml_node_tablestyles TYPE string VALUE 'tableStyles', " Colors lc_xml_node_colors TYPE string VALUE 'colors', + lc_xml_node_indexedcolors TYPE string VALUE 'indexedColors', + lc_xml_node_rgbcolor TYPE string VALUE 'rgbColor', lc_xml_node_mrucolors TYPE string VALUE 'mruColors', " Alignment lc_xml_node_alignment TYPE string VALUE 'alignment', @@ -3125,7 +3127,9 @@ endmethod. ls_styles_mapping TYPE zexcel_s_styles_mapping, ls_style_cond_mapping TYPE zexcel_s_styles_cond_mapping, ls_cellis TYPE zexcel_conditional_cellis, - ls_expression TYPE zexcel_conditional_expression. + ls_expression TYPE zexcel_conditional_expression, + lt_colors TYPE zexcel_t_style_color_argb, + ls_color LIKE LINE OF lt_colors. DATA: lv_value TYPE string, lv_dfx_count TYPE i, @@ -3859,6 +3863,27 @@ endmethod. value = zcl_excel_table=>builtinstyle_pivot_light16 ). lo_element_root->append_child( new_child = lo_element ). + "write legacy color palette in case any indexed color was changed + IF excel->legacy_palette->is_modified( ) = abap_true. + lo_element = lo_document->create_simple_element( name = lc_xml_node_colors + parent = lo_document ). + lo_sub_element = lo_document->create_simple_element( name = lc_xml_node_indexedcolors + parent = lo_document ). + lo_element->append_child( new_child = lo_sub_element ). + + lt_colors = excel->legacy_palette->get_colors( ). + LOOP AT lt_colors INTO ls_color. + lo_sub_element_2 = lo_document->create_simple_element( name = lc_xml_node_rgbcolor + parent = lo_document ). + lv_value = ls_color. + lo_sub_element_2->set_attribute_ns( name = lc_xml_attr_rgb + value = lv_value ). + lo_sub_element->append_child( new_child = lo_sub_element_2 ). + ENDLOOP. + + lo_element_root->append_child( new_child = lo_element ). + ENDIF. + ********************************************************************** * STEP 5: Create xstring stream lo_streamfactory = lo_ixml->create_stream_factory( ). diff --git a/ZA2X/PROG/ZDEMO_EXCEL2.slnk b/ZA2X/PROG/ZDEMO_EXCEL2.slnk index 9af041b..da913b1 100644 --- a/ZA2X/PROG/ZDEMO_EXCEL2.slnk +++ b/ZA2X/PROG/ZDEMO_EXCEL2.slnk @@ -32,7 +32,8 @@ DATA: lv_style_bold_guid TYPE zexcel_cell_style, lv_style_filled_guid TYPE zexcel_cell_style, lv_style_filled_green_guid TYPE zexcel_cell_style, lv_style_border_guid TYPE zexcel_cell_style, - lv_style_button_guid TYPE zexcel_cell_style. + lv_style_button_guid TYPE zexcel_cell_style, + lv_style_filled_turquoise_guid TYPE zexcel_cell_style. DATA: lv_file TYPE xstring, lv_bytecount TYPE i, @@ -117,6 +118,16 @@ START-OF-SELECTION. lo_style_filled->font->scheme = zcl_excel_style_font=>c_scheme_major. lv_style_filled_green_guid = lo_style_filled->get_guid( ). + " Create filled style turquoise using legacy excel ver <= 2003 palette. (https://code.sdn.sap.com/spaces/abap2xlsx/tickets/92) + lo_style_filled = lo_excel->add_new_style( ). + lo_excel->legacy_palette->set_color( "replace built-in color from palette with out custom RGB turquoise + ip_index = 16 + ip_color = '0040E0D0' ). + + lo_style_filled->fill->filltype = zcl_excel_style_fill=>c_fill_solid. + lo_style_filled->fill->fgcolor-indexed = 16. + lv_style_filled_turquoise_guid = lo_style_filled->get_guid( ). + " Get active sheet lo_worksheet = lo_excel->get_active_worksheet( ). lo_worksheet->set_title( ip_title = 'Styles' ). @@ -126,6 +137,7 @@ START-OF-SELECTION. lo_worksheet->set_cell( ip_column = 'B' ip_row = 5 ip_value = 'Filled text' ip_style = lv_style_filled_guid ). lo_worksheet->set_cell( ip_column = 'C' ip_row = 6 ip_value = 'Borders' ip_style = lv_style_border_guid ). lo_worksheet->set_cell( ip_column = 'D' ip_row = 7 ip_value = 'I''m not a button :)' ip_style = lv_style_button_guid ). + lo_worksheet->set_cell( ip_column = 'B' ip_row = 9 ip_value = 'Modified color for Excel 2003' ip_style = lv_style_filled_turquoise_guid ). " Fill the cell and apply one style lo_worksheet->set_cell( ip_column = 'B' ip_row = 6 ip_value = 'Filled text' ip_style = lv_style_filled_guid ). " Change the style diff --git a/ZA2X/TTYP/ZEXCEL_T_STYLE_COLOR_ARGB.slnk b/ZA2X/TTYP/ZEXCEL_T_STYLE_COLOR_ARGB.slnk new file mode 100644 index 0000000..ddc08f4 --- /dev/null +++ b/ZA2X/TTYP/ZEXCEL_T_STYLE_COLOR_ARGB.slnk @@ -0,0 +1,2 @@ + +