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

When you call the CONVERTER in Listing 11.29, you’re passing in bound instances

of the Excel and worksheet objects (because you just created them in Listing

11.28), so the CONVERTER will use those and add the report data to the specified

worksheet. If the worksheet and Excel ob jects were not yet created when you

called the CONVERTER, then the CONVERTER would have created them for you.

To recap, when you want to use macros in a spreadsheet generated from

ABAP2XLSX, create the Excel object from a template stored in the database so that

you retain any macros therein. Otherwise , if you’re not interested in macros,

then create a new, blank Excel object.

Modifying and Saving the Excel Object

Once created, the Excel object can be modified in the exact same way as has been

demonstrated in Section 11.2.1 through Section 11.2.6, by using the standard

ABAP2XLSX classes and methods. The only tricky bit is that you need to do some

jiggery pokery just before converting the Excel object into XML to make sure that

the spreadsheet is download ed with the correct extens ion. You cannot trust the

users to get this right; you have to force the issue.

This jiggery pokery is shown in Listin g 11.30. You want to know if the Excel

object contains any macros, and the easie st way to find out is to ask the object

itself (this is an OO programming principle called “ask, don’t tell”). The object has

an attribute with the words VBA_PROJECT in its name; this attribute will only be

populated if the spreadsheet has a macro.

Once you know if the Excel object is macro enabled or not (see the LF_MACRO_

ENABLED flag in Listing 11.30), you can ch eck the file extension the user has

entered, correct it if needed, and also create the correct subclass for converting

the object into XML. If you get the extension or subclass wrong, then the user will

not be able to open the do wnloaded spreadsheet; he will only get an error message

about corruption in high places.

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

* Convert to XML

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

DATA: ld_xml_file TYPE xstring,

lo_excel_writer TYPE REF TO zif_excel_writer,

lf_macro_enabled TYPE abap_bool.

IF lo_excel->zif_excel_book_vba_project~codename_pr IS NOT INITIAL.

492

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

Saved successfully!

Ooh no, something went wrong!