-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAM_FILL_71224.sql
1885 lines (1863 loc) · 71.1 KB
/
AM_FILL_71224.sql
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create or replace PACKAGE "AM_FILL" as
/*
* Using the Microsoft XLSX files as a templates for data output
*
* Require AS_ZIP package designed by Anton Scheffer
* Download from: http://technology.amis.nl/wp-content/uploads/2010/06/as_zip7.txt
*
* Based on the code of packages designed by Anton Scheffer
* https://technology.amis.nl/wp-content/uploads/2011/02/as_xlsx11.txt
* https://technology.amis.nl/wp-content/uploads/2013/01/as_read_xlsx9.txt
*
* Author: miktim@mail.ru, Petrozavodsk State University
* Date: 2013-06-24
* Updated:
* 2017-12-13 changed:
* active sheet detection, sheet names,
* in_table, in_sheet, new_workbook, address,...
* 2017-12-12 added support for formulas
* Thanks github.com/Fynjy1984
* 2017-03-28 fixed bug: 200 rows 'limitation' (IN_TABLE),
* options added (IN_SHEET),
* 2016-06-08 fixed bug: calc new rId (IN_SHEET)
* 2016-06-07 fixed bugs: get sheet xml name, sheets without merges (align_loc)
* Thanks github.com/Zulus88
* 2016-02-01 free xmlDocument objects
* 2015-03-18 procedure in_sheet added
* 2015-02-27 support Oracle non UTF-8 charSets.
******************************************************************************
* Copyright (C) 2011 - 2013 Anton Scheffer (as_xlsx, as_read_xlsx, as_zip)
* 2013 - 2017 MikTim
* License: MIT
******************************************************************************
*/
version constant varchar2(10):='71224';
/*
data_error EXCEPTION messages:
#WORKBOOK! unknown structure
#SHEET!...
#REF!... sheet or named range not found (1),
aligning in merged cells (2),
vertically merged cells within rows insert mode (3).
*/
data_error EXCEPTION;
PRAGMA EXCEPTION_INIT(data_error, -20711);
/*
INIT: Initialize package by xlsx template
p_options: case sensitive
e - enable exception on #REF! (1-3),
otherwise ignore filling (1), cut merged cells (2-3)
d - replace the common style for DATEs by user-defined or workbook default
n - replace NULLs with original value from sheet
*/
Procedure init
( p_xtemplate BLOB -- xlsx template BLOB
, p_options varchar2:=''
);
/* INIT: Clear internal structures */
Procedure init;
/*
IN_FIELD: Fill in cell. Destination sheet becomes active (current).
p_value: date, number, string or formula:
'=SUM(A1:A12)' - formula
'''=SUM(A1:A12)' - string
WARNING: formula values can become unpredictable in row insertion mode
p_address: relative A1 style cell address.
You can not enclose in single quotes sheet names without !$
Exаmples:
am_fill.in_field(123, 'First sheet!a12');
am_fill.in_field('string', 'Named_Area!A12);
am_fill.in_field(sysdate, 'Named_Area');
am_fill.in_field(null, 'A11');
p_options:
i - row insert mode (sequentially on every call), default - overwrite
*/
Procedure in_field
( p_value date
, p_address varchar2
, p_options varchar2:='');
Procedure in_field
( p_value number
, p_address varchar2
, p_options varchar2:='');
Procedure in_field
( p_value varchar2
, p_address varchar2
, p_options varchar2:='');
/*
IN_TABLE: Fill in table
p_table: ref_cursor or sql query text (without trailing semicolon)
p_address: cell address for first field (see IN_FIELD)
p_options:
h - print headings (field names)
i - rows insert mode (see INIT e-option).
*/
Type ref_cursor is REF CURSOR;
Procedure in_table
( p_table in out ref_cursor
, p_address varchar2
, p_options varchar2 := '');
Procedure in_table
( p_table CLOB
, p_address varchar2
, p_options varchar2 := '');
/*
IN_SHEET: Save FILLED sheet with new name AFTER source sheet,
clears data from source sheet, new sheet becomes active and visible.
WARNING:
all data, except numbers, strings, dates,formulas, will be REMOVED from the new sheet
p_sheet_name: filled sheet name
p_newsheet_name: max 31 char, cannot contain /\*[]:?'
If is null, visibility of the source sheet will change
p_options:
h - hide source sheet
b - insert BEFORE source sheet
*/
Procedure in_sheet
( p_sheet_name varchar2
, p_newsheet_name varchar2:=''
, p_options varchar2:=''
);
/*
FINISH: Generate workbook. Save FILLED sheets, clear internal structures.
Named ranges in the saved sheets are corrected (moved and extended).
WARNING:
all data, except numbers, strings, dates, formulas, will be REMOVED from saved sheets
*/
Procedure finish
( p_xfile in out nocopy BLOB -- filled in xlsx returns
);
/*
ADDRESS: calculates and aligns new cell address,
returns A1 style cell address or null on #REF!(1).
p_address - cell address (see IN_FIELD).
p_options: case sensitive
l - align to left cell in merge
t - align to top cell in merge
R - move outside merge in row then align
B - move outside merge in column then align
*/
Function address
( p_row pls_integer -- from 1
, p_col pls_integer -- from 1
, p_address varchar2
, p_options varchar2 := ''
) return varchar2;
/*
NEW_WORKBOOK: returns workbook with two sheets:
'Sheet1' - visible.
'Sheet0' - hidden. A1 cell formatted as date (YYYY-MM-DD)
*/
Function new_workbook return BLOB;
/* Cell location */
/*
Type tp_location is record
( sht_nr pls_integer := null
, row_nr pls_integer := null
, col_nr pls_integer := null
);
Function location(p_address varchar2) return tp_location;
Function address(p_location tp_location) return varchar2;
Procedure in_field
( p_value date
, p_location tp_location
, p_options varchar2:='');
Procedure in_field
( p_value number
, p_location tp_location
, p_options varchar2:='');
Procedure in_field
( p_value varchar2
, p_location tp_location
, p_options varchar2:='');
*/
end;
/
create or replace PACKAGE BODY "AM_FILL" is
c_errcode constant number := -20711;
c_sheeturl constant varchar2(200) := 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
c_sheetns constant varchar2(200) := 'xmlns="'||c_sheeturl||'"';
c_rurl constant varchar2(200) := 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
c_rns constant varchar2(200) := 'xmlns:r="'||c_rurl||'"';
c_relsns constant varchar2(200) := 'xmlns="http://schemas.openxmlformats.org/package/2006/relationships"';
type tp_one_range is record
( range_name varchar2(31 char) -- sheet or named raange name
, isSheet boolean := false
, sht_nr pls_integer := null -- sheet number
, row_nr pls_integer := null -- upper left row
, row_off pls_integer := 0 -- range v_size-1
, col_nr pls_integer := null -- upper left col
, col_off pls_integer := 0 -- range h_size-1
);
type tp_all_ranges is table of tp_one_range index by pls_integer;
--
type tp_one_field is record
( type char(1) -- N,D,S,F
, value number -- for strings (S) index tp_str_ind
, function varchar2(32767)
);
-- field access: (sht)(row)(row ins)(col)
type tp_fcols is table of tp_one_field index by pls_integer;
type tp_frows_of is table of tp_fcols index by pls_integer;
type tp_frows is table of tp_frows_of index by pls_integer;
type tp_fsheets is table of tp_frows index by pls_integer;
--
type tp_strings is table of pls_integer index by varchar2(32767);
type tp_str_ind is table of varchar2(32767) index by pls_integer;
--
type tp_one_merge is record
( row_off pls_integer := 0 -- v_size-1
, col_off pls_integer := 0 -- h_size-1
);
type tp_col_merge is table of tp_one_merge not null index by pls_integer;
type tp_row_merge is table of tp_col_merge not null index by pls_integer;
type tp_merges is table of tp_row_merge not null index by pls_integer;
--
type tp_context is record
( template BLOB
, options varchar2(10)
, insmode boolean := false -- insert/overwrite mode
, date1904 boolean := true -- workbook date format
, date_style pls_integer := null -- user-defined or workbook-default date style
, current_sht pls_integer := 1
, strings tp_strings
, str_ind tp_str_ind
, str_cnt_of pls_integer
, ranges tp_strings -- sheet or named range names in upper case
, ran_ind tp_all_ranges
, merges tp_merges
, fields tp_fsheets
);
--
type tp_loc is record
( sht_nr pls_integer := null
, row_nr pls_integer := null
, row_off pls_integer := 0 -- v_size-1 for merges, inserted row index
, col_nr pls_integer := null
, col_off pls_integer := 0 -- h_size-1 for merges
);
--
type tp_one_style is record
( s pls_integer -- style
, t varchar2(100) -- type
, v varchar2(500) -- value
);
type tp_cell_styles is table of tp_one_style not null index by pls_integer;
type tp_row_style is record
( style varchar2(200)
, cells tp_cell_styles
);
--
context tp_context;
--
Procedure debug(str1 varchar2,str2 varchar2:=null,str3 varchar2:=null,str4 varchar2:=null,str5 varchar2:=null)
is
begin
dbms_output.put_line(trim(str1||' '||str2||' '||str3||' '||str4||' '||str5));
end;
--
Procedure clear_sheet_fields(p_s pls_integer)
as
r pls_integer;
ro pls_integer;
begin
if not context.fields.exists(p_s) then return; end if;
r := context.fields(p_s).first();
while r is not null loop
ro := context.fields(p_s)(r).first();
while ro is not null loop
context.fields( p_s )( r )( ro ).delete();
ro := context.fields( p_s )( r ).next(ro);
end loop;
context.fields( p_s )( r ).delete();
r := context.fields(p_s).next(r);
end loop;
-- context.fields( p_s ).delete();
context.fields.delete( p_s );
end;
--
Procedure clear_fields
is
s pls_integer;
begin
s := context.fields.first();
while s is not null loop
clear_sheet_fields(s);
s := context.fields.next( s );
end loop;
context.fields.delete();
end;
--
Procedure clear_sheet_merges(p_sht_nr pls_integer)
is
r pls_integer;
begin
if not context.merges.exists(p_sht_nr) then return; end if;
r := context.merges(p_sht_nr).first();
while r is not null
loop
context.merges(p_sht_nr).delete(r);
r := context.merges(p_sht_nr).next( r );
end loop;
context.merges.delete(p_sht_nr);
end;
--
Procedure clear_merges
is
s pls_integer;
begin
s:=context.merges.first();
while s is not null
loop
clear_sheet_merges(s);
s:=context.merges.next(s);
end loop;
context.merges.delete();
end;
--
Procedure clear_context
is
s pls_integer;
r pls_integer;
begin
context.strings.delete;
context.str_ind.delete;
context.ranges.delete;
context.ran_ind.delete;
context.date1904 := true;
context.date_style := null;
context.current_sht := 1;
clear_fields;
clear_merges;
end;
--
function add_string( p_string varchar2 )
return pls_integer
is
t_cnt pls_integer;
begin
if p_string is null then return null; end if;
if context.strings.exists( p_string )
then
t_cnt := context.strings( p_string );
else
t_cnt := nvl(context.str_cnt_of,0)+context.strings.count();
context.str_ind( t_cnt ) := nvl( p_string, '' );
context.strings( nvl( p_string, '' ) ) := t_cnt;
end if;
return t_cnt;
end;
--
function blob2node( p_blob blob )
return dbms_xmldom.domnode
is
begin
if p_blob is null or dbms_lob.getlength( p_blob ) = 0
then
return null;
end if;
return dbms_xmldom.makenode( dbms_xmldom.getdocumentelement(
dbms_xmldom.newdomdocument( xmltype( p_blob, nls_charset_id( 'AL32UTF8' ) ) ) )
);
end;
--*** in-zip replace needed!!
procedure replace1file
( p_zipped_blob in out blob
, p_name varchar2
, p_content blob
)
is
t_blob blob;
zip_files as_zip.file_list;
begin
if p_zipped_blob is null
then
dbms_lob.createtemporary( p_zipped_blob, true );
end if;
zip_files := as_zip.get_file_list(p_zipped_blob);
for i in zip_files.first() .. zip_files.last
loop
begin
if zip_files(i) <> p_name then
as_zip.add1file(t_blob
, zip_files( i )
, as_zip.get_file(p_zipped_blob,zip_files( i ))
);
end if;
exception -- zip entry is empty folder
when others then null;
end;
end loop;
as_zip.add1file(t_blob, p_name, p_content);
as_zip.finish_zip(t_blob);
dbms_lob.trim(p_zipped_blob,0);
dbms_lob.append(p_zipped_blob,t_blob);
end;
--
procedure replace1xml
( p_msFile in out nocopy blob
, p_filePath varchar2
, p_xml xmlType
)
is
t_blob BLOB;
t_xml xmltype;
begin
t_blob := p_xml.getBlobVal(nls_charset_id('AL32UTF8'),4,0);
replace1file( p_msFile, p_filePath, t_blob );
if dbms_lob.istemporary(t_blob)=1 then dbms_lob.freetemporary(t_blob); end if;
end;
--
Function get1xml
( p_msfile in out nocopy blob
, p_filename varchar2
) return xmlType
is
t_btmp blob;
t_xml xmltype;
begin
t_btmp := as_zip.get_file(p_msfile,p_filename);
if t_btmp is null or dbms_lob.getlength( t_btmp ) = 0
then
return null;
end if;
t_xml := xmltype( t_btmp, nls_charset_id( 'AL32UTF8' ) );
if dbms_lob.istemporary(t_btmp) = 1 then dbms_lob.freetemporary(t_btmp); end if;
return t_xml;
end;
--
function col2alfan( p_col pls_integer )
return varchar2
is
begin
return
case
when p_col > 702 then
chr( 64 + trunc( ( p_col - 27 ) / 676 ) )
|| chr( 65 + mod( trunc( ( p_col - 1 ) / 26 ) - 1, 26 ) )
|| chr( 65 + mod( p_col - 1, 26 ) )
when p_col > 26 then
chr( 64 + trunc( ( p_col - 1 ) / 26 ) ) || chr( 65 + mod( p_col - 1, 26 ) )
else chr( 64 + p_col )
end;
end;
--
Function cell2alfan
( p_row pls_integer
, p_col pls_integer
, p_range_name varchar2:=null
, p_opt varchar2:='' -- '1' absolute style
) return varchar2
is
t_$ varchar2(1) := case when instr(p_opt,'1') > 0 then '$' end;
t_alfan varchar2(200) := p_range_name;
begin
if t_alfan is not null then
if regexp_instr(t_alfan,'^[[:alnum:]_]*$') = 0 then
t_alfan := ''''||t_alfan||'''';
end if;
if p_row is null and p_col is null then return t_alfan; end if;
t_alfan := t_alfan||'!';
end if;
return t_alfan||t_$||col2alfan(p_col)||t_$||p_row;
end;
--
function alfan2col( p_col varchar2 )
return pls_integer
is
begin
return ascii( substr( p_col, -1 ) ) - 64
+ nvl( ( ascii( substr( p_col, -2, 1 ) ) - 64 ) * 26, 0 )
+ nvl( ( ascii( substr( p_col, -3, 1 ) ) - 64 ) * 676, 0 );
end;
--
Function range2loc(p_ind pls_integer, p_opt varchar2:='') return tp_loc
is
t_loc tp_loc;
t_ran tp_one_range := context.ran_ind(p_ind);
begin
t_loc.sht_nr := t_ran.sht_nr;
t_loc.row_nr := t_ran.row_nr;
t_loc.col_nr := t_ran.col_nr;
t_loc.row_off := t_ran.row_off;
t_loc.col_off := t_ran.col_off;
return t_loc;
end;
--
Function loc2cell(p_loc tp_loc) return tp_loc
is
t_loc tp_loc := p_loc;
begin
t_loc.row_off := 0;
t_loc.col_off := 0;
return t_loc;
end;
--
Function addr2loc
( p_address varchar2
, raise_ref boolean := instr(context.options,'e') > 0
) return tp_loc
is
t_loc tp_loc;
t_range_name varchar2(100);
t_addrp varchar2(200):= --
'^($?\''[^'']+\''|[^\!]+)(\!([a-zA-Z0-9\:\$]+))?$'; --'1,3
t_rangep varchar2(200):=
'^(\$?([a-zA-Z]{1,3})\$?([1-9][0-9]*))(:(\$?([a-zA-Z]{1,3})\$?([1-9][0-9]*)))?$'; --2,3 6,7
t_s varchar2(200); -- sheet/range name
t_r varchar2(200); -- cell range (a1:b2)
begin
if p_address is null or regexp_instr(p_address,t_addrp) = 0
then raise data_error; end if;
t_s:=regexp_replace(p_address,t_addrp,'\1'); -- sheet name
t_r:=regexp_replace(p_address,t_addrp,'\3'); -- cell range
if (t_r is not null and regexp_instr(t_r, t_rangep) = 0) -- range parsing error
then raise data_error; end if;
t_range_name:= replace(t_s,''''); -- set unquoted range name
if context.ranges.exists(upper(t_range_name)) then
t_loc := range2loc(context.ranges(upper(t_range_name)),'d');
else
if t_r is null and regexp_instr(t_s,t_rangep) > 0 then
t_r := t_s; -- assume sheet name is cell addr
t_loc := range2loc(context.current_sht);
else raise data_error; end if;
end if;
t_loc.col_nr :=
t_loc.col_nr+nvl(alfan2col(regexp_replace(t_r,t_rangep,'\2')),1)-1;
t_loc.row_nr :=
t_loc.row_nr+nvl(to_number(regexp_replace(t_r,t_rangep,'\3')),1)-1;
t_loc.col_off :=
nvl(alfan2col(regexp_replace(t_r,t_rangep,'\6')),t_loc.col_nr)-t_loc.col_nr;
t_loc.row_off :=
nvl(to_number(regexp_replace(t_r,t_rangep,'\7')),t_loc.row_nr)-t_loc.row_nr;
--debug(t_loc.sht_nr,t_loc.row_nr,t_loc.row_off,t_loc.col_nr,t_loc.col_off);
if t_loc.col_off < 0 or t_loc.row_off < 0 then raise data_error; end if;
return t_loc;
exception
when data_error then
if raise_ref then
raise_application_error(c_errcode,'#REF!: '||nvl(p_address,'null'));
else
t_loc.sht_nr := null;
return t_loc;
end if;
end;
--
Function loc2addr(p_loc tp_loc, p_opt varchar2:='') return varchar2
is
t_addr varchar2(500);
begin
if not (p_loc.row_nr > 0 and p_loc.col_nr > 0) then
return null;
end if;
if p_loc.sht_nr > 0 then
t_addr := context.ran_ind(p_loc.sht_nr).range_name;
end if;
t_addr := cell2alfan(p_loc.row_nr,p_loc.col_nr,t_addr,p_opt);
if instr(p_opt,'d') > 0 then
t_addr := t_addr||':'
||cell2alfan(p_loc.row_nr+p_loc.row_off,p_loc.col_nr+p_loc.col_off,null,p_opt);
end if;
return t_addr;
end;
--
Procedure add_range
( p_range_name varchar2
, p_range_def varchar2
)
is
t_cnt pls_integer;
t_loc tp_loc;
begin
if not context.ranges.exists( upper(p_range_name) )
then
t_cnt := context.ranges.count()+1;
context.ran_ind( t_cnt ).range_name := p_range_name;
context.ranges( upper(nvl( p_range_name, '' ) )) := t_cnt;
if p_range_def is not null then
t_loc := addr2loc(p_range_def);
--debug(p_range_name);
context.ran_ind( t_cnt ).sht_nr := t_loc.sht_nr;
context.ran_ind( t_cnt ).row_nr := t_loc.row_nr;
context.ran_ind( t_cnt ).col_nr := t_loc.col_nr;
context.ran_ind( t_cnt ).row_off := t_loc.row_off;
context.ran_ind( t_cnt ).col_off := t_loc.col_off;
--debug(loc2addr(t_loc,'d'));
else
context.ran_ind( t_cnt ).sht_nr := t_cnt;
context.ran_ind( t_cnt ).isSheet := true;
context.ran_ind( t_cnt ).row_nr := 1;
context.ran_ind( t_cnt ).col_nr := 1;
context.ran_ind( t_cnt ).row_off := 0;
context.ran_ind( t_cnt ).col_off := 0;
end if;
end if;
end;
--
Function merge_exists
( p_sht_nr pls_integer
, p_row_nr pls_integer
, p_col_nr pls_integer
) return boolean
is
begin
return context.merges(p_sht_nr)(p_row_nr).exists(p_col_nr);
exception when no_data_found then return false;
end;
-- Return merge as tp_loc
Function merge2loc
( p_sht_nr pls_integer
, p_row_nr pls_integer
, p_col_nr pls_integer
) return tp_loc
is
t_loc tp_loc;
begin
t_loc.sht_nr := p_sht_nr;
t_loc.row_nr := p_row_nr;
t_loc.col_nr := p_col_nr;
t_loc.row_off := context.merges(p_sht_nr)(p_row_nr)(p_col_nr).row_off;
t_loc.col_off := context.merges(p_sht_nr)(p_row_nr)(p_col_nr).col_off;
return t_loc;
end;
--
Function loc_in_range
( p_rloc tp_loc -- range or merge loc
, p_loc tp_loc -- cell loc
) return boolean
is
begin
return (p_loc.row_nr between p_rloc.row_nr and p_rloc.row_nr + p_rloc.row_off)
and (p_loc.col_nr between p_rloc.col_nr and p_rloc.col_nr + p_rloc.col_off);
end;
-- Returns merge that intersect loc
Function get_merge( p_loc tp_loc ) return tp_loc
is
t_mloc tp_loc;
t_r pls_integer;
t_c pls_integer;
begin
if merge_exists(p_loc.sht_nr, p_loc.row_nr, p_loc.col_nr) then
return merge2loc(p_loc.sht_nr, p_loc.row_nr, p_loc.col_nr);
end if;
if context.merges.exists(p_loc.sht_nr) then
t_r := context.merges(p_loc.sht_nr).first();
while t_r is not null and t_r <= p_loc.row_nr loop
t_c := context.merges(p_loc.sht_nr)(t_r).first();
while t_c is not null and t_c <= p_loc.col_nr loop
t_mloc := merge2loc(p_loc.sht_nr, t_r, t_c);
if loc_in_range(t_mloc, p_loc)
then
return t_mloc;
end if;
t_c := context.merges(p_loc.sht_nr)(t_r).next(t_c);
end loop;
t_r := context.merges(p_loc.sht_nr).next(t_r);
end loop;
end if;
return loc2cell(p_loc);
end;
/*
-- Cut vertical merge
Procedure cut_one_merge
( p_mloc tp_loc -- merge loc
, p_cloc tp_loc -- cell loc
)
as
t_dc pls_integer;
t_merge tp_one_merge;
begin
if p_cloc.row_nr = p_mloc.row_nr and p_mloc.row_off = 0 then return; end if;
t_merge.col_off := p_mloc.col_off;
t_dc := p_cloc.row_nr - p_mloc.row_nr ;
t_merge.row_off := greatest(t_dc - 1, 0);
context.merges(p_mloc.sht_nr)(p_mloc.row_nr)(p_mloc.col_nr) := t_merge;
if t_dc > 0 then
t_merge.row_off := 0;
context.merges(p_mloc.sht_nr)(p_cloc.row_nr)(p_mloc.col_nr) := t_merge;
end if;
if t_dc < p_mloc.row_off then
t_merge.row_off := p_mloc.row_off - t_dc - 1;
context.merges(p_mloc.sht_nr)(p_cloc.row_nr + 1)(p_mloc.col_nr) := t_merge;
end if;
end;
*/
-- Cut merged cell
Procedure cut_one_merge
( p_mloc tp_loc -- merge loc
, p_loc tp_loc -- cell loc
)
as
t_merge tp_one_merge;
begin
if not merge_exists(p_mloc.sht_nr, p_mloc.row_nr, p_mloc.col_nr)
then return; end if;
if p_mloc.row_off = 0 and p_mloc.row_nr = p_loc.row_nr and p_mloc.col_nr = p_loc.col_nr
then return; end if;
context.merges(p_mloc.sht_nr)(p_mloc.row_nr).delete(p_mloc.col_nr);
t_merge.col_off := p_mloc.col_off - (p_loc.col_nr - p_mloc.col_nr);
if t_merge.col_off <= 0 then return; end if;
context.merges(p_loc.sht_nr)(p_loc.row_nr)(p_loc.col_nr) := t_merge;
end;
/*
-- Cut merges in p_loc row
Procedure cut_merges
( p_loc tp_loc -- cell loc
)
as
t_r pls_integer;
t_c pls_integer;
t_mloc tp_loc;
t_loc tp_loc := p_loc;
begin
if p_loc.row_off = 0 and context.merges.exists(p_loc.sht_nr) then
t_r := context.merges(p_loc.sht_nr).first();
while t_r is not null and t_r <= p_loc.row_nr loop
t_c := context.merges(p_loc.sht_nr)(t_r).first();
while t_c is not null loop
t_mloc := merge2loc(p_loc.sht_nr, t_r, t_c);
t_loc.col_nr := t_mloc.col_nr; --????
if loc_in_range(t_mloc, t_loc)
then
--debug(loc2addr(p_loc),loc2addr(t_mloc));
cut_one_merge(t_mloc, t_loc);
end if;
t_c := context.merges(p_loc.sht_nr)(t_r).next(t_c);
end loop;
t_r := context.merges(p_loc.sht_nr).next(t_r);
end loop;
end if;
end;
*/
-- Check loc aligning in merge
Function align_in_merge
( p_loc tp_loc
, t_insert boolean := context.insmode
) return tp_loc
is
t_mloc tp_loc;
begin
t_mloc := get_merge(p_loc);
if instr(context.options,'e') > 0 --
and (( p_loc.col_nr != t_mloc.col_nr or p_loc.row_nr != t_mloc.row_nr )
or (t_insert and t_mloc.row_off > 0) )
then
raise_application_error(c_errcode,'#REF! aligning: ' || loc2addr(p_loc,''));
end if;
--debug(t_mloc.row_off);
if t_insert or (p_loc.col_nr != t_mloc.col_nr or p_loc.row_nr != t_mloc.row_nr)
then cut_one_merge(t_mloc, p_loc); end if;
return p_loc;
end;
--
Function field_exists(p_loc tp_loc)
return boolean
is
begin
return context.fields(p_loc.sht_nr)(p_loc.row_nr)(p_loc.row_off).exists(p_loc.col_nr);
exception
when others then return false;
end;
--
Function field_exists(p_sht_nr pls_integer, p_row_nr pls_integer, p_row_off pls_integer:=0, p_col_nr pls_integer)
return boolean
is
begin
return context.fields(p_sht_nr)(p_row_nr)(p_row_off).exists(p_col_nr);
exception
when others then return false;
end;
--
Procedure add_value
( p_value number
, p_type char
, p_loc tp_loc
, p_func varchar2 := ''
)
is
t_field tp_one_field;
t_loc tp_loc := p_loc;
begin
t_loc := align_in_merge(p_loc, context.insmode); -- alignment in_table next_row
if p_loc.sht_nr is null then return; end if;
t_field.type := p_type;
t_field.value := p_value;
t_field.function := p_func;
context.current_sht := p_loc.sht_nr;
context.fields( t_loc.sht_nr )( t_loc.row_nr )( t_loc.row_off )( t_loc.col_nr ) := t_field;
end;
--
Procedure add_field
( p_value date
, p_loc tp_loc
)
is
begin
add_value( p_value - case when context.date1904 then to_date('01-01-1904','DD-MM-YYYY')
else to_date('01-01-1900','DD-MM-YYYY') end + 2
,'D', p_loc);
end;
--
Procedure add_field
( p_value number
, p_loc tp_loc )
is
begin
add_value(p_value, 'N', p_loc);
end;
--
Procedure add_field
( p_value varchar2
, p_loc tp_loc )
is
begin
if substr(p_value,1,1) = '=' then
add_value(null,'F',p_loc,substr(p_value,2));
else
add_value(add_string(regexp_replace(p_value,'^''=')), 'S', p_loc);
end if;
end;
--
Function ins_count(p_sht_nr pls_integer, p_row_nr pls_integer, p_col_nr pls_integer)
return pls_integer
is
t_cnt pls_integer := 0;
begin
while field_exists(p_sht_nr,p_row_nr,t_cnt,p_col_nr) loop
t_cnt := t_cnt + 1;
end loop;
return t_cnt;
end;
--
Function loc_field(p_address varchar2, p_options varchar2) return tp_loc
is
t_loc tp_loc;
begin
context.insmode := instr(p_options,'i') > 0;
t_loc := loc2cell(addr2loc(p_address));
if t_loc.sht_nr is not null then
t_loc := align_in_merge(t_loc); --
if context.insmode then
t_loc.row_off := ins_count(t_loc.sht_nr,t_loc.row_nr,t_loc.col_nr);
end if;
end if;
return t_loc;
end;
--
Procedure IN_FIELD(p_value number, p_address varchar2, p_options varchar2:='')
is
t_loc tp_loc;
begin
t_loc := loc_field(p_address, p_options);
if t_loc.sht_nr is null then return; end if;
add_field( p_value, t_loc );
end;
--
Procedure IN_FIELD(p_value date, p_address varchar2, p_options varchar2:='')
is
t_loc tp_loc;
begin
t_loc := loc_field(p_address, p_options);
if t_loc.sht_nr is null then return; end if;
add_field( p_value, t_loc );
end;
--
Procedure IN_FIELD(p_value varchar2, p_address varchar2, p_options varchar2:='')
is
t_loc tp_loc;
begin
t_loc := loc_field(p_address, p_options);
if t_loc.sht_nr is null then return; end if;
add_field( p_value, t_loc );
end;
--
Procedure IN_TABLE(p_table CLOB, p_address varchar2, p_options varchar2:='')
as
l_cursor ref_cursor;
begin
-- Open REF CURSOR variable:
OPEN l_cursor FOR p_table;
in_table(l_cursor, p_address, p_options);
end;
---
Function next_row
( p_loc tp_loc
, p_insert boolean:=context.insmode
) return tp_loc
is
t_loc tp_loc := p_loc;
t_mloc tp_loc;
begin
if p_insert then
t_loc.row_off := t_loc.row_off + 1;
else
t_loc.row_nr := t_loc.row_nr + 1;
end if;
return t_loc;
end;
--
Function next_col
( p_loc tp_loc
, p_insert boolean
) return tp_loc
is
t_loc tp_loc := p_loc;
t_mloc tp_loc := get_merge( p_loc );
begin
t_loc.col_nr := t_mloc.col_nr + t_mloc.col_off + 1;
return t_loc;
end;
--
Procedure IN_TABLE(p_table in out ref_cursor, p_address varchar2, p_options varchar2:='')
as
t_header boolean := instr(p_options, 'h') > 0;
t_insert boolean := instr(p_options, 'i') > 0;
t_loc tp_loc;
t_cloc tp_loc;
t_rloc tp_loc;
t_c integer;
t_col_cnt integer;
t_desc_tab dbms_sql.desc_tab2;
d_tab dbms_sql.date_table;
n_tab dbms_sql.number_table;
v_tab dbms_sql.varchar2_table;
t_bulk_size pls_integer := 200;
t_r integer;
begin
context.insmode := t_insert;
t_c := DBMS_SQL.TO_CURSOR_NUMBER(p_table);
t_loc := loc2cell(addr2loc(p_address));
if t_loc.sht_nr is null then return; end if;
t_loc := align_in_merge(t_loc, t_insert );
-- t_c := dbms_sql.open_cursor;
-- dbms_sql.parse( t_c, p_sql, dbms_sql.native );
dbms_sql.describe_columns2( t_c, t_col_cnt, t_desc_tab );
t_cloc := t_loc;
for c in 1 .. t_col_cnt
loop
if t_header
then
add_field(t_desc_tab( c ).col_name, t_cloc);
t_cloc := next_col( t_cloc, t_insert );
end if;
case
when t_desc_tab( c ).col_type in ( 2, 100, 101 )
then
dbms_sql.define_array( t_c, c, n_tab, t_bulk_size, 1 );
when t_desc_tab( c ).col_type in ( 12, 178, 179, 180, 181 , 231 )
then
dbms_sql.define_array( t_c, c, d_tab, t_bulk_size, 1 );
when t_desc_tab( c ).col_type in ( 1, 8, 9, 96, 112 )
then
dbms_sql.define_array( t_c, c, v_tab, t_bulk_size, 1 );
else
null;
end case;
end loop;
--
if t_header then t_loc := next_row(t_loc, t_insert); end if;
-- t_r := dbms_sql.execute( t_c );
loop
t_r := dbms_sql.fetch_rows( t_c );
t_cloc := t_loc;
for c in 1 .. t_col_cnt
loop
t_rloc := t_cloc;
case
when t_desc_tab( c ).col_type in ( 2, 100, 101 )
then
dbms_sql.column_value( t_c, c, n_tab );
for i in 0 .. t_r - 1
loop
add_field(n_tab( i + n_tab.first() ), t_rloc);
t_rloc := next_row(t_rloc, t_insert);
end loop;
n_tab.delete;
when t_desc_tab( c ).col_type in ( 12, 178, 179, 180, 181 , 231 )
then
dbms_sql.column_value( t_c, c, d_tab );
for i in 0 .. t_r - 1
loop
add_field(d_tab( i + d_tab.first() ), t_rloc);
t_rloc := next_row(t_rloc, t_insert);
end loop;