-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamic_Table_Data_Download_TXT_v1.0.abap
356 lines (276 loc) · 10.3 KB
/
Dynamic_Table_Data_Download_TXT_v1.0.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
*&---------------------------------------------------------------------*
*& Report ZTABLE_DATA_EXTRACVT_BTP
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_data.
* Program to extract data from table and prepare a file with field name
* Format patter (field name = value) just a convenient utility
* To copy paste from file to class method for BTP - data insert to table
* Actions and Activities
* Input table name
* Dynamic internal table prepared using the table name
* Table field list is retrieved using table name
* Respective field data is formatted and pushed to file
* With Multiple insert block for records - limitation in "value #" work area record definition range 1100 ~ 1250
* 250 charcter length line limit for file
TYPES : BEGIN OF gty_det,
tabname TYPE tabname,
fieldname TYPE fieldname,
index TYPE i,
END OF gty_det,
BEGIN OF gty_text,
text(2500) TYPE c,
END OF gty_text.
DATA : lt_table_filds TYPE TABLE OF dfies,
lt_dref TYPE REF TO data,
ls_dref TYPE REF TO data,
lt_dd30l TYPE STANDARD TABLE OF dd03l,
ls_dd30l TYPE dd03l,
lt_tbl_fld TYPE STANDARD TABLE OF gty_det,
ls_tbl_fld TYPE gty_det,
lv_variable_1(500) TYPE c, "char100,
lv_variable_2(2500) TYPE c,
lv_field TYPE char30,
lt_text TYPE STANDARD TABLE OF gty_text,
ls_text TYPE gty_text,
gv_table TYPE tabname16,
gv_path TYPE string,
lv_filename TYPE string,
lv_file TYPE string,
lv_path TYPE string,
lv_file_name TYPE string,
lv_count TYPE i,
lv_temp(2500) TYPE c,
lv_val_1 TYPE i,
lv_val_2 TYPE i,
lv_total TYPE i.
*** --- Field Symbols
FIELD-SYMBOLS : <fld_value> TYPE any.
FIELD-SYMBOLS : <fs_table> TYPE any.
FIELD-SYMBOLS : <ft_table> TYPE STANDARD TABLE.
FIELD-SYMBOLS : <dyn_field>.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-t01.
PARAMETER:rad1 RADIOBUTTON GROUP rad USER-COMMAND frad1 DEFAULT 'X',
p_table TYPE tabname16 MODIF ID aa,
rad2 RADIOBUTTON GROUP rad.
SELECT-OPTIONS s_table FOR gv_table NO INTERVALS MODIF ID bb.
PARAMETERS:sel_fold TYPE string OBLIGATORY MODIF ID bb.
SELECTION-SCREEN END OF BLOCK b1.
AT SELECTION-SCREEN OUTPUT.
IF rad1 = 'X'.
LOOP AT SCREEN.
IF screen-group1 = 'AA'.
screen-active = '1'.
MODIFY SCREEN.
ENDIF.
IF screen-group1 = 'BB'.
screen-active = '0'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ELSEIF rad2 = 'X'.
LOOP AT SCREEN.
IF screen-group1 = 'AA'.
screen-active = '0'.
MODIFY SCREEN.
ENDIF.
IF screen-group1 = 'BB'.
screen-active = '1'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR sel_fold.
CALL METHOD cl_gui_frontend_services=>directory_browse
EXPORTING
window_title = 'Select Directory'
initial_folder = 'c:\'
CHANGING
selected_folder = sel_fold
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE 'Directory selection Error' TYPE 'E'.
ENDIF.
START-OF-SELECTION.
* Create OBJECT lr_descr.
IF rad1 EQ abap_true.
PERFORM process USING p_table.
ELSEIF rad2 EQ abap_true.
* Loop the select options and pass the value inside the perform
gv_path = sel_fold.
DESCRIBE TABLE s_table LINES DATA(lv_lines).
LOOP AT s_table INTO DATA(ls_table).
gv_table = ls_table-low.
IF sy-tabix EQ lv_lines.
DATA(gv_flag) = abap_true.
ENDIF.
PERFORM process USING gv_table.
CLEAR : lv_filename, lv_file, ls_table, gv_table, lt_text, lv_count.
ENDLOOP.
ENDIF.
*&---------------------------------------------------------------------*
*& Form PROCESS
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> P_TABLE
*&---------------------------------------------------------------------*
FORM process USING pv_table TYPE tabname16.
*** --- Assigning fields symbols for Tables
CREATE DATA lt_dref TYPE TABLE OF (pv_table).
CREATE DATA ls_dref TYPE (pv_table).
*** --- Assign field symbol with table type of DDIC
ASSIGN lt_dref->* TO <ft_table>.
*** --- Assign field symbol with Structure type of DDIC
ASSIGN ls_dref->* TO <fs_table>.
SELECT *
FROM (pv_table)
INTO TABLE <ft_table>.
IF sy-subrc EQ 0.
SELECT tabname, fieldname, position FROM dd03l
INTO TABLE @DATA(lt_dd03l) WHERE tabname EQ @pv_table.
SORT lt_dd03l BY position ASCENDING. "Field order in the table
DELETE lt_dd03l WHERE fieldname = '.INCLU--AP' " append structure
OR fieldname = '.INCLUDE'. " include structure
LOOP AT lt_dd03l ASSIGNING FIELD-SYMBOL(<lfs_dd03l>).
<lfs_dd03l>-position = sy-tabix.
ENDLOOP.
DESCRIBE TABLE lt_dd03l LINES DATA(lv_lines).
LOOP AT <ft_table> ASSIGNING <fs_table>.
IF lv_count EQ 0.
ADD 1 TO lv_count.
* create new statement for record insert block
ls_text-text = |lt_| & |{ pv_table }| & | | & |= VALUE #(|.
APPEND ls_text TO lt_text.
CLEAR : ls_text.
ELSEIF lv_count GT 225.
* ELSEIF lv_count GT 1100.
* closing record insert block with 1100 records
CLEAR lv_count.
DATA(gv_end) = abap_true.
ELSE.
ADD 1 TO lv_count.
ENDIF.
DO lv_lines TIMES.
READ TABLE lt_dd03l INTO DATA(ls_fld) WITH KEY position = sy-index.
lv_field = ls_fld-fieldname.
ASSIGN COMPONENT lv_field OF STRUCTURE <fs_table> TO <fld_value>.
* DURING FIRST FIELD ASSIGNMENT
IF sy-index EQ 1.
lv_variable_1 = |(| & | | & |{ ls_fld-fieldname }| & | | & |=| & | | & |'| & |{ <fld_value> }| & |'|.
ENDIF.
* DURING OTHER MIDDLE FIELDS ASSIGNMENT
IF sy-index > 1.
lv_variable_1 = |{ ls_fld-fieldname }| & | | & |=| & | | & |'| & |{ <fld_value> }| & |'|.
ENDIF.
* DURING LAST FIELD ASSIGNMENT
IF sy-index EQ lv_lines.
CLEAR lv_variable_1.
lv_variable_1 = |{ ls_fld-fieldname }| & | | & |=| & | | & |'| & |{ <fld_value> }| & |'| & | | & |)|.
ENDIF.
CLEAR : lv_temp, lv_val_1, lv_val_2, lv_total.
lv_temp = lv_variable_2.
lv_val_1 = strlen( lv_temp ).
lv_val_2 = strlen( lv_variable_1 ).
lv_total = lv_val_1 + lv_val_2.
IF lv_total <= 250.
CONCATENATE lv_variable_2 lv_variable_1 INTO lv_variable_2 SEPARATED BY space.
ELSE.
CONCATENATE lv_variable_2 lv_variable_1 INTO lv_variable_2 SEPARATED BY space.
ls_text-text = lv_variable_2.
APPEND ls_text TO lt_text.
CLEAR : lv_variable_2, ls_text.
ENDIF.
CLEAR : ls_fld, lv_field, lv_variable_1.
ENDDO.
* FINAL TABLE PREPARED
ls_text-text = lv_variable_2.
APPEND ls_text TO lt_text.
IF gv_end EQ abap_true.
* RECORD INSERT BLOCK STATEMENT CLOSE
ls_text-text = |).|.
APPEND ls_text TO lt_text.
* BLANK LINE
ls_text-text = | |.
APPEND ls_text TO lt_text.
CLEAR : ls_text.
CLEAR gv_end.
ENDIF.
CLEAR : ls_text, lv_variable_2.
ENDLOOP.
* RECORD INSERT BLOCK STATEMENT CLOSE
ls_text-text = |).|.
APPEND ls_text TO lt_text.
lv_filename = pv_table.
IF rad1 EQ abap_true.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = 'Save Form'
default_extension = 'txt'
default_file_name = lv_filename
prompt_on_overwrite = 'X'
CHANGING
filename = lv_file_name
path = lv_path
fullpath = lv_file
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
invalid_default_file_name = 4
OTHERS = 5.
IF sy-subrc <> 0.
* Implement suitable error handling here
MESSAGE 'File Process Error' TYPE 'E'.
ENDIF.
ELSEIF rad2 EQ abap_true.
CONCATENATE gv_path '\' lv_filename '.txt' INTO lv_file.
ENDIF.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_file
filetype = 'ASC'
* write_field_separator = 'X'
TABLES
data_tab = lt_text
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc IS NOT INITIAL.
* Implement suitable error handling here
MESSAGE 'File Process Error' TYPE 'E'.
ENDIF.
ENDIF.
IF rad2 EQ abap_true
AND gv_flag EQ abap_true.
MESSAGE 'All files downloaded' TYPE 'I'.
ENDIF.
ENDFORM.