abap2xlsx/ZA2X/CLAS/ZCL_EXCEL_WRITER_XLSM.slnk
2015-08-03 20:40:04 +02:00

309 lines
16 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<CLAS CLSNAME="ZCL_EXCEL_WRITER_XLSM" VERSION="1" LANGU="E" DESCRIPT="Excel with macro writer" CATEGORY="00" EXPOSURE="2" STATE="1" RELEASE="0" CLSCCINCL="X" FIXPT="X" UNICODE="X" CLSBCCAT="00" DURATION_TYPE="0 " RISK_LEVEL="0 " ZSAPLINK_PLUGIN_MAJOR_VERSION="0 " ZSAPLINK_PLUGIN_MINOR_VERSION="1 " ZSAPLINK_PLUGIN_BUILD_VERSION="0 " ZSAPLINK_PLUGIN_INFO1="ZSAPLINK_CLASS is part of the main ZSAPLINK project --&gt; This plugin found there instead of ZSAPLINK_PLUGINS projects" ZSAPLINK_PLUGIN_INFO2="SAPLINK homepage: https://www.assembla.com/spaces/saplink/wiki" ZSAPLINK_PLUGIN_INFO3="Download from https://www.assembla.com/code/saplink/subversion/nodes" ZSAPLINK_PLUGIN_INFO4="and navigate to: trunk -&gt; core -&gt; ZSAPLINK -&gt; CLAS -&gt; ZSAPLINK_CLASS.slnk" REFCLSNAME="ZCL_EXCEL_WRITER_2007">
<localImplementation>*&quot;* use this source file for the definition and implementation of
*&quot;* local helper classes, interface definitions and type
*&quot;* declarations</localImplementation>
<localTypes>*&quot;* use this source file for any type of declarations (class
*&quot;* definitions, interfaces or type declarations) you need for
*&quot;* components in the private section</localTypes>
<localMacros>*&quot;* use this source file for any macro definitions you need
*&quot;* in the implementation part of the class</localMacros>
<attribute CLSNAME="ZCL_EXCEL_WRITER_XLSM" CMPNAME="C_XL_VBAPROJECT" VERSION="1" LANGU="E" EXPOSURE="1" STATE="1" EDITORDER="1 " ATTDECLTYP="2" ATTVALUE="&apos;xl/vbaProject.bin&apos;" ATTEXPVIRT="0" TYPTYPE="1" TYPE="STRING" SRCROW1="0 " SRCCOLUMN1="0 " SRCROW2="0 " SRCCOLUMN2="0 " TYPESRC_LENG="0 "/>
<inheritance CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" STATE="1">
<redefinition CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" MTDNAME="ADD_FURTHER_DATA_TO_ZIP" EXPOSURE="1"/>
<redefinition CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" MTDNAME="CREATE_CONTENT_TYPES" EXPOSURE="1"/>
<redefinition CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" MTDNAME="CREATE_XL_RELATIONSHIPS" EXPOSURE="1"/>
<redefinition CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" MTDNAME="CREATE_XL_SHEET" EXPOSURE="1"/>
<redefinition CLSNAME="ZCL_EXCEL_WRITER_XLSM" REFCLSNAME="ZCL_EXCEL_WRITER_2007" VERSION="1" MTDNAME="CREATE_XL_WORKBOOK" EXPOSURE="1"/>
</inheritance>
<method CLSNAME="ZCL_EXCEL_WRITER_2007" CMPNAME="ADD_FURTHER_DATA_TO_ZIP" VERSION="0" EXPOSURE="0" STATE="0" EDITORDER="0 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
<source>METHOD add_further_data_to_zip.
super-&gt;add_further_data_to_zip( io_zip = io_zip ).
* Add vbaProject.bin to zip
io_zip-&gt;add( name = me-&gt;c_xl_vbaproject
content = me-&gt;excel-&gt;zif_excel_book_vba_project~vbaproject ).
ENDMETHOD.</source>
</method>
<method CLSNAME="ZCL_EXCEL_WRITER_2007" CMPNAME="CREATE_CONTENT_TYPES" VERSION="0" EXPOSURE="0" STATE="0" EDITORDER="0 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
<source>METHOD create_content_types.
** Constant node name
DATA: lc_xml_node_workb_ct TYPE string VALUE &apos;application/vnd.ms-excel.sheet.macroEnabled.main+xml&apos;,
lc_xml_node_default TYPE string VALUE &apos;Default&apos;,
&quot; Node attributes
lc_xml_attr_partname TYPE string VALUE &apos;PartName&apos;,
lc_xml_attr_extension TYPE string VALUE &apos;Extension&apos;,
lc_xml_attr_contenttype TYPE string VALUE &apos;ContentType&apos;,
lc_xml_attr_codename TYPE string VALUE &apos;codeName&apos;,
lc_xml_node_workb_pn TYPE string VALUE &apos;/xl/workbook.xml&apos;,
lc_xml_node_bin_ext TYPE string VALUE &apos;bin&apos;,
lc_xml_node_bin_ct TYPE string VALUE &apos;application/vnd.ms-office.vbaProject&apos;.
DATA: lo_ixml TYPE REF TO if_ixml,
lo_document TYPE REF TO if_ixml_document,
lo_document_xml TYPE REF TO cl_xml_document,
lo_element_root TYPE REF TO if_ixml_node,
lo_element TYPE REF TO if_ixml_element,
lo_collection TYPE REF TO if_ixml_node_collection,
lo_iterator TYPE REF TO if_ixml_node_iterator,
lo_node TYPE REF TO if_ixml_node,
lo_encoding TYPE REF TO if_ixml_encoding,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_ostream TYPE REF TO if_ixml_ostream,
lo_renderer TYPE REF TO if_ixml_renderer.
DATA: lv_subrc TYPE sysubrc,
lv_contenttype TYPE string,
lv_syindex(2) TYPE c.
**********************************************************************
* STEP 3: Create standard contentType
ep_content = super-&gt;create_content_types( ).
**********************************************************************
* STEP 2: modify XML adding the extension bin definition
CREATE OBJECT lo_document_xml.
lv_subrc = lo_document_xml-&gt;parse_xstring( ep_content ).
lo_document ?= lo_document_xml-&gt;m_document.
lo_element_root = lo_document-&gt;if_ixml_node~get_first_child( ).
&quot; extension node
lo_element = lo_document-&gt;create_simple_element( name = lc_xml_node_default
parent = lo_document ).
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_extension
value = lc_xml_node_bin_ext ).
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_contenttype
value = lc_xml_node_bin_ct ).
lo_element_root-&gt;append_child( new_child = lo_element ).
**********************************************************************
* STEP 3: modify XML changing the contentType of node Override /xl/workbook.xml
lo_collection = lo_document-&gt;get_elements_by_tag_name( &apos;Override&apos; ).
lo_iterator = lo_collection-&gt;create_iterator( ).
lo_element ?= lo_iterator-&gt;get_next( ).
WHILE lo_element IS BOUND.
lv_contenttype = lo_element-&gt;get_attribute_ns( lc_xml_attr_partname ).
IF lv_contenttype EQ lc_xml_node_workb_pn.
lo_element-&gt;remove_attribute_ns( lc_xml_attr_contenttype ).
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_contenttype
value = lc_xml_node_workb_ct ).
EXIT.
ENDIF.
lo_element ?= lo_iterator-&gt;get_next( ).
ENDWHILE.
**********************************************************************
* STEP 3: Create xstring stream
CLEAR ep_content.
lo_ixml = cl_ixml=&gt;create( ).
lo_streamfactory = lo_ixml-&gt;create_stream_factory( ).
lo_ostream = lo_streamfactory-&gt;create_ostream_xstring( string = ep_content ).
lo_renderer = lo_ixml-&gt;create_renderer( ostream = lo_ostream document = lo_document ).
lo_renderer-&gt;render( ).
ENDMETHOD.</source>
</method>
<method CLSNAME="ZCL_EXCEL_WRITER_2007" CMPNAME="CREATE_XL_RELATIONSHIPS" VERSION="0" EXPOSURE="0" STATE="0" EDITORDER="0 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
<source>METHOD create_xl_relationships.
** Constant node name
DATA: lc_xml_node_relationships TYPE string VALUE &apos;Relationships&apos;,
lc_xml_node_relationship TYPE string VALUE &apos;Relationship&apos;,
&quot; Node attributes
lc_xml_attr_id TYPE string VALUE &apos;Id&apos;,
lc_xml_attr_type TYPE string VALUE &apos;Type&apos;,
lc_xml_attr_target TYPE string VALUE &apos;Target&apos;,
&quot; Node id
lc_xml_node_ridx_id TYPE string VALUE &apos;rId#&apos;,
&quot; Node type
lc_xml_node_rid_vba_tp TYPE string VALUE &apos;http://schemas.microsoft.com/office/2006/relationships/vbaProject&apos;,
&quot; Node target
lc_xml_node_rid_vba_tg TYPE string VALUE &apos;vbaProject.bin&apos;.
DATA: lo_ixml TYPE REF TO if_ixml,
lo_document TYPE REF TO if_ixml_document,
lo_document_xml TYPE REF TO cl_xml_document,
lo_element_root TYPE REF TO if_ixml_node,
lo_element TYPE REF TO if_ixml_element,
lo_node TYPE REF TO if_ixml_node,
lo_encoding TYPE REF TO if_ixml_encoding,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_ostream TYPE REF TO if_ixml_ostream,
lo_renderer TYPE REF TO if_ixml_renderer.
DATA: lv_xml_node_ridx_tg TYPE string,
lv_xml_node_ridx_id TYPE string,
lv_size TYPE i,
lv_subrc TYPE sysubrc,
lv_syindex(2) TYPE c.
**********************************************************************
* STEP 3: Create standard relationship
ep_content = super-&gt;create_xl_relationships( ).
**********************************************************************
* STEP 2: modify XML adding the vbaProject relation
CREATE OBJECT lo_document_xml.
lv_subrc = lo_document_xml-&gt;parse_xstring( ep_content ).
lo_document ?= lo_document_xml-&gt;m_document.
lo_element_root = lo_document-&gt;if_ixml_node~get_first_child( ).
lv_size = excel-&gt;get_worksheets_size( ).
&quot; Relationship node
lo_element = lo_document-&gt;create_simple_element( name = lc_xml_node_relationship
parent = lo_document ).
ADD 4 TO lv_size.
lv_syindex = lv_size.
SHIFT lv_syindex RIGHT DELETING TRAILING space.
SHIFT lv_syindex LEFT DELETING LEADING space.
lv_xml_node_ridx_id = lc_xml_node_ridx_id.
REPLACE ALL OCCURRENCES OF &apos;#&apos; IN lv_xml_node_ridx_id WITH lv_syindex.
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_id
value = lv_xml_node_ridx_id ).
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_type
value = lc_xml_node_rid_vba_tp ).
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_target
value = lc_xml_node_rid_vba_tg ).
lo_element_root-&gt;append_child( new_child = lo_element ).
**********************************************************************
* STEP 3: Create xstring stream
CLEAR ep_content.
lo_ixml = cl_ixml=&gt;create( ).
lo_streamfactory = lo_ixml-&gt;create_stream_factory( ).
lo_ostream = lo_streamfactory-&gt;create_ostream_xstring( string = ep_content ).
lo_renderer = lo_ixml-&gt;create_renderer( ostream = lo_ostream document = lo_document ).
lo_renderer-&gt;render( ).
ENDMETHOD.</source>
</method>
<method CLSNAME="ZCL_EXCEL_WRITER_2007" CMPNAME="CREATE_XL_SHEET" VERSION="0" EXPOSURE="0" STATE="0" EDITORDER="0 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
<source>METHOD create_xl_sheet.
** Constant node name
DATA: lc_xml_attr_codename TYPE string VALUE &apos;codeName&apos;.
DATA: lo_ixml TYPE REF TO if_ixml,
lo_document TYPE REF TO if_ixml_document,
lo_document_xml TYPE REF TO cl_xml_document,
lo_element_root TYPE REF TO if_ixml_node,
lo_element TYPE REF TO if_ixml_element,
lo_collection TYPE REF TO if_ixml_node_collection,
lo_iterator TYPE REF TO if_ixml_node_iterator,
lo_node TYPE REF TO if_ixml_node,
lo_encoding TYPE REF TO if_ixml_encoding,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_ostream TYPE REF TO if_ixml_ostream,
lo_renderer TYPE REF TO if_ixml_renderer.
DATA: lv_subrc TYPE sysubrc,
lv_syindex(2) TYPE c.
**********************************************************************
* STEP 3: Create standard relationship
ep_content = super-&gt;create_xl_sheet( io_worksheet = io_worksheet
iv_active = iv_active ).
**********************************************************************
* STEP 2: modify XML adding the vbaProject relation
CREATE OBJECT lo_document_xml.
lv_subrc = lo_document_xml-&gt;parse_xstring( ep_content ).
lo_document ?= lo_document_xml-&gt;m_document.
lo_element_root = lo_document-&gt;if_ixml_node~get_first_child( ).
lo_collection = lo_document-&gt;get_elements_by_tag_name( &apos;sheetPr&apos; ).
lo_iterator = lo_collection-&gt;create_iterator( ).
lo_element ?= lo_iterator-&gt;get_next( ).
WHILE lo_element IS BOUND.
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_codename
value = io_worksheet-&gt;zif_excel_sheet_vba_project~codename_pr ).
lo_element ?= lo_iterator-&gt;get_next( ).
ENDWHILE.
**********************************************************************
* STEP 3: Create xstring stream
CLEAR ep_content.
lo_ixml = cl_ixml=&gt;create( ).
lo_streamfactory = lo_ixml-&gt;create_stream_factory( ).
lo_ostream = lo_streamfactory-&gt;create_ostream_xstring( string = ep_content ).
lo_renderer = lo_ixml-&gt;create_renderer( ostream = lo_ostream document = lo_document ).
lo_renderer-&gt;render( ).
ENDMETHOD.</source>
</method>
<method CLSNAME="ZCL_EXCEL_WRITER_2007" CMPNAME="CREATE_XL_WORKBOOK" VERSION="0" EXPOSURE="0" STATE="0" EDITORDER="0 " DISPID="0 " MTDTYPE="0" MTDDECLTYP="0" BCMTDCAT="00" BCMTDSYN="0">
<source>METHOD create_xl_workbook.
** Constant node name
DATA: lc_xml_attr_codename TYPE string VALUE &apos;codeName&apos;.
DATA: lo_ixml TYPE REF TO if_ixml,
lo_document TYPE REF TO if_ixml_document,
lo_document_xml TYPE REF TO cl_xml_document,
lo_element_root TYPE REF TO if_ixml_node,
lo_element TYPE REF TO if_ixml_element,
lo_collection TYPE REF TO if_ixml_node_collection,
lo_iterator TYPE REF TO if_ixml_node_iterator,
lo_node TYPE REF TO if_ixml_node,
lo_encoding TYPE REF TO if_ixml_encoding,
lo_streamfactory TYPE REF TO if_ixml_stream_factory,
lo_ostream TYPE REF TO if_ixml_ostream,
lo_renderer TYPE REF TO if_ixml_renderer.
DATA: lv_subrc TYPE sysubrc,
lv_syindex(2) TYPE c.
**********************************************************************
* STEP 3: Create standard relationship
ep_content = super-&gt;create_xl_workbook( ).
**********************************************************************
* STEP 2: modify XML adding the vbaProject relation
CREATE OBJECT lo_document_xml.
lv_subrc = lo_document_xml-&gt;parse_xstring( ep_content ).
lo_document ?= lo_document_xml-&gt;m_document.
lo_element_root = lo_document-&gt;if_ixml_node~get_first_child( ).
lo_collection = lo_document-&gt;get_elements_by_tag_name( &apos;fileVersion&apos; ).
lo_iterator = lo_collection-&gt;create_iterator( ).
lo_element ?= lo_iterator-&gt;get_next( ).
WHILE lo_element IS BOUND.
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_codename
value = me-&gt;excel-&gt;zif_excel_book_vba_project~codename ).
lo_element ?= lo_iterator-&gt;get_next( ).
ENDWHILE.
lo_collection = lo_document-&gt;get_elements_by_tag_name( &apos;workbookPr&apos; ).
lo_iterator = lo_collection-&gt;create_iterator( ).
lo_element ?= lo_iterator-&gt;get_next( ).
WHILE lo_element IS BOUND.
lo_element-&gt;set_attribute_ns( name = lc_xml_attr_codename
value = me-&gt;excel-&gt;zif_excel_book_vba_project~codename_pr ).
lo_element ?= lo_iterator-&gt;get_next( ).
ENDWHILE.
**********************************************************************
* STEP 3: Create xstring stream
CLEAR ep_content.
lo_ixml = cl_ixml=&gt;create( ).
lo_streamfactory = lo_ixml-&gt;create_stream_factory( ).
lo_ostream = lo_streamfactory-&gt;create_ostream_xstring( string = ep_content ).
lo_renderer = lo_ixml-&gt;create_renderer( ostream = lo_ostream document = lo_document ).
lo_renderer-&gt;render( ).
ENDMETHOD.</source>
</method>
</CLAS>