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.

Enhancing Custom Reports with ABAP2XLSX 11.2

the results into the mainframe. Within half an hour, I was bored and started looking

for an easier way to do what I was doing; thus, I discovered macros. This was

not even in Excel; back in those days, it was Lotus 1-2-3. Nonetheless, the principle

was the same, and when Excel came al ong I was writing macros all over the

place. In 1997, I made the jump to SAP, and I haven’t written a macro since.

Therefore, I’m not going to give a fancy example here; instead, I’ll present the

bog-standard Microsoft equivalent of the “hello world” macro, which you’ll find

right at the start of their training documentation. In this example, you will use a

macro to set the spreadsheet name. To do so, open a blank spreadsheet, go to the

macro section, and type the code in Listing 11.25.

Private Sub Workbook_Open()

Sheets("Sheet1").Select

Sheets("Sheet1").Name = "Name Set by Macro"

End Sub

Listing 11.25 Macro to Change the Name of a Worksheet

Save the blank spreadsheet as an XLSM file (macro-enabled format) to your local

drive.

Uploading the Blank Spreadsheet into SAP

I will not show any code for making the user(which is you, in this case) select the

file path, because that is 100% normal SAP code that you have probably written a

million times before. Suffice to say that there is a selection screen to choose the

file path and the name of the template to be stored.

In Listing 11.26, you start off knowing where the file lives: the file path is in LD_

FULL_PATH. Load the blank worksh eet with the macros off of the desktop into

SAP, and then transform it into an Excel object. For once, you don’t want to modify

that Excel object, so leave it as is, and transform it into an XML representation

stored inside an XSTRING, which is the form the data needs to be in if you’re going

to be able to save it to the database.

START-OF-SELECTION.

DATA: lo_excel

TYPE REF TO zcl_excel,

lo_excel_writer TYPE REF TO zif_excel_writer,

lo_excel_reader TYPE REF TO zif_excel_reader,

ld_object_as_xstring TYPE xstring.

CREATE OBJECT lo_excel_reader TYPE zcl_excel_reader_xlsm.

489

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

Saved successfully!

Ooh no, something went wrong!