This commit is contained in:
danrega
2024-12-17 11:44:12 +01:00
parent 3c1ec86a5b
commit 4081ecf027

View File

@@ -10,6 +10,7 @@
- [Transactional Consistency](#transactional-consistency)
- [Numbers and Calculations](#numbers-and-calculations)
- [String Processing](#string-processing)
- [Handling Codepages and Binary Data](#handling-codepages-and-binary-data)
- [Regular Expressions](#regular-expressions)
- [Time and Date](#time-and-date)
- [Calendar-Related Information](#calendar-related-information)
@@ -34,6 +35,7 @@
- [Reading and Writing XLSX Content](#reading-and-writing-xlsx-content)
- [Zip Files](#zip-files)
- [ABAP Unit](#abap-unit)
- [Units of Measurement](#units-of-measurement)
This ABAP cheat sheet contains a selection of [released](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreleased_api_glosry.htm) ABAP classes that are available in [ABAP for Cloud Development](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_for_cloud_dev_glosry.htm). It serves as a quick introduction, along with code snippets to explore the functionality in action.
@@ -523,68 +525,7 @@ DATA(random_num2) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
<tr>
<td> Class </td> <td> Details/Code Snippet </td>
</tr>
<tr>
<td> <code>CL_ABAP_GZIP*</code> </td>
<td>
For (de)compressing character strings and byte strings using GZIP:
<code>CL_ABAP_GZIP</code>,
<code>CL_ABAP_GZIP_BINARY_STREAM</code>,
<code>CL_ABAP_GZIP_TEXT_STREAM</code>,
<code>CL_ABAP_UNGZIP_BINARY_STREAM</code>,
<code>CL_ABAP_UNGZIP_TEXT_STREAM</code>
<br><br>
``` abap
"------- (De)compressing binary data -------
DATA(str) = `This is a data object of type string. It should be converted to xstring, compressed and decompressed.`.
DATA(xstr) = cl_abap_conv_codepage=>create_out( )->convert( str ).
DATA xstr_comp TYPE xstring.
"Compressing binary data
TRY.
cl_abap_gzip=>compress_binary( EXPORTING raw_in = xstr
IMPORTING gzip_out = xstr_comp ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error.
ENDTRY.
"Comparing the length of the data objects
DATA(len_xstr) = xstrlen( xstr ). "101
DATA(len_xstr_comp) = xstrlen( xstr_comp ). "81
"Decompressing binary data
DATA xstr_decomp TYPE xstring.
TRY.
cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = xstr_comp
IMPORTING raw_out = xstr_decomp ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error.
ENDTRY.
DATA(len_xstr_decomp) = xstrlen( xstr_decomp ). "101
DATA(conv_str) = cl_abap_conv_codepage=>create_in( )->convert( xstr_decomp ).
"abap_true
DATA(is_equal) = xsdbool( len_xstr = len_xstr_decomp AND str = conv_str ).
"------- (De)compressing character strings -------
DATA zipped TYPE xstring.
TRY.
cl_abap_gzip=>compress_text( EXPORTING text_in = `Hello world`
IMPORTING gzip_out = zipped ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error.
ENDTRY.
DATA txt TYPE string.
TRY.
cl_abap_gzip=>decompress_text( EXPORTING gzip_in = zipped
IMPORTING text_out = txt ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error.
ENDTRY.
ASSERT txt = `Hello world`.
```
</td>
</tr>
<tr>
<td> <code>CL_ABAP_CHAR_UTILITIES</code> </td>
<td>
@@ -628,30 +569,7 @@ DATA(str_no_blanks) = CONV string( chars ).
</td>
</tr>
<tr>
<td> <code>CL_ABAP_CONV_CODEPAGE</code> </td>
<td>
For handling code pages, converting strings to the binary representation of different code pages and vice versa.
<br><br>
``` abap
DATA(hi) = `Hello world`.
"string -> xstring
"Note: UTF-8 is used by default. Here, it is specified explicitly.
TRY.
DATA(conv_xstring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( hi ).
CATCH cx_sy_conversion_codepage.
ENDTRY.
"48656C6C6F20776F726C64
"xstring -> string
DATA(conv_string) = cl_abap_conv_codepage=>create_in( )->convert( conv_xstring ).
"Hello world
```
</td>
</tr>
<tr>
<td> <code>XCO_CP</code> </td>
<td>
@@ -766,42 +684,6 @@ ELSE.
check = `no`.
ENDIF.
"--------- Converting strings to xstrings using a codepage ---------
"536F6D6520737472696E67
DATA(xstr) = xco_cp=>string( `Some string` )->as_xstring( xco_cp_character=>code_page->utf_8 )->value.
"--------- Camel case compositions and decompositions with split and join operations ---------
"Pascal case is also possible
"someValue
DATA(comp) = xco_cp=>string( `some_value` )->split( `_` )->compose( xco_cp_string=>composition->camel_case )->value.
"Camel case decomposition
"some_value
DATA(decomp) = xco_cp=>string( `someValue` )->decompose( xco_cp_string=>decomposition->camel_case )->join( `_` )->value.
"------ Processing Base64 representations of raw binary data ------
DATA(a_string) = `Hello world`.
"string -> xstring
"Result: 48656C6C6F20776F726C64
DATA(conv_xstring) = xco_cp=>string( a_string
)->as_xstring( xco_cp_character=>code_page->utf_8
)->value.
"Encoding of raw binary data into its Base64 representation
"Result: SGVsbG8gd29ybGQ=
DATA(raw2base64) = xco_cp=>xstring( conv_xstring
)->as_string( xco_cp_binary=>text_encoding->base64
)->value.
"Decoding of a Base64 representation into raw binary data
"Result: 48656C6C6F20776F726C64
DATA(base642raw) = xco_cp=>string( raw2base64
)->as_xstring( xco_cp_binary=>text_encoding->base64
)->value.
"xstring -> string
"Result: Hello world
DATA(conv_string_xco) = xco_cp=>xstring( base642raw
)->as_string( xco_cp_character=>code_page->utf_8
)->value.
"--------- Matching string against regular expression ---------
DATA match TYPE string.
@@ -826,6 +708,156 @@ ENDIF.
<p align="right"><a href="#top">⬆️ back to top</a></p>
## Handling Codepages and Binary Data
<table>
<tr>
<td> Class </td> <td> Details/Code Snippet </td>
</tr>
<tr>
<td> <code>CL_ABAP_GZIP*</code> </td>
<td>
(De)compressing character strings and byte strings using GZIP:
<code>CL_ABAP_GZIP</code>,
<code>CL_ABAP_GZIP_BINARY_STREAM</code>,
<code>CL_ABAP_GZIP_TEXT_STREAM</code>,
<code>CL_ABAP_UNGZIP_BINARY_STREAM</code>,
<code>CL_ABAP_UNGZIP_TEXT_STREAM</code>
<br><br>
``` abap
"------- (De)compressing binary data -------
DATA(str) = `This is a data object of type string. It should be converted to xstring, compressed and decompressed.`.
DATA(xstr) = cl_abap_conv_codepage=>create_out( )->convert( str ).
DATA xstr_comp TYPE xstring.
"Compressing binary data
TRY.
cl_abap_gzip=>compress_binary( EXPORTING raw_in = xstr
IMPORTING gzip_out = xstr_comp ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error.
ENDTRY.
"Comparing the length of the data objects
DATA(len_xstr) = xstrlen( xstr ). "101
DATA(len_xstr_comp) = xstrlen( xstr_comp ). "81
"Decompressing binary data
DATA xstr_decomp TYPE xstring.
TRY.
cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = xstr_comp
IMPORTING raw_out = xstr_decomp ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error.
ENDTRY.
DATA(len_xstr_decomp) = xstrlen( xstr_decomp ). "101
DATA(conv_str) = cl_abap_conv_codepage=>create_in( )->convert( xstr_decomp ).
"abap_true
DATA(is_equal) = xsdbool( len_xstr = len_xstr_decomp AND str = conv_str ).
"------- (De)compressing character strings -------
DATA zipped TYPE xstring.
TRY.
cl_abap_gzip=>compress_text( EXPORTING text_in = `Hello world`
IMPORTING gzip_out = zipped ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error.
ENDTRY.
DATA txt TYPE string.
TRY.
cl_abap_gzip=>decompress_text( EXPORTING gzip_in = zipped
IMPORTING text_out = txt ).
CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error.
ENDTRY.
ASSERT txt = `Hello world`.
```
</td>
</tr>
<tr>
<td> <code>CL_ABAP_CONV_CODEPAGE</code> </td>
<td>
For handling code pages, converting strings to the binary representation of different code pages and vice versa.
<br><br>
``` abap
DATA(hi) = `Hello world`.
"string -> xstring
"Note: UTF-8 is used by default. Here, it is specified explicitly.
TRY.
DATA(conv_xstring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( hi ).
CATCH cx_sy_conversion_codepage.
ENDTRY.
"48656C6C6F20776F726C64
"xstring -> string
DATA(conv_string) = cl_abap_conv_codepage=>create_in( )->convert( conv_xstring ).
"Hello world
```
</td>
</tr>
<tr>
<td> <code>XCO_CP</code> </td>
<td>
Converting strings to xstrings using a codepage using the XCO Library
<br><br>
``` abap
"536F6D6520737472696E67
DATA(xstr) = xco_cp=>string( `Some string` )->as_xstring( xco_cp_character=>code_page->utf_8 )->value.
"Some string
DATA(str) = xco_cp=>xstring( xstr )->as_string( xco_cp_character=>code_page->utf_8 )->value.
```
</td>
</tr>
<tr>
<td> <code>XCO_CP</code> </td>
<td>
Processing Base64 representations of raw binary data
<br><br>
``` abap
DATA(a_string) = `Hello world`.
"string -> xstring
"Result: 48656C6C6F20776F726C64
DATA(conv_xstring) = xco_cp=>string( a_string
)->as_xstring( xco_cp_character=>code_page->utf_8
)->value.
"Encoding of raw binary data into its Base64 representation
"Result: SGVsbG8gd29ybGQ=
DATA(raw2base64) = xco_cp=>xstring( conv_xstring
)->as_string( xco_cp_binary=>text_encoding->base64
)->value.
"Decoding of a Base64 representation into raw binary data
"Result: 48656C6C6F20776F726C64
DATA(base642raw) = xco_cp=>string( raw2base64
)->as_xstring( xco_cp_binary=>text_encoding->base64
)->value.
"xstring -> string
"Result: Hello world
DATA(conv_string_xco) = xco_cp=>xstring( base642raw
)->as_string( xco_cp_character=>code_page->utf_8
)->value.
```
</td>
</tr>
</table>
<p align="right"><a href="#top">⬆️ back to top</a></p>
## Regular Expressions
<table>
@@ -3857,4 +3889,245 @@ cl_abap_unit_assert=>assert_equals(
</tr>
</table>
<p align="right"><a href="#top">⬆️ back to top</a></p>
## Units of Measurement
<table>
<tr>
<td> Class </td> <td> Details/Code Snippet </td>
</tr>
<tr>
<td> <code>CL_UOM_DIM_MAINTENANCE</code> </td>
<td>
- Handling dimensions
- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/8961c2c4cebf457f95fb080a736babdc.html?locale=en-US) and in the class documentation.
- The example code snippet includes reading dimensions. See the link above for the methods to create, change and delete dimensions.
<br><br>
```abap
CLASS zcl_some_class DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_some_class IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA(dimension_inst) = cl_uom_dim_maintenance=>get_instance( ).
TRY.
dimension_inst->read( EXPORTING dimid = 'TEMP'
IMPORTING dim_st = DATA(dimension_details) ).
DATA(id) = dimension_details-dimid.
DATA(text) = dimension_details-txdim.
out->write( data = id name = `id` ).
out->write( data = text name = `text` ).
CATCH cx_uom_error INTO DATA(read_error).
out->write( read_error->get_text( ) ).
ENDTRY.
out->write( repeat( val = `-` occ = 50 ) ).
**********************************************************************
"Reading dimensions based on entries in the released view I_UnitOfMeasureDimension
"Various components and values from the returned structure are output for demonstration purposes.
SELECT UnitOfMeasureDimension FROM I_UnitOfMeasureDimension INTO TABLE @DATA(dimensions) UP TO 10 ROWS.
LOOP AT dimensions INTO DATA(wa).
DATA(dim_inst) = cl_uom_dim_maintenance=>get_instance( ).
TYPES c6 TYPE c LENGTH 6.
TRY.
dim_inst->read( EXPORTING dimid = CONV c6( wa )
IMPORTING dim_st = DATA(dim_details) ).
LOOP AT CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( dim_details ) )->components INTO DATA(co).
"Applying string conversion to name and values for output purposes
"Note that many of the components have numeric types.
DATA(comp) = CONV string( co-name ).
DATA(value) = CONV string( dim_details-(comp) ).
"Outputting only those entries that do not have an initial value
IF value IS NOT INITIAL AND value NP `0*`.
out->write( data = value name = comp ).
ENDIF.
ENDLOOP.
CATCH cx_uom_error INTO DATA(read_err).
out->write( read_err->get_text( ) ).
ENDTRY.
out->write( repeat( val = `-` occ = 20 ) ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.
```
</td>
</tr>
<tr>
<td> <code>CL_UOM_MAINTENANCE</code> </td>
<td>
- Handling units of measurement
- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/238be94930874ed9ba3a3dc6469e99b3.html?locale=en-US) and in the class documentation.
- The example code snippet includes reading units of measurement. See the link above for the methods to create, change and delete dimensions.
<br><br>
```abap
DATA(unit_mea_inst) = cl_uom_maintenance=>get_instance( ).
TRY.
unit_mea_inst->read( EXPORTING unit = 'S'
IMPORTING unit_st = DATA(unit_mea) ).
DATA(unit) = unit_mea-unit.
DATA(comm) = unit_mea-commercial.
DATA(tech) = unit_mea-technical.
DATA(iso) = unit_mea-isocode.
DATA(id) = unit_mea-dimid.
DATA(text) = unit_mea-long_text.
CATCH cx_uom_error INTO DATA(unit_read_error).
DATA(error_msg) = unit_read_error->get_text( ).
ENDTRY.
"Note: Released view for units of measurement: I_UNITOFMEASURE.
```
</td>
</tr>
<tr>
<td> <code>CL_UOM_CONVERSION</code> </td>
<td>
- Converting units of measurement
- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/73109c66f397494abfa2bf3608740c12.html?locale=en-US) and in the class documentation.
- The example code snippet explores unit of measurement conversion. See the link above for more methods available.
<br><br>
```abap
DATA output TYPE decfloat34.
DATA(conv_unit_inst) = cl_uom_conversion=>create( ).
conv_unit_inst->unit_conversion_simple( EXPORTING input = CONV decfloat34( '1' )
round_sign = 'X'
unit_in = 'KG'
unit_out = 'G'
IMPORTING output = output
EXCEPTIONS conversion_not_found = 01
division_by_zero = 02
input_invalid = 03
output_invalid = 04
overflow = 05
units_missing = 06
unit_in_not_found = 07
unit_out_not_found = 08 ).
IF sy-subrc = 0.
"1000.000
DATA(outp) = output.
ELSE.
DATA(subrc) = sy-subrc.
ENDIF.
TYPES: BEGIN OF s,
input TYPE decfloat34,
round_sign TYPE c LENGTH 1,
unit_in TYPE cl_uom_conversion=>ty_msehi,
unit_out TYPE cl_uom_conversion=>ty_msehi,
END OF s,
tab_type TYPE TABLE OF s WITH EMPTY KEY.
DATA(tab4conv) = VALUE tab_type( ( input = '1.9876543210'
round_sign = '+' "rounding up
unit_in = 'KG'
unit_out = 'G' )
( input = '1.9876543210'
round_sign = '-' "rounding down
unit_in = 'KG'
unit_out = 'G' )
( input = '1987.6543210'
round_sign = ' ' "no rounding
unit_in = 'G'
unit_out = 'KG' )
( input = '60'
round_sign = 'X' "commercial
unit_in = 'MIN'
unit_out = 'H' )
( input = '1'
round_sign = 'X'
unit_in = 'TAG'
unit_out = 'H' )
( input = '1'
round_sign = 'X'
unit_in = 'JHR'
unit_out = 'TAG' )
( input = '123456'
round_sign = 'X'
unit_in = 'ABC'
unit_out = 'H' ) ).
LOOP AT tab4conv INTO DATA(conversion).
DATA(conv_inst) = cl_uom_conversion=>create( ).
conv_inst->unit_conversion_simple( EXPORTING input = conversion-input
round_sign = conversion-round_sign
unit_in = conversion-unit_in
unit_out = conversion-unit_out
IMPORTING output = output
EXCEPTIONS conversion_not_found = 01
division_by_zero = 02
input_invalid = 03
output_invalid = 04
overflow = 05
units_missing = 06
unit_in_not_found = 07
unit_out_not_found = 08 ).
IF sy-subrc = 0.
outp = output.
ELSE.
subrc = sy-subrc.
ENDIF.
ENDLOOP.
*Results:
*1987.655
*1987.654
*1.98765
*1.0
*24.0
*365.0
*7 (error, sy-subrc value)
"Retrieving mass- and time-related units of measurement using
"a released API
SELECT *
FROM i_unitofmeasure
WHERE unitofmeasuredimension = `TIME` OR unitofmeasuredimension = `MASS`
INTO TABLE @DATA(umea).
```
</td>
</tr>
</table>
<p align="right"><a href="#top">⬆️ back to top</a></p>