14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

11

ABAP2XLSX

The next step is to consid er the most basic type of conditional formatting, in

which you look for specific values and set the color accordingly. In Listing 11.15,

you’ll see the code to set up conditional formatting. Three times, the code sets up

a rule—in this case, an “is equal to” fo rmula—and applies it to the same range

(i.e., the cells in the Monster Strength column). If more than one of the formulas

turns out to be true (impossible in this example, but you could have one rule

that is true if the value is greater than 5 and another rule that is true if the value

is greater than 10), then the rule with the highest priority (1 being higher than 3

in this case) will win.

DATA: ld_first_data_row TYPE sy-tabix,

ld_last_data_row TYPE sy-tabix.

ld_first_data_row = 2. "i.e. first row after header

ld_last_data_row = lines( gt_monsters ) + 1.

"High Strength Monster - Green for 'Good'

lo_style_conditional = lo_worksheet->add_new_conditional_style( ).

lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_cellis.

ls_cellis-formula

= '"HIGH"'.

ls_cellis-operator = zcl_excel_style_conditional=>c_operator_equal.

ls_cellis-cell_style

= ld_green_guid.

lo_style_conditional->mode_cellis = ls_cellis.

lo_style_conditional->priority = 1.

lo_style_conditional->set_range( ip_start_column = lc_strength_column

ip_start_row = ld_first_data_row

ip_stop_column = lc_strength_column

ip_stop_row = ld_last_data_row ).

"Low Strength Monster - Red for 'Bad'

lo_style_conditional = lo_worksheet->add_new_conditional_style( ).

lo_style_conditional->rule = zcl_excel_style_conditional=>c_rule_cellis.

ls_cellis-formula

= '"LOW"'.

ls_cellis-operator = zcl_excel_style_conditional=>c_operator_equal.

ls_cellis-cell_style

= ld_red_guid.

lo_style_conditional->mode_cellis = ls_cellis.

lo_style_conditional->priority = 2.

lo_style_conditional->set_range( ip_start_column = lc_strength_column

ip_start_row = ld_first_data_row

ip_stop_column = lc_strength_column

ip_stop_row = ld_last_data_row ).

476

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

Saved successfully!

Ooh no, something went wrong!