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

BIND_TABLE - table_style: None. New method CHANGE_RANGE_STYLE #1228

Closed
wants to merge 46 commits into from

Conversation

RixarSAP
Copy link
Contributor

Hello,

The method BIND_TABLE is great to write your data from an internal table to your excel sheet. But is always assigning a style to the table, like this:

image

Even if you leave this field empty is assigning a "default" style:

image

In method CREATE_XL_TABLE of class ZCL_EXCEL_WRITER_2007 is always adding this XML tag:

image

But sometimes I prefer to not set any style to my table and change some cells afterwards... and this possibility does not exist now.

So I've created a new constant: zcl_excel_table=>table_style_none to not add any style to the tables.
You can test it setting this constant in report ZDEMO_EXCEL10.

image


There is another thing I miss in ABAP2XLSX, and is change the style to a range of cells. Now I need to create several nested loops and change the style cell by cell. (Correct me if I'm wrong, I'm still learning how to use abap2xlsx)

So I've created a new method CHANGE_CELL_STYLE.

You can test it with the following demo program.

image

image

*&---------------------------------------------------------------------*
*& Report  ZDEMO_EXCEL_RANGE_STYLES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdemo_excel_range_styles.

TYPE-POOLS: abap.

PARAMETERS:
  pa_table TYPE tabname MATCHCODE OBJECT dd_tabl.

SELECTION-SCREEN SKIP.

PARAMETERS:
  pa_nrows TYPE i DEFAULT 50,
  pa_nokey TYPE i DEFAULT 5.

START-OF-SELECTION.

  DATA:
    lv_no_key_fields TYPE i,
    lv_key_fields    TYPE i,
    lv_lenght        TYPE i,
    lv_decimals      TYPE i,
    lt_fields        TYPE STANDARD TABLE OF dd03l,
    lt_comp          TYPE cl_abap_structdescr=>component_table,
    ls_comp          LIKE LINE OF lt_comp,
    lo_table         TYPE REF TO data,
    lo_new_type      TYPE REF TO cl_abap_structdescr,
    lo_table_type    TYPE REF TO cl_abap_tabledescr.

  DATA:
    lv_title          TYPE zexcel_sheet_title,
    lv_col_from       TYPE string,
    lv_col_to         TYPE string,
    lv_col            TYPE i,
    lo_excel          TYPE REF TO zcl_excel,
    ls_table_settings TYPE zexcel_s_table_settings,
    lo_worksheet      TYPE REF TO zcl_excel_worksheet.

  FIELD-SYMBOLS:
    <ls_fields> TYPE dd03l,
    <lo_table>  TYPE STANDARD TABLE.

  SELECT fieldname position keyflag inttype intlen decimals
   INTO CORRESPONDING FIELDS OF TABLE lt_fields
   FROM dd03l
   WHERE tabname  EQ pa_table
     AND comptype IN ('E', space )
   ORDER BY position.
  IF sy-subrc NE 0.
    MESSAGE 'The table does not exist' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.

  LOOP AT lt_fields ASSIGNING <ls_fields>.

    IF <ls_fields>-keyflag EQ abap_false.
      ADD 1 TO lv_no_key_fields.
    ELSE.
      ADD 1 TO lv_key_fields.
    ENDIF.

    IF lv_no_key_fields GT pa_nokey.
      EXIT.
    ENDIF.

    lv_lenght   = <ls_fields>-intlen.
    lv_decimals = <ls_fields>-decimals.
    CASE <ls_fields>-inttype.
      WHEN 'STRING'.  ls_comp-type = cl_abap_elemdescr=>get_string( ).
      WHEN 'XSTRING'. ls_comp-type = cl_abap_elemdescr=>get_xstring( ).
      WHEN 'I'.       ls_comp-type = cl_abap_elemdescr=>get_i( ).
      WHEN 'F'.       ls_comp-type = cl_abap_elemdescr=>get_f( ).
      WHEN 'D'.       ls_comp-type = cl_abap_elemdescr=>get_d( ).
      WHEN 'T'.       ls_comp-type = cl_abap_elemdescr=>get_t(  ).
      WHEN 'C'.       ls_comp-type = cl_abap_elemdescr=>get_c( p_length = lv_lenght ).
      WHEN 'N'.       ls_comp-type = cl_abap_elemdescr=>get_n( p_length = lv_lenght ).
      WHEN 'X'.
*                      ls_comp-type = cl_abap_elemdescr=>get_x( p_length = lv_lenght ).
                      ls_comp-type = cl_abap_elemdescr=>get_i( ).
      WHEN 'P'.       ls_comp-type = cl_abap_elemdescr=>get_p( p_length = lv_lenght p_decimals = lv_decimals ).
      WHEN OTHERS.
        ls_comp-type = cl_abap_elemdescr=>get_string( ).
    ENDCASE.

    ls_comp-name = <ls_fields>-fieldname.
    APPEND ls_comp TO lt_comp. CLEAR ls_comp.

  ENDLOOP.

  IF lv_no_key_fields GT 0.
    SUBTRACT 1 FROM lv_no_key_fields.
  ENDIF.

* Create dynamic table with these components:
  lo_new_type = cl_abap_structdescr=>create( lt_comp ).
  lo_table_type = cl_abap_tabledescr=>create( lo_new_type ).
  CREATE DATA lo_table TYPE HANDLE lo_table_type.
  ASSIGN lo_table->* TO <lo_table>.

* Select the data:
  SELECT *
   FROM (pa_table)
   INTO CORRESPONDING FIELDS OF TABLE <lo_table>
   UP TO pa_nrows ROWS.
  IF sy-subrc NE 0.
    MESSAGE 'The data found' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.

*--------------------------------------------------------------------*
*--------------------------------------------------------------------*


  CREATE OBJECT lo_excel.

  lv_title = pa_table.
  lo_worksheet = lo_excel->get_active_worksheet( ).
  lo_worksheet->set_title( ip_title = lv_title ).

  ls_table_settings-table_style  = zcl_excel_table=>table_style_none.

  TRY.
      CALL METHOD lo_worksheet->bind_table
        EXPORTING
          ip_table          = <lo_table>
*         it_field_catalog  =
          is_table_settings = ls_table_settings.
    CATCH zcx_excel .
  ENDTRY.

* Change header of KEY fields
  lv_col_from = zcl_excel_common=>convert_column2alpha( 1 ).
  lv_col_to   = zcl_excel_common=>convert_column2alpha( lv_key_fields ).
  CALL METHOD lo_worksheet->change_range_style
    EXPORTING
      ip_column_from              = lv_col_from
      ip_row_from                 = 1
      ip_column_to                = lv_col_to
      ip_row_to                   = 1
      ip_alignment_horizontal     = zcl_excel_style_alignment=>c_horizontal_center
      ip_alignment_vertical       = zcl_excel_style_alignment=>c_vertical_center
      ip_fill_filltype            = zcl_excel_style_fill=>c_fill_solid
      ip_fill_fgcolor_rgb         = '9BC2E6' "Blue
      ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin.

* Change styles of key fields items
  CALL METHOD lo_worksheet->change_range_style
    EXPORTING
      ip_column_from              = lv_col_from
      ip_row_from                 = 2
      ip_column_to                = lv_col_to
      ip_row_to                   = lines( <lo_table> )
      ip_alignment_horizontal     = zcl_excel_style_alignment=>c_horizontal_center
      ip_alignment_vertical       = zcl_excel_style_alignment=>c_vertical_center
      ip_fill_filltype            = zcl_excel_style_fill=>c_fill_solid
      ip_fill_fgcolor_rgb         = 'DDEBF7' "light blue
      ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin.

* Change header of NO KEY fields
  IF lv_no_key_fields IS NOT INITIAL.
    lv_col = lv_key_fields + 1.
    lv_col_from = zcl_excel_common=>convert_column2alpha( lv_col ).
    lv_col = lv_col + lv_no_key_fields - 1.
    lv_col_to   = zcl_excel_common=>convert_column2alpha( lv_col ).
    CALL METHOD lo_worksheet->change_range_style
      EXPORTING
        ip_column_from              = lv_col_from
        ip_row_from                 = 1
        ip_column_to                = lv_col_to
        ip_row_to                   = 1
        ip_alignment_horizontal     = zcl_excel_style_alignment=>c_horizontal_center
        ip_alignment_vertical       = zcl_excel_style_alignment=>c_vertical_center
        ip_fill_filltype            = zcl_excel_style_fill=>c_fill_solid
        ip_fill_fgcolor_rgb         = 'A3E37B' "Green
        ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin.

* Change styles of NO key fields items
    CALL METHOD lo_worksheet->change_range_style
      EXPORTING
        ip_column_from              = lv_col_from
        ip_row_from                 = 2
        ip_column_to                = lv_col_to
        ip_row_to                   = lines( <lo_table> )
        ip_alignment_horizontal     = zcl_excel_style_alignment=>c_horizontal_center
        ip_alignment_vertical       = zcl_excel_style_alignment=>c_vertical_center
        ip_fill_filltype            = zcl_excel_style_fill=>c_fill_solid
        ip_fill_fgcolor_rgb         = 'E2EFDA' "light green
        ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin.
  ENDIF.

  PERFORM save_file.

*&---------------------------------------------------------------------*
*&      Form  SAVE_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM save_file .

  DATA:
    lv_xdata     TYPE xstring,
    lt_rawdata   TYPE solix_tab,
    lv_bytecount TYPE i,
    cl_writer    TYPE REF TO zif_excel_writer.

  DATA:
    lv_folder   TYPE string,
    lv_filename TYPE string.

  CALL METHOD cl_gui_frontend_services=>directory_browse
    CHANGING
      selected_folder      = lv_folder
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0 OR lv_folder IS INITIAL.
    MESSAGE 'No folder' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ENDIF.

  CONCATENATE lv_folder '\' pa_table '.xlsx' INTO lv_filename.


  CREATE OBJECT cl_writer TYPE zcl_excel_writer_2007.
  lv_xdata = cl_writer->write_file( lo_excel ).
  lt_rawdata = cl_bcs_convert=>xstring_to_solix( iv_xstring  = lv_xdata ).
  lv_bytecount = xstrlen( lv_xdata ).

* Descargar fichero
  CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
      bin_filesize            = lv_bytecount
      filename                = lv_filename
      filetype                = 'BIN'
    CHANGING
      data_tab                = lt_rawdata
    EXCEPTIONS
      file_write_error        = 1
      no_batch                = 2
      gui_refuse_filetransfer = 3
      invalid_type            = 4
      no_authority            = 5
      unknown_error           = 6
      header_not_allowed      = 7
      separator_not_allowed   = 8
      filesize_not_allowed    = 9
      header_too_long         = 10
      dp_error_create         = 11
      dp_error_send           = 12
      dp_error_write          = 13
      unknown_dp_error        = 14
      access_denied           = 15
      dp_out_of_memory        = 16
      disk_full               = 17
      dp_timeout              = 18
      file_not_found          = 19
      dataprovider_exception  = 20
      control_flush_error     = 21
      not_supported_by_gui    = 22
      error_no_gui            = 23
      OTHERS                  = 24.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
               DISPLAY LIKE 'E'.
  ELSE.
    MESSAGE 'Download complete' TYPE 'S'.
  ENDIF.

ENDFORM.                    " SAVE_FILE

Ley me know if you like the changes.
Regards.

RixarSAP and others added 30 commits May 2, 2024 11:10
Add methods SET_SIZE and GET_SIZE to add the posibility to change the size of the comments.

These methods modify the new attributes of the class WIDTH and HEIGHT.
These attributes will be read in CREATE_XL_DRAWING_FOR_COMMENTS of class ZCL_EXCEL_WRITER_2007 when the XML of the excel sheet is being gnerated
New Methods SET_SIZE and GET_SIZE.
New attributes: WIDHT, HEIGHT, DEFAULT_WIDTH, DEFAULT_HEIGHT

The size of the comments will be read in class ZCL_EXCEL_WRITER_2007, method CREATE_XL_DRAWING_FOR_COMMENTS
Modified method CREATE_XL_DRAWING_FOR_COMMENTS to have the possibility to change the size of the comments
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
I've changed again the default width to 2. 
I've changed other numbers in the anchor string, and I think work better with width 2 by defautl.

Sorry for these last changes.
Renaming attributes to better understanding
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
@sandraros
Copy link
Collaborator

Mass update of styles already exist, see the class ZCL_EXCEL_STYLE_CHANGER and the demo program using it.

@RixarSAP
Copy link
Contributor Author

Mass update of styles already exist, see the class ZCL_EXCEL_STYLE_CHANGER and the demo program using it.

Oh, thanks, didn't know that.

I'll remove the PR. may be creating a new one with only the first part.

@sandraros
Copy link
Collaborator

sandraros commented May 14, 2024

Okay. Are you sure it's important to set a null style if you set the styles right after? Also, a Table Style is different from a Cell Style.

@RixarSAP
Copy link
Contributor Author

RixarSAP commented May 14, 2024

Okay. Are you sure it's important to set a null style if you set the styles right after? Also, a Table Style is different from a Cell Style.

Well, not important, it's just to have this option, may be in some scenarios we dont need any style in a table.

I've made this change because I've tried to change the style of a cell and was not working. Probably I'm doing something wrong.

What I did is modify the program ZDEMO_EXCEL10 adding these lines after the BIND_TABLE, to change the font color from white to black, and its not working.

**--------------------------------------------------------------------*
  DATA:
    lv_cell_style TYPE zexcel_cell_style,
    ls_font       TYPE zexcel_s_cstyle_font,
    ls_font_color TYPE zexcel_s_style_color.

  ls_font-size      = 8.
  ls_font-color     = zcl_excel_style_color=>c_black..
*  ls_font_color-rgb = zcl_excel_style_color=>c_black. "'000000'.  "c_blue.

  TRY.
      CALL METHOD lo_worksheet->change_cell_style
        EXPORTING
          ip_column                   = 'A'
          ip_row                      = 1
          ip_alignment_horizontal     = zcl_excel_style_alignment=>c_horizontal_center
          ip_alignment_vertical       = zcl_excel_style_alignment=>c_vertical_center
          ip_borders_allborders_style = zcl_excel_style_border=>c_border_thin
          ip_alignment_wraptext       = abap_true
          ip_font                     = ls_font
**          ip_font_color               = ls_font_color
**          ip_font_color_rgb           = zcl_excel_style_color=>c_black   "c_blue
          ip_fill_filltype            = zcl_excel_style_fill=>c_fill_solid
          ip_fill_fgcolor_rgb         = 'C5D9F1'
        RECEIVING
          ep_guid                     = lv_cell_style.


    CATCH zcx_excel .
  ENDTRY.
*--------------------------------------------------------------------*

image

The strange it's that if I use c_blue instead c_black, then its changing the font color:

image

I thought there was some problem with table style and cell styles, so I've made this modification to not add any style to the table.

I'll try with ZCL_EXCEL_STYLE_CHANGER.

PS: Other thing I miss in method BIND_TABLE is the possibility to write the table without header. I know there is a method SET_TABLE, but is a simple method without all the features of BIND_TABLE method...

@RixarSAP RixarSAP closed this May 20, 2024
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.

None yet

2 participants