This commit is contained in:
danrega
2024-09-02 16:54:23 +02:00
parent f3dfa335e9
commit 367538af5e
10 changed files with 381 additions and 23 deletions

View File

@@ -1021,7 +1021,7 @@ itab = CORRESPONDING #( itab2 DISCARDING DUPLICATES ).
<td> Copying data from deep internal tables </td>
<td>
- A deep internal table is table with [deep](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendeep_glosry.htm) line type, which means the table can itself contain internal tables as components, among others.
- A deep internal table is a table with [deep](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendeep_glosry.htm) line type, which means the table can itself contain internal tables as components, among others.
- The `BASE` addition does not delete the existing content.
- See also the alternative `MOVE-CORRESPONDING` statements.

View File

@@ -10,7 +10,7 @@
- [SELECT List Variants](#select-list-variants)
- [Data Sources of SELECT Queries](#data-sources-of-select-queries)
- [Retrieving Single and Multiple Rows](#retrieving-single-and-multiple-rows)
- [Miscellaneous Options Regarding the Result](#miscellaneous-options-regarding-the-result)
- [Miscellaneous Options for the Selection Result](#miscellaneous-options-for-the-selection-result)
- [Additional Clauses](#additional-clauses)
- [Selecting Data by Evaluating the Content of Other Tables](#selecting-data-by-evaluating-the-content-of-other-tables)
- [Combining Data of Multiple Data Sources](#combining-data-of-multiple-data-sources)
@@ -422,7 +422,7 @@ SELECT DISTINCT comp1
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Miscellaneous Options Regarding the Result
### Miscellaneous Options for the Selection Result
This section demonstrates different patterns of the ABAP SQL `SELECT` statement for handling query results. There is a wide range of options available, some of which have already been covered above. This section covers a selection of additions and syntax options. For complete details and syntax options, you can refer to the ABAP Keyword Documentation.
@@ -2194,6 +2194,7 @@ SELECT SINGLE * FROM dbtab WHERE key_field = ... INTO @DATA(read_line).
"original content and are not initialized when writing to the database table.
UPDATE dbtab FROM @( VALUE #( BASE read_line comp2 = ... comp4 = ... ) ).
"--- CORRESPONDING ---
"The following example assumes that some_itab has a different line type than dbtab.
"I.e. some_itab may have more components that are not available in dbtab. The
"corresponding fields with identical names are used. It is assumed that the components'
@@ -2546,10 +2547,10 @@ ENDCLASS.
"SELECT * FROM zdemo_abap_carr WHERE mandt = '123' INTO TABLE @DATA(itab2).
```
- However, in classic ABAP, not in ABAP Cloud, you can use the [`USING CLIENT` (F1 docu for Standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapselect_client.htm) addition modifies this default behavior. It disables implicit client handling.
- However, in classic ABAP, not in ABAP Cloud, you can use the [`USING CLIENT` (F1 docu for Standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapselect_client.htm) addition that modifies this default behavior. It disables implicit client handling.
- With the addition, you can specify other clients from which to retrieve data. Find more information in the ABAP Keyword Documentation.
- You may also stumble on the `CLIENT SPECIFIED` addition. It is obsolete, only `USING CLIENT` should be used.
- The following (classic ABAP only) code shows a variety of syntax options. Note that it uses a data source of the cheat sheet repository. For exploration, you may want to use another data source. You may also want to check the commands that are passed by the [ABAP SQL Interface](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_sql_interface_glosry.htm) when activating the SQL trace (transaction ST05) to the explore the use of another client ID and more.
- The following (classic ABAP only) code shows a variety of syntax options. Note that it uses a data source of the cheat sheet repository. For exploration, you may want to use another data source. You may also want to check the commands that are passed by the [ABAP SQL Interface](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_sql_interface_glosry.htm) when activating the SQL trace (transaction ST05) to explore the use of another client ID and more.
```abap
"---- The following code is for classic ABAP only. ----

View File

@@ -1024,6 +1024,7 @@ DATA(oref_b) = REF #( oref_a ).
)->meth( ... )`). Method chaining, standalone and
functional method calls are possible, too. See more information
[here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconstructor_expression_cast.htm).
- Find more examples on using the `CAST` operator, up- and downcasts in the [ABAP Object Orientation](04_ABAP_Object_Orientation.md#demonstrating-upcasts-and-downcasts-using-the-rtts-inheritance-tree) cheat sheet.
Examples:
``` abap
@@ -1636,7 +1637,7 @@ DATA(it5) = VALUE itab_type( FOR wa IN itab ( VALUE #( BASE wa col1 = wa-col1 &&
*bby 5 10
*cccy 6 20
"Using the CORRESPONDING operator to handle different types
"Using the CORRESPONDING operator to handle types whose components have non-identical names
TYPES: BEGIN OF s2,
col1 TYPE c LENGTH 5,
col2 TYPE i,

View File

@@ -507,7 +507,7 @@ DATA(len_xstr) = xstrlen( xstr ). "24
`&&` and string templates. Alternatively, you can use
[`CONCATENATE`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapconcatenate.htm)
statements.
- It is also possible to concatenate lines from internal tables
- It is also possible to concatenate lines from internal tables with character-like line type
into a string to avoid a loop.
- A more modern way is to use
the string function
@@ -827,6 +827,7 @@ and
- These functions offer more options in terms of parameters, such as the use of [PCRE regular
expressions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenpcre_regex_glosry.htm "Glossary Entry"),
which are covered below.
- As also shown further down, using the built-in function `match`, you can extract substrings matching a given pattern.
Syntax examples:
``` abap
@@ -904,6 +905,12 @@ s2 = substring_from( val = s1 sub = `a3` ). "a3bb4
"... up to a specified substring. It includes the substring specified
"in sub. len/off and other parameters are possible.
s2 = substring_to( val = s1 sub = `3b` ). "aa1bb2aa3b
"Extracting a substring matching a given pattern
"AB
s2 = match( val = `ABAP`
pcre = `.b` "Any character followed by b/B
case = abap_false ).
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
@@ -1942,7 +1949,7 @@ REPLACE ALL OCCURRENCES OF PCRE `p.` IN s1 WITH `XY` "XY?aXY#ab?a
#### Overview/Examples: Using PCRE Regular Expressions in Various Contexts
- As also covered in other sections, there are multiple contexts where regular expressions (PCRE) are possible, among them:
- As also covered in other sections, there are multiple contexts where regular expressions are possible, for example, PCRE regular expressions. Among them are:
- Statements (`PCRE` addition): `FIND`, `REPLACE`
- Classes: `CL_ABAP_REGEX`, `CL_ABAP_MATCHER`
- Built-in functions having the `pcre` parameter: `find`, `find_end`, `count`, `match`, `replace`, `substring_from`, `substring_after`, `substring_before`, `substring_to`
@@ -2001,13 +2008,13 @@ some_string = original_string.
DATA(replace_all_occ) = replace( val = some_string pcre = `\s` with = `#` occ = 0 ).
"The following examples always use the first occurrence
"' bb cc dd ee'
"` bb cc dd ee`
DATA(substr_from) = substring_from( val = some_string pcre = `\s` ).
"'aa '
"`aa `
DATA(substr_to) = substring_to( val = some_string pcre = `\s` ).
"'aa'
"`aa`
DATA(substr_before) = substring_before( val = some_string pcre = `\s` ).
"bb cc dd ee

View File

@@ -14,10 +14,10 @@
- [Type Mapping for RAP](#type-mapping-for-rap)
- [RAP-Specific Additions to the CORRESPONDING Operator](#rap-specific-additions-to-the-corresponding-operator)
- [ABAP SQL Statements with BDEF Derived Types](#abap-sql-statements-with-bdef-derived-types)
- [EML Syntax](#eml-syntax)
- [EML Syntax for Modifying Operations](#eml-syntax-for-modifying-operations)
- [EML Syntax for Reading Operations](#eml-syntax-for-reading-operations)
- [Dynamic Forms of EML Statements](#dynamic-forms-of-eml-statements)
- [ABAP EML Syntax](#abap-eml-syntax)
- [ABAP EML Syntax for Modifying Operations](#abap-eml-syntax-for-modifying-operations)
- [ABAP EML Syntax for Reading Operations](#abap-eml-syntax-for-reading-operations)
- [Dynamic Forms of ABAP EML Statements](#dynamic-forms-of-abap-eml-statements)
- [Persisting to the Database](#persisting-to-the-database)
- [Raising RAP Business Events](#raising-rap-business-events)
- [Additions to EML Statements in ABAP Behavior Pools](#additions-to-eml-statements-in-abap-behavior-pools)
@@ -482,6 +482,9 @@ sequence](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?f
methods](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabp_saver_method_glosry.htm "Glossary Entry")
to save data from the transactional buffer to the database).
> **💡 Note**<br>
> You can optionally define auxiliary classes for ABPs using the syntax `... FOR BEHAVIOR OF bdef_name ...`. These classes reference a RAP business object and have enhanced access rights to it. Their main function is to support implementation in an ABAP behavior pool. They achieve this by offering reusable methods and enabling developers to implement RAP behavior simultaneously.
<p align="right"><a href="#top">⬆️ back to top</a></p>
### RAP Handler Classes and Methods
@@ -1041,14 +1044,14 @@ DELETE zdemo_abap_rapt1 FROM @cr_der_type MAPPING FROM ENTITY.
<p align="right"><a href="#top">⬆️ back to top</a></p>
## EML Syntax
## ABAP EML Syntax
The focus is here on selected EML statements. These statements can be
fairly long and various additions are possible. Find more information on
the EML statements
[here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abeneml.htm).
### EML Syntax for Modifying Operations
### ABAP EML Syntax for Modifying Operations
The modifying operations covered include the standard operations (using
the additions
@@ -1311,7 +1314,7 @@ MODIFY ENTITIES OF root_ent
<p align="right"><a href="#top">⬆️ back to top</a></p>
### EML Syntax for Reading Operations
### ABAP EML Syntax for Reading Operations
- Read-only operations always return a result, i.e. the syntax of the
EML statement requires the addition `RESULT` and an operand.
@@ -1372,7 +1375,7 @@ READ ENTITIES OF root_ent
```
<p align="right"><a href="#top">⬆️ back to top</a></p>
### Dynamic Forms of EML Statements
### Dynamic Forms of ABAP EML Statements
In addition to the short and long forms described above, various ABAP EML statements also have dynamic forms.
Taking EML read operations as an example, the following code snippet shows a dynamic EML [`READ ENTITIES`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapread_entities_operations.htm) statement. The relevant syntax element is the `OPERATIONS` addition.

View File

@@ -852,7 +852,7 @@ Special function modules exist in [Standard ABAP](https://help.sap.com/doc/abapd
Regarding the exiting of procedures, note the hint mentioned above. The use of `RETURN` is recommended.
`RETURN` terminates the current processing block. Usually, the statement is intended for leaving processing blocks early.
In case of functional method, i.e. methods that have one returning parameter, the `RETURN` statement can also be specified with an expression. In doing so, the
In case of functional methods, i.e. methods that have one returning parameter, the `RETURN` statement can also be specified with an expression. In doing so, the
following statement
```abap
res = some_expr.

View File

@@ -1501,6 +1501,18 @@ dobj_enum_a = a.
"Data object declared inline, the typed derived is t_enum
DATA(dobj_enum_b) = b.
"Note: The technical data type of an enumerated value is the base type
"of the enumerated type. You can use the base type of an enumerated type
"in special conversions using CONV. A base type is flat, elementary, and
"has a maximum length of 16 bytes.
DATA some_dobj TYPE c LENGTH 2 VALUE 'ap'.
DATA(dobj_enum_c) = CONV t_enum_struc( some_dobj ).
ASSERT dobj_enum_c = en_struc-m.
"Conversion not possible
DATA some_string TYPE string VALUE 'ap'.
"DATA(dobj_enum_d) = CONV t_enum_struc( some_string ).
```
Find more information on enumerated types in the (commented code of the) cheat sheet example and [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenenumerated_types_usage.htm).

View File

@@ -5,7 +5,7 @@
- [Selection Screens and Classic Lists](#selection-screens-and-classic-lists)
- [Introduction](#introduction)
- [Terms](#terms)
- [Selection screens](#selection-screens)
- [Selection Screens](#selection-screens)
- [Classic Lists](#classic-lists)
- [ABAP Statements for Selection Screens](#abap-statements-for-selection-screens)
- [Creating Selection Screens](#creating-selection-screens)
@@ -50,7 +50,7 @@ For more detailed information and syntax options, see the topics [Selection Scre
## Terms
### Selection screens
### Selection Screens
- Are special [dynpros](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abendynpro_glosry.htm) in [executable programs](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenexecutable_program_glosry.htm) (*"reports"*; they're also possible in [function groups](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenfunction_group_glosry.htm) and [module pools](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenmodul_pool_glosry.htm), but the focus here is on executable programs).
- Used for data entry in an executable program, i.e. they allow users to ...
- enter parameters (for entering single values).

View File

@@ -30,6 +30,7 @@
- [Locking](#locking)
- [Calling Services](#calling-services)
- [Reading and Writing XLSX Content](#reading-and-writing-xlsx-content)
- [Zip Files](#zip-files)
This ABAP cheat sheet contains a selection of available ABAP classes, serving as a quick introduction, along with code snippets to explore the functionality in action.
@@ -2389,6 +2390,299 @@ The latter can be created, among others, based on a communication arrangement or
<li>For more information, refer to the class documentation and the topic <a href="https://help.sap.com/docs/btp/sap-business-technology-platform/integration-and-connectivity">Integration and Connectivity</a>.</li>
</ul>
To check out examples in demo classes, expand the collapsible sections below.
<details>
<summary>1. Read example: Getting Markdown content and sending ZIP file via email</summary>
<!-- -->
> **⚠️ Note/Disclaimer**<br>
> - The following self-contained and oversimplified example is not a representative best practice example, nor does it cover a meaningful use case. It only explores method calls and is intended to give a rough idea of the functionality.</li>
> - The example uses the <code>create_by_url</code> method, which is only suitable for public services or testing purposes. No authentication is required for the APIs used.
> - Note the <a href="README.md#%EF%B8%8F-disclaimer">Disclaimer</a>.</li>
> - For more information, more meaningful examples, and tutorials that deal with the classes and methods, see the following links:
> - <a href="https://developers.sap.com/tutorials/abap-environment-external-api.html">Call an External API and Parse the Response in SAP BTP ABAP Environment</a>
> - <a href="https://community.sap.com/t5/technology-blogs-by-sap/how-to-call-a-remote-odata-service-from-the-trial-version-of-sap-cloud/ba-p/13411535">How to call a remote OData service from the trial version of SAP Cloud Platform ABAP environment</a>
> - The example is generally about calling external APIs and parsing the HTTP responses. It retrieves the Markdown files of the ABAP cheat sheet documents Markdown contained in the ABAP cheat sheet GitHub repository.
> - Before using the GitHub APIs, make sure that you have consulted the following documentation: <a href="https://docs.github.com/en">GitHub Docs</a>, <a href="https://docs.github.com/en/enterprise-cloud@latest/rest/markdown/markdown?apiVersion=2022-11-28#render-a-markdown-document">Render a Markdown document</a>, <a href="https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28">Rate limits for the REST API</a>
> - For the example to work and send emails, make sure that the configurations from [here](https://help.sap.com/docs/btp/sap-business-technology-platform/emailing) have been performed.
> - To run the example class, copy and paste the code into a class named `zcl_some_class`. Run the class using F9. The email sending status will be displayed, and you can expect an email to be sent.
<br>
``` abap
CLASS zcl_some_class DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
CONSTANTS url_cs TYPE string VALUE `https://api.github.com/repos/SAP-samples/abap-cheat-sheets/git/trees/main`.
CONSTANTS url_gh TYPE string VALUE `https://raw.githubusercontent.com/SAP-samples/abap-cheat-sheets/main/`.
CONSTANTS url_api TYPE string VALUE `https://api.github.com/markdown`.
DATA url TYPE string.
TYPES: BEGIN OF s,
file_name TYPE string,
title TYPE string,
code_snippets TYPE string_table,
error TYPE abap_boolean,
END OF s.
DATA tab TYPE TABLE OF s WITH EMPTY KEY.
DATA snippets TYPE string_table.
DATA html TYPE string.
ENDCLASS.
CLASS zcl_some_class IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TRY.
"Creating a client object using a destination
"In the example, the HTTP destination is created using a plain URL.
"Here, a GitHub API is used to retrieve file names of the ABAP cheat sheet repository.
DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( i_destination = cl_http_destination_provider=>create_by_url( i_url = url_cs ) ).
"Sending an HTTP GET request and returning the response
"In the example, the HTTP body is retrieved as string data.
DATA(response) = http_client->execute( if_web_http_client=>get )->get_text( ).
CATCH cx_root INTO DATA(err).
out->write( err->get_text( ) ).
ENDTRY.
IF err IS INITIAL.
"Markdown file names are contained in the returned string in a specific
"pattern. In the following code, the markdown file names are extracted
"using a regular expression (pattern: "path":"04_ABAP_Object_Orientation.md")
"After '"path":"' (not including this part, indivated by \K), two
"digits must follow. Then, the further file name is captured with a
"non-greedy capturing up to '.md'.
FIND ALL OCCURRENCES OF PCRE `("path":")\K\d\d.*?\.md` IN response
RESULTS DATA(results)
IGNORING CASE.
"The 'results' internal table contains all findings and includes their
"offset and length information.
"Using a loop, the actual file names are extracted from the 'response'
"string and added to an internal table that is to receive more information
"in the code below.
LOOP AT results REFERENCE INTO DATA(md).
tab = VALUE #( BASE tab ( file_name = substring( val = response off = md->offset len = md->length ) ) ).
ENDLOOP.
SORT tab BY file_name ASCENDING.
"In the following loop, the raw markdown content is retrieved using an HTTP GET request, also
"by creating a client object and using a destination (another plain URL). The URL is constructed
"using the constant value plus the markdown file that was retrieved before.
LOOP AT tab REFERENCE INTO DATA(cs).
url = url_gh && cs->file_name.
TRY.
http_client = cl_web_http_client_manager=>create_by_http_destination( i_destination = cl_http_destination_provider=>create_by_url( i_url = url ) ).
DATA(raw_md) = http_client->execute( if_web_http_client=>get )->get_text( ).
"Putting the long string that was retrieved in an internal table of type string
"for further processing (extracting the code snippets).
SPLIT raw_md AT |\n| INTO TABLE snippets.
DATA(flag) = ''.
"In the loop, all content from the markdown that is not part of a code
"snippet (indicated by the triple ```) is deleted.
"The replacements with dummy content in the loop are only done so that
"the POST request further down can work with the provided content
"(i.e. avoiding issues with characters such as "; they are inserted later again).
LOOP AT snippets REFERENCE INTO DATA(line).
DATA(tabix) = sy-tabix.
FIND PCRE '^\s*```' IN line->*.
IF sy-subrc = 0 AND flag = ''.
line->* = `%%%--START--%%%%`.
flag = 'X'.
ELSEIF sy-subrc = 0 AND flag = 'X'.
line->* = `%%%--END--%%%%`.
flag = ''.
ELSEIF flag <> 'X'.
DELETE snippets INDEX tabix.
ELSE.
FIND PCRE `^\s*"` IN line->*.
IF sy-subrc = 0.
DATA(comment1) = 'X'.
ENDIF.
FIND PCRE `^\*` IN line->*.
IF sy-subrc = 0.
DATA(comment2) = 'X'.
ENDIF.
FIND `***********************************************************************`
IN line->*.
IF sy-subrc = 0.
DATA(divider) = 'X'.
ENDIF.
IF comment1 = 'X' OR comment2 = 'X' OR divider = 'X'.
DELETE snippets INDEX tabix.
CLEAR: comment1, comment2, divider.
ELSE.
REPLACE ALL OCCURRENCES OF `"` IN line->* WITH `§§§§§`.
REPLACE ALL OCCURRENCES OF `\` IN line->* WITH `%%%%%`.
ENDIF.
ENDIF.
ENDLOOP.
"Adding the code snippets to the information table
cs->code_snippets = snippets.
CLEAR snippets.
CATCH cx_root INTO err.
cs->error = abap_true.
ENDTRY.
DELETE ADJACENT DUPLICATES FROM cs->code_snippets COMPARING table_line.
ENDLOOP.
"Creating the final html to be displayed
LOOP AT tab REFERENCE INTO cs WHERE code_snippets IS NOT INITIAL AND error = abap_false.
LOOP AT cs->code_snippets REFERENCE INTO DATA(code).
tabix = sy-tabix.
IF code->* = `%%%--START--%%%%`.
code->* = |```|.
ENDIF.
IF code->* = `%%%--END--%%%%`.
code->* = |```|.
INSERT `*****************` && |\\n|
INTO cs->code_snippets INDEX tabix + 1.
ENDIF.
code->* = code->* && |\\n|.
ENDLOOP.
"For the POST request, concatenating the string table to a single string.
DATA(code_string) = concat_lines_of( table = cs->code_snippets ).
TRY.
"Another creation of a client object using a destination
"This example deals with a POST request.
http_client = cl_web_http_client_manager=>create_by_http_destination( i_destination = cl_http_destination_provider=>create_by_url( i_url = url_api ) ).
DATA(request) = http_client->get_http_request( ).
request->set_text( `{"text":"` && code_string && `"}` ).
request->set_header_fields( VALUE #( ( name = 'Accept' value = 'application/vnd.github+json' ) ) ).
DATA(post) = http_client->execute( if_web_http_client=>post ).
DATA(status) = post->get_status( ).
IF status-code <> 200.
cs->error = abap_true.
DATA(status_error) = |Post request error: { status-code } / { status-reason }|.
ELSE.
"Retrieving the created html code
DATA(html_code) = post->get_text( ).
REPLACE ALL OCCURRENCES OF `§§§§§` IN html_code WITH `"`.
REPLACE ALL OCCURRENCES OF `%%%%%` IN html_code WITH `\`.
REPLACE ALL OCCURRENCES OF PCRE `(<code>)(\w.*)` IN html_code WITH `$1 $2`.
ENDIF.
CATCH cx_root INTO DATA(error).
cs->error = abap_true.
ENDTRY.
"Preparing the title for expandable sections
DATA(title) = cs->file_name.
REPLACE ALL OCCURRENCES OF `_` IN title WITH ` `.
REPLACE PCRE `^..` IN title WITH ``.
REPLACE `.md` IN title WITH ``.
"Assembling expandable sections
html = html &&
`<br><details>` &&
` <summary>` && title && `</summary>` &&
COND #( WHEN cs->error = abap_false THEN html_code ELSE COND #( WHEN error IS INITIAL THEN status_error ELSE error->get_text( ) ) ) &&
`</details>`.
ENDLOOP.
"Providing the html skeleton and inserting the assembled expandable sections from above
DATA(final_html) =
`<!DOCTYPE html>` &&
`<html>` &&
`<head>` &&
`<title>ABAP Cheat Sheet Code Snippets</title>` &&
`<style>` &&
` body {background-color: #F8F8F8;}` &&
` h1 {color: blue; font-family: verdana;}` &&
` pre {background: #f4f4f4;border: 1px solid #ddd;border-left: 3px solid #0070f2;color: #36454F;` &&
` page-break-inside: avoid;font-size: 14px;line-height: 1.3;max-width: 100%;overflow: auto;padding: 1em 1.5em;` &&
` display: block;word-wrap: break-word;} ` &&
`</style>` &&
`</head>` &&
`<body>` &&
`<h1>ABAP Cheat Sheet Code Snippets</h1>` &&
`<a href="https://github.com/SAP-samples/abap-cheat-sheets">https://github.com/SAP-samples/abap-cheat-sheets</a><br><br>` &&
html &&
`<script>` &&
` const snippets = document.querySelectorAll("code");` &&
` snippets.forEach(elem => {` &&
` var abap = elem.innerHTML;` &&
` abap = abap.replace(/(\b[A-Z]{2,}\b)/g, "<strong>$1</strong>");` &&
` elem.innerHTML = abap;` &&
` });` &&
`</script>` &&
`</body>` &&
`</html>`.
"Displaying the html result in the ADT console
"Note: Before running the class, clear the ADT console.
"When the html code is displayed in the ADT console, you can, for example,
"create a file named ABAP_cheat_sheet_code.html on your local machine.
"Open the file in an editor, copy & paste the entire ADT console content and
"save the local file. In doing so, you have various code snippets at your
"disposal offline.
out->write( final_html ).
ENDIF.
ENDMETHOD.
ENDCLASS.
```
</details>
<br>
<details>
<summary>2. Post example: Demonstrating a post request by converting Markdown to HTML using the GitHub API</summary>
<!-- -->
> **⚠️ Note/Disclaimer**<br>
> - As stated for the previous example, also note for this example: Before using the GitHub APIs, make sure that you have consulted the following documentation: <a href="https://docs.github.com/en">GitHub Docs</a>, <a href="https://docs.github.com/en/enterprise-cloud@latest/rest/markdown/markdown?apiVersion=2022-11-28#render-a-markdown-document">Render a Markdown document</a>, <a href="https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28">Rate limits for the REST API</a>
> - To run the example class, copy and paste the code into a class named `zcl_some_class`. Run the class using F9. It is set up to display HTML content in the console. Using the GitHub API, sample Markdown content is sent and converted to HTML.
<br>
``` abap
CLASS zcl_some_class DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
CONSTANTS url_api TYPE string VALUE `https://api.github.com/markdown`.
ENDCLASS.
CLASS zcl_some_class IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA(nl) = |\n|.
DATA(markdown_content) =
`# Lorem ipsum dolor sit amet \n` &&
`Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \n` &&
`Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \n` &&
`- Duis aute irure \n` &&
`- Dolor in reprehenderit in voluptate \n` &&
`1. Velit esse cillum dolore eu fugiat nulla pariatur \n` &&
`2. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \n` &&
`3. [ABAP cheat sheets](https://github.com/SAP-samples/abap-cheat-sheets)`.
TRY.
"Creation of a client object using a destination
"This example deals with a POST request.
DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( i_destination = cl_http_destination_provider=>create_by_url( i_url = url_api ) ).
DATA(request) = http_client->get_http_request( ).
request->set_text( `{"text":"` && markdown_content && `"}` ).
request->set_header_fields( VALUE #( ( name = 'Accept' value = 'application/vnd.github+json' ) ) ).
DATA(post) = http_client->execute( if_web_http_client=>post ).
DATA(status) = post->get_status( ).
IF status-code <> 200.
out->write( |Post request error: { status-code } / { status-reason }| ).
ELSE.
DATA(html) = post->get_text( ).
out->write( html ).
ENDIF.
CATCH cx_root INTO DATA(error).
out->write( error->get_text( ) ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
```
</details>
</td>
</tr>
@@ -2987,4 +3281,44 @@ ENDCLASS.
</tr>
</table>
<p align="right"><a href="#top">⬆️ back to top</a></p>
## Zip Files
<table>
<tr>
<td> Class </td> <td> Details/Code Snippet </td>
</tr>
<tr>
<td> <code>CL_ABAP_ZIP</code> </td>
<td>
The following example creates a zip file and adds three txt files with sample content.
```abap
"Create zip file
DATA(zip) = NEW cl_abap_zip( ).
"Adding 3 files to a zip file
DO 3 TIMES.
DATA(some_content) = |{ sy-index }. Some text content|.
TRY.
DATA(conv_xstring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( some_content ).
CATCH cx_sy_conversion_codepage.
ENDTRY.
"Add xstring as file content to zip
zip->add( EXPORTING name = |file{ sy-index }.txt|
content = conv_xstring ).
ENDDO.
DATA(zipped_file) = zip->save( ).
```
</td>
</tr>
</table>
<p align="right"><a href="#top">⬆️ back to top</a></p>

View File

@@ -985,7 +985,7 @@ DATA(shift_right4) = shift_right( val = to_be_shifted ).
</tr>
<tr>
<td> <code>substring</code><br><code>substring_after</code><br><code>substring_before</code><br><code>substring_to</code> </td>
<td> <code>substring</code><br><code>substring_after</code><br><code>substring_before</code><br><code>substring_to</code><br><code>substring_from</code> </td>
<td>
Processing substrings
<br><br>