Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header/footer images and comments together create wrong file destinations in sheet#.xml.rels #1201

Merged
merged 5 commits into from
Apr 20, 2024

Conversation

darnoc312
Copy link
Contributor

@darnoc312 darnoc312 commented Apr 6, 2024

Hello,

Try to add any comment in the second sheet of demo program ZTEST_EXCEL_IMAGE_HEADER and run it.
You will not succeed.

For comments the same index is used for the xl/comments.xml file and the xl/drawings/vmlDrawing.vml file.
This works as long as the comments are the only user of vml drawings.
But now there are header/footer images which also uses vml drawings.

In the example above however the first vml index is already used by first sheet's header/footer if the comment is created.
That means that the comment needs comments1.xml and vmlDrawing2.vml. This is not possible with current ABAP2XLSX.
And you cannot use comment2.xml leaving a gap, because comment files in [Content_Types].xml are counted from 1 without gaps.

The relationship between comment index and corresponding vml index must be separated.
We need one comment counter for xl/comments#.xml only and one vml counter
supplying vml index for comments and vml index for header/footer.

The following changes are to make in zcl_excel_writer_2007's methods create and create_xl_sheet_rels:

The new idea of calling method create_xl_sheet_rels is not to pass counters as currently but indices which are filled with
current counter values if needed or with initial value if not. The number of parameters to pass increases to four.
So you can use them in this method as a mark to create the respective relation or not without any need to call the according
get data method for empty check again.

I attach an excel file with the content to achieve:
Image_Header_Footer_with_comment.xlsx

Demo program ZTEST_EXCEL_IMAGE_HEADER with a minimum change for demonstration:

*&---------------------------------------------------------------------*
*& Report  ZTEST_EXCEL_IMAGE_HEADER
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ztest_excel_image_header.

DATA: lo_excel     TYPE REF TO zcl_excel,
      lo_worksheet TYPE REF TO zcl_excel_worksheet,
      lo_drawing   TYPE REF TO zcl_excel_drawing,
      ls_key       TYPE wwwdatatab,
      ls_header    TYPE zexcel_s_worksheet_head_foot,
      ls_footer    TYPE zexcel_s_worksheet_head_foot,
      lv_content   TYPE xstring.

CONSTANTS: gc_save_file_name TYPE string VALUE 'Image_Header_Footer.xlsx'.
INCLUDE zdemo_excel_outputopt_incl.

START-OF-SELECTION.

  " Creates active sheet
  CREATE OBJECT lo_excel.

**********************************************************************
*** Header Center
  " create global drawing, set position and media from web repository
  lo_drawing = lo_excel->add_new_drawing( ip_type = zcl_excel_drawing=>type_image_header_footer ).

  ls_key-relid = 'MI'.
  ls_key-objid = 'SAPLOGO.GIF'.
  lo_drawing->set_media_www( ip_key = ls_key
                            ip_width = 166
                            ip_height = 75 ).
**********************************************************************
  ls_header-center_image = lo_drawing.


**********************************************************************
*** Header Left
  " create global drawing, set position and media from web repository
  lo_drawing = lo_excel->add_new_drawing( ip_type = zcl_excel_drawing=>type_image_header_footer ).

  ls_key-relid = 'MI'.
  ls_key-objid = 'SAPLOGO.GIF'.
  lo_drawing->set_media_www( ip_key = ls_key
                             ip_width = 166
                             ip_height = 75 ).


  ls_header-left_image = ls_footer-left_image = lo_drawing.
  ls_header-left_value = 'Hallo'.
  lo_worksheet = lo_excel->get_active_worksheet( ).

  lo_worksheet->sheet_setup->set_header_footer( ip_odd_header = ls_header
                                                ip_odd_footer = ls_footer ).

**********************************************************************
*** Normal Image
  " create global drawing, set position and media from web repository
  lo_drawing = lo_excel->add_new_drawing( ).
  lo_drawing->set_position( ip_from_row = 3
                            ip_from_col = 'B' ).

  ls_key-relid = 'MI'.
  ls_key-objid = 'SAPLOGO.GIF'.
  lo_drawing->set_media_www( ip_key = ls_key
                             ip_width = 166
                             ip_height = 75 ).

  " assign drawing to the worksheet
  lo_worksheet->add_drawing( lo_drawing ).

**********************************************************************
**********************************************************************
* New sheet
  lo_worksheet = lo_excel->add_new_worksheet( 'Sheet2' ).

  " Add some content otherwise the error "nothing to be printed" is shown
  lo_worksheet->set_cell( ip_column = 'B' ip_row = 3 ip_value = 'Hello world' ).

**** Adding a comment in this sheet for demonstration ****
  DATA lo_comment TYPE REF TO zcl_excel_comment.
  lo_comment = lo_excel->add_new_comment( ).
  lo_comment->set_text( ip_ref = 'B3' ip_text = 'Just a comment' ).
  lo_worksheet->add_comment( lo_comment ).

**********************************************************************
*** Header Left
  " create global drawing, set position and media from web repository
  lo_drawing = lo_excel->add_new_drawing( ip_type = zcl_excel_drawing=>type_image_header_footer ).

  ls_key-relid = 'MI'.
  ls_key-objid = 'SAPLOGO.GIF'.
  lo_drawing->set_media_www( ip_key = ls_key
                             ip_width = 166
                             ip_height = 75 ).


  CLEAR ls_header.
  ls_header-left_image = ls_footer-left_image = lo_drawing.

  lo_worksheet->sheet_setup->set_header_footer( ip_odd_header = ls_header ).


*** Create output
  lcl_output=>output( lo_excel ).

Copy link
Collaborator

@sandraros sandraros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, it works nicely 👍

@sandraros sandraros merged commit 5cb47b1 into abap2xlsx:main Apr 20, 2024
5 checks passed
@darnoc312 darnoc312 deleted the patch-2 branch April 22, 2024 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants