No matter how much I try to move away, I keep coming back to this. I have
talked about this multiple times in multiple posts and yet it seems less. So
bear with me and hope you stay sane/awake by the time we reach the end.
Alright, The issue is simple. Handling of trailing blanks. No need to
read further if you know it all. For the rest of the mortals like me, let's
proceed.
When it comes to character string processing, ABAP gives you two built-in
options:
C - Fixed length, trailing blanks are ignored, minimum length is
1.
STRING - Dynamic length, trailing blanks are relevant, minimum
length is 0.
Before you start feeling unrewarded for all the build-up at the start, I have
a program to share:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The Output is different even with the seemingly same
code.
Why? Because the devil is in the details,
c_text_space is type c and contains one
blank(trailing) which gets ignored. It seems trivial but is very easy
to miss. The problem is that you see a blank in the code but you don't get
it with text literal.
See, easy peasy, lemon squeezy, just that lemon gets replaced with
your brain sometimes.
Run this code and see how:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The Output has reversed. But Why? because the ABAP gods have decided
so.
There are no empty text field literals in ABAP, their minimum length is 1, so
they internally get converted to ' '(containing blank of length 1), and
Separated by clause respect the blanks.
Whereas, the text string field stays as ``(no space in between) because
the minimum length is 0. Hence the output is without space.
Alright, let's go a step further. What do you think is the output of this
code?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String length should have been 1 in all cases as string can hold blanks, but
it's not.
It's the same culprit, text field literal, whose trailing blank is ignored and
thus an empty character is assigned to the string in the first case, which
leaves the string empty with zero length.
Congratulations for making it this far. We are just about to end it with one
more example.
Suppose you want to replace a part of your text with space. Say "X,Y,Z" ->
"X Y Z".
You go ahead and use your trusty old replace statement. here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Another lifesaver is String templates, their return type is string and
spaces are always preserved, doesn't matter the position. Plus, they can do
much more than just hold literal.
Reporting is incomplete without file interfacing. Sometimes the requirement is to send a file via email or download it to a PC. but what to do when the user requests the file in XLSX format? In today's post, I am sharing how to convert an internal table to .xlsx format and then send it to a PC or email. One of the quickest ways I found it been done is using SALV classes. Below is the Code snippet Now once you have the xstring with you, simply pass it to relevant libraries to handle the output. Downloading to PC: Sending via Email It's that simple. Hope you like the post and use it in your day to day work.
You are given a requirement to upload data to a custom DB table where the user provides you with the file path of the excel on his PC and the report loads the data from it to the Custom table. For experienced Abapers, the requirement is quite straightforward, except that the user wants to upload an XLSX file. There are not many options to read data from XLSX/XLS file. The TEXT_CONVERT_XLS_TO_SAP function module is one of them but is a performance killer and uses OLE. In this post, I wanted to showcase How we can read data from the XLSX files using CL_FDT_SPOREADSHEET class. Let's start with uploading the file. Now that we have the file in xstring table, let's pass it to CL_FDT_XL_SPREADSHEET to get its object reference. From the object reference, we can retrieve each worksheet data. The data is returned in the form of an anonymous data object which can be dereferenced into a field symbol and passed to the target internal table. Below is the co...
Many a time you need a report for which the output needs to be an email. SO_NEW_DOCUMENT_SEND_API1 is heavily used to achieve the task but let's explore another way of achieving the same using CL_BCS. This post talks about how to use the CL_BCS library provided by SAP to easily send emails. CL_BCS Library The class serves as the interface from BCS to the applications. The methods of the class cater for the send functions. Using CL_BCS, You can: – Attach files to email – Build the HTML/raw of the body. – Set email’s Senders & Receiver – Send an email, etc. Let's see the steps one by one: Initiating CL_BCS: The first thing you need is to initiate a persistent send request. Creating BCS Document: Create the BCS document object which will be sent with the CL_BCS interface to BCS. In this case the Email. SET_DOCUMENT Once we have the document, It needs to be set to the send request. SET_SENDER Assign the sender to the send request. ADD_RECIPIENT Pass ...