14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

11

ABAP2XLSX

lo_worksheet->get_cell(

EXPORTING ip_column = ld_column_alpha

ip_row = ld_row_integer

IMPORTING ep_guid = ld_cell_style ).

TRY.

ls_stylemapping = lo_worksheet->excel->get_style_to_guid( ld_cell_style ).

CATCH zcx_excel.

CLEAR ls_stylemapping.

ENDTRY.

Listing 11.8 Finding the Style of a Spreadsheet Cell

Once you know the style, you can see the current number of decimal places. If it’s

two, then you’re dealing with a currency number; if there are three decimal

places, then you’re dealing with a quantity.

Listing 11.9 makes changes based on th e current style (determined in Listing

11.8). If there are two decimal places (i.e., a currency), then the code sets the cell

to turn red when negative, to not have a minus sign, and to display a dash when

the value is zero (quantities are unchanged so the end user can tell that numbers

with three decimal places are quantiti es). Changing the number of decimals

places and so forth is done by setting the format code variable to the same value

as the string you would manually type in to Excel to achieve the same goal and

then passing this variable into a method called CHANGE_CELL_STYLE. Once the code

has made the change, it moves on to the next cell.

IF ls_stylemapping-complete_style-number_format-format_code = '#,##0.00'.

" This is a currency amount, use the accounting conventions

" which are to have negative numbers as red in brackets, and show zero values

" as dashes, so as to focus the eye on the real numbers

ls_stylemapping-complete_style-number_format-format_code = '$#,##0.00;

[Red]($#,##0.00);-'.

lo_worksheet->change_cell_style(

ip_column

= ld_column_alpha

ip_row

= ld_row_integer

ip_number_format_format_code = ls_stylemapping-complete_style-number_

format-format_code ).

ENDIF."It’s a number with two decimals

ENDDO."Rows

ENDDO."Columns

Listing 11.9 Setting a Cell to be Formatted Appropriately

470

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!