Skip to content

Commit ddeb59b

Browse files
authored
Update README.md
1 parent bc18234 commit ddeb59b

File tree

1 file changed

+88
-31
lines changed

1 file changed

+88
-31
lines changed

README.md

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,100 @@
11
# ABAP CSV Manager
22

3-
![Example](https://i.ibb.co/yfsSQZ7/csv-ex.png)
3+
![Example](/docs/img/cover.png)
4+
5+
## Requirement
6+
:cloud: ABAP for Cloud Development edition
47

58
## Quick Start
69

10+
:office_worker: **I need to read a CSV, how can I do it?**
11+
12+
:mage: Create a *reader* instance and call `read` method.
713
```abap
8-
* First, create an instance
9-
DATA(csv_man) = NEW ztbox_cl_csvman( ).
14+
DATA(csv_reader) = zcl_tbox_csvman=>get_reader( ).
15+
csv_reader->read(
16+
EXPORTING i_csv = csv_data
17+
IMPORTING e_table = tab_data ).
18+
```
1019

11-
* Now you can transform an internal table into a CSV string
12-
DATA(csv_str) = csv_man->create_csv( t_table_data ).
20+
:office_worker: **I need to create a CSV, how can I do it?**
1321

14-
* csv_str is a string, you can transform it into a table of strings
15-
DATA(csv_tab) = csv_man->to_table_string( csv_str ).
22+
:mage: Create a *writer* instance and call `write` method.
23+
```abap
24+
DATA(csv_writer) = zcl_tbox_csvman=>get_writer( ).
25+
DATA(csv_data) = csv_reader->write( tab_data ).
1626
17-
* To read a CSV file (imported as a table of strings):
18-
csv_man->read_csv( EXPORTING it_csv = csv_file CHANGING ct_table = t_table_data ).
27+
* csv_data is a string, if you need the output as a string table:
28+
DATA(csv_data) = csv_reader->write_to_string_table( tab_data ).
29+
```
30+
31+
### Some remarks about reading a CSV
32+
33+
When you call the `read` method of a *reader* instance,
34+
`csv_data` can be:
35+
- a string
36+
- a table of strings
37+
- a table with one-component table line
38+
39+
`tab_data` must be a *flat* table: a table whose table line is a structure, whose components are simple elements.
40+
41+
Sometimes you need to read a CSV but you don't know a priori how many and which columns it contains. In these cases you still have two ways to read it:
42+
43+
- you can get a reference variable to a table whose fields (of type string) are named F1, F2, ..., FN and so on;
44+
```abap
45+
DATA(csv_ref) = csv_reader->read_with_reference( csv_data ).
46+
47+
FIELD-SYMBOLS <tab> TYPE STANDARD TABLE.
48+
ASSIGN csv_ref->* TO <tab>.
49+
50+
LOOP AT <tab> ASSIGNING FIELD-SYMBOL(<row>).
51+
DATA(row_index) = sy-tabix.
52+
DO.
53+
ASSIGN COMPONENT sy-index OF STRUCTURE <row> TO FIELD-SYMBOL(<val>).
54+
IF sy-subrc <> 0.
55+
EXIT.
56+
ENDIF.
57+
DATA(info) = |The value at row { row_index } and column { sy-index } is { <val> }| ).
58+
ENDDO.
59+
ENDLOOP.
60+
61+
* You can always get a statically typed table using dereference method
62+
csv_reader->dereference( EXPORTING i_reference = csv_ref IMPORTING e_table = safe_data ).
63+
```
64+
65+
- or you can get a transposed version of the CSV, that is, a table with two fields: the row index and a table containing the list of its values, for each column.
66+
67+
```abap
68+
DATA(csv_tab_t) = csv_reader->read_with_transposition( csv_data ).
69+
70+
LOOP AT csv_tab_t INTO DATA(csv_row).
71+
LOOP AT csv_row-row INTO DATA(row_values).
72+
73+
DATA(info) = |The value at row { csv_row-row_index } and column { row_values-column_index } is { row_values-value }| ).
74+
75+
ENDLOOP.
76+
ENDLOOP.
77+
78+
* You can always get a standard table, if you have a suitable variable, using detranspose method
79+
csv_reader->detranspose( EXPORTING i_transposed = csv_tab_t IMPORTING e_table = safe_data ).
1980
```
2081

2182
## General Configuration
2283
:office_worker: **Can I decide which character to use as delimiter, quotechar, and line terminator? And what about escaping special characters?**
2384

2485
:mage: Once instantiated you can configure the csv management object:
25-
- ``csv_man->delimiter( `;` ).`` *fields delimiter, default is comma:* `,`*. Here "delimiter" is a synonym of "separator".*
26-
- ``csv_man->quotechar( `"` ).`` *to quote fields, default is none.*
86+
- ``csv_man->delimiter( `;` ).`` *fields delimiter, default is semicolon:* `;`*. Here "delimiter" is a synonym of "separator".*
87+
- ``csv_man->quotechar( `"` ).`` *to quote fields, default is none.
2788
- ``csv_man->end_of_line( `|` ).`` *line-terminator char, default is Carriage Return and Line Feed* `%_CR_LF` *.*
2889
- ``csv_man->escapechar( `/` ).`` *to escape special characters, both in read and write mode.*
2990
- ``csv_man->doublequote( abap_true ).`` *to escape a quotechar character with a quotechar character.*
3091
- `csv_man->quoting( ztbox_cl_csvman=>c_quote_minimal ).` *to restrict quoting application, with these options:*
31-
- `ztbox_cl_csvman=>c_quote_all` *to apply quotechar character to all fields (this is default behaviour if a quotechar is set);*
32-
- `ztbox_cl_csvman=>c_quote_minimal` *to apply quotechar character only to fields containing special characters;*
33-
- `ztbox_cl_csvman=>c_quote_nonnumeric` *to apply quotechar character only to non-numeric fields;*
34-
- `ztbox_cl_csvman=>c_quote_none` *to never quote fields (this is the default behaviour if no quotechar is set).*
35-
- `csv_man->header( abap_true ).` *to write/expect an header line in write/read mode.*
36-
- `csv_man->header_desc( abap_true ).` *to use long label description (from data element, in the log-on language) as header text field. If the field is not typed with a dictionary data element its name is still used as description.*
37-
- `csv_man->ingore_rows( int_range ).` *in read mode, ignore rows whose index is into parameter (it's a range of int)*
92+
- `zcl_tbox_csv_writer=>c_quote_all` *to apply quotechar character to all fields (this is default behaviour if a quotechar is set);*
93+
- `zcl_tbox_csv_writer=>c_quote_minimal` *to apply quotechar character only to fields containing special characters;*
94+
- `zcl_tbox_csv_writer=>c_quote_nonnumeric` *to apply quotechar character only to non-numeric fields;*
95+
- `zcl_tbox_csv_writer=>c_quote_none` *to never quote fields (this is the default behaviour if no quotechar is set).*
96+
- `csv_man->header( abap_true ).` *to write/expect an header line in write/read mode. Default is* `abap_true` *.*
97+
- `csv_man->header_desc( abap_true ).` *to use field name as header text field. You can column text by calling* `->label( )` *method on fields level, see below for details.*
3898

3999
## Output Format
40100
:office_worker: **Nice, but I want also control fields output format, especially for date/time/numeric fields.**
@@ -48,9 +108,8 @@ csv_man->read_csv( EXPORTING it_csv = csv_file CHANGING ct_table = t_table_data
48108
- `Y` *to output numbers like* `1 234 567,89`
49109
- ``csv_man->country( `US` ).`` *to output date, time and numbers according to a country rules (less specific than previous methods).*
50110
- `csv_man->decimals( 3 ).` *to write numerical fields with the specified decimals precision.*
51-
- `csv_man->convexit( abap_true ).` *to apply domain conversion exit, internal-to-external in write mode, external-to-internal in read mode. Default is* `abap_true`.
52-
- `csv_man->condense( abap_true ).` *to remove leading and trailing spaces. Default is* `abap_false`.
53-
- `csv_man->keep_init( abap_true ).` *to maintain initial values: if set to* `abap_false` *a numerical field containing only 0, as well as an initial date or initial time, became blank in write mode. Default is* `abap_true`.
111+
- `csv_man->condense_values( ).` *to remove leading and trailing spaces. Default is* `abap_false`.
112+
- `csv_man->keep_init( ).` *to maintain initial values: if set to* `abap_false` *a numerical field containing only 0, as well as an initial date or initial time, became blank in write mode. Default is* `abap_true`.
54113
- `csv_man->alignment( cl_abap_format=>a_right ).` *to align fields content according to the following options:*
55114
- `cl_abap_format=>a_left` *to justify text on the left (default option);*
56115
- `cl_abap_format=>a_right` *to justify text on the right.*
@@ -64,25 +123,25 @@ csv_man->read_csv( EXPORTING it_csv = csv_file CHANGING ct_table = t_table_data
64123
csv_man->field( `AMOUNT_USD` )->number_format( `X` ).
65124
csv_man->field( `AMOUNT_EUR` )->country( `IT` )->decimals( 2 ).
66125
```
67-
You can also exclude some fields from the CSV generation/reading process using `exclude( abap_true )` method:
126+
You can also exclude some fields from the CSV generation/reading process using `exclude( )` method:
68127

69128
```abap
70-
csv_man->field( `MANDT` )->exclude( abap_true ).
129+
csv_man->field( `MANDT` )->exclude( ).
71130
```
72131

73-
Viceversa, if you work with a table having too many fields, you can generate or reading a CSV considering only a small subset of fields using `include( abap_true )` method. Once you have called `include` for a field, only fields for which `include` has been called will be considered.
132+
Viceversa, if you work with a table having too many fields, you can generate or reading a CSV considering only a small subset of fields using `include( )` method. Once you have called `include` for a field, only fields for which `include` has been called will be considered.
74133

75134
```abap
76-
csv_man->field( `MATNR` )->include( abap_true ).
77-
csv_man->field( `WERKS` )->include( abap_true ).
135+
csv_man->field( `MATNR` )->include( ).
136+
csv_man->field( `WERKS` )->include( ).
78137
```
79138

80139
If the order of the fields in the table does not match the columns in the CSV to generate or read, you can map each field with the corresponding csv-column position:
81140

82141
```abap
83142
csv_man->header_desc( abap_true ).
84-
csv_man->field( `MATNR` )->csv_position( 2 )->label( `Material!!!` ).
85-
csv_man->field( `WERKS` )->csv_position( 1 )->label( `THE Plant` ).
143+
csv_man->field( `MATNR` )->position( 2 )->label( `Material!!!` ).
144+
csv_man->field( `WERKS` )->position( 1 )->label( `THE Plant` ).
86145
```
87146
In this way, the following table:
88147
| MATNR | WERKS |
@@ -129,7 +188,7 @@ And the `get_validation_fails( )` output is this table:
129188
You can add also custom validation checks: it must be an instance method with the following signature
130189

131190
```abap
132-
METHODS sample_check IMPORTING value TYPE string RETURNING VALUE(fail) TYPE flag.
191+
METHODS sample_check IMPORTING i_value TYPE string RETURNING VALUE(r_fail) TYPE flag.
133192
```
134193
If, e.g., an object `sample_object` implements method `sample_check`, you can add this check to a field:
135194

@@ -145,7 +204,5 @@ in two ways: as a *pre validation* by calling method `add_pre_validation( )`, an
145204

146205
`fail = abap_true` means the check has not been passed.
147206

148-
There are also two global defualt validations: a check verifying that CSV has the same number of fields for each row and a check for empty rows. These checks are applied only calling `check_csv( csv_tab )` method, where `csv_tab` is the CSV file as a table of strings.
149-
150207
## Installation
151208
Install this project using [abapGit](https://abapgit.org/) ![abapGit](https://docs.abapgit.org/img/favicon.png)

0 commit comments

Comments
 (0)