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

Advanced

A more advanced example of conditional formatting is to take advantage of the

full range of formulas available in Excel to test if a value is bigger or smaller than

a certain value. For instance, Listing 11.16 causes a cell to become red if it starts

with a 1.

lo_style_conditional = lo_worksheet->add_new_conditional_style( ).

lo_style_conditional->rule = zcl_excel_style_conditional=>c_operator_

beginswith.

ls_cellis-formula = '1'.

ls_cellis-operator = zcl_excel_style_conditional=>c_operator_beginswith.

ls_cellis-cell_style = ld_red_guid.

Listing 11.16 Conditional Formatting: Testing the Start of a String

Testing for cells that begin with something is not standard in ABAP2XLSX, but it

can be done. First, manually create a sp readsheet and set up the conditional formatting

the way you want it—in this example, with colors changing based on a

cell starting with a 1. Then save the sp readsheet in the way described in Section

11.1.1 so that you can see the structure of the resulting XML file. You’ll notice

that in the sheet1 section of the XML file, the XML code shown in Listing 11.17

had been generated.

-<conditionalFormatting sqref="G6:G12">-

<cfRule operator="beginsWith" priority="4" dxfId="4"

type="beginsWith" text="1">

<formula>LEFT(G6,LEN("1"))="1"</formula>

</cfRule>

</conditionalFormatting>

Listing 11.17 Generated XML Code

If you want this same functionality in ABAP2XLSX, you have to find a way to

manually generate this XML code based onthe attributes of the worksheet object.

It turns out the XML for conditional formatting is generated inside the ZCL_

EXCEL_WRITER_2007 class in the CREATE_XL_SHEET method. Inside that method,

there is a CASE statement on LO_STYLE_CONDITION->RULE, which is a value that was

set in Listing 11.16. Listing 11.18 adds a new branch to the case statement and

calls various methods to build up the XML structure.

The top node is set outside of the CASE statement by the standard ABAP2XLSX

class; this is represented by an object called LO_ELEMENT, which contains the node

that starts and ends with <conditional formatting>.

478

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

Saved successfully!

Ooh no, something went wrong!