forked from ggreen86/XLSX-Workbook-Class
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vfpxworkbookxlsx.PRG
9442 lines (8540 loc) · 354 KB
/
vfpxworkbookxlsx.PRG
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
**************************************************
*-- Class: vfpxworkbookxlsx (vfpxworkbookxlsx.vcx)
*-- ParentClass: custom
*-- BaseClass: custom
*-- Time Stamp: 12.02.20 16:16:04
*
#INCLUDE "vfpxworkbookxlsx.h"
*
Define Class vfpxworkbookxlsx As Custom
*-- Indicates to auto-trim sheet name to max length ***Boolean
AutoTrimSheetName = .T.
*-- CodePage to use for the Strings cursor
Codepage = 0
*-- Company Name
CompanyName = "VFPxWorkbookXLSX"
*-- Creator Name
CreatorName = "VFPxWorkbookXLSX"
*-- Sets debugging mode ***Boolean
Debug = .F.
*-- Flag to execute the DeclareDLL method in the Init() ***Boolean
DeclareWinApi = .T.
*-- Workbook Default Font
DefaultFont = "Calibri"
*-- Default font size ***Integer
DefaultFontSize = 11
*-- Default sheet name (language specific)
DefaultSheetName = "Sheet"
*-- Error level Id ***Integer
ErrorLevelId = 0
*-- Release version of Class ***Integer
ExcelXlsxRelease = 36
*-- XML DOM Object
oXDom = ""
*-- Flag to save a currency value as a numeric rather than currency in the spreadsheet; will convert the value to four decimal places ***Boolean
SaveCurrencyAsNumeric = .F.
*-- Subject of Workbook (stored in properties)
Subject = ""
*-- Title of workbook (stored in properties)
Title = ""
*-- The boolean value as displayed in Excel delimited by a pipe symbol
TrueFalseValue = "Yes|No"
*-- User Name
UserName = "VFPxWorkbookXLSX"
*-- XML Metadata for customizable properties
_MemberData = [<VFPData>]+;
[<memberdata name="autotrimsheetname" type="property" display="AutoTrimSheetName"/>]+;
[<memberdata name="defaultfontsize" type="property" display="DefaultFontSize"/>]+;
[<memberdata name="cellrefasciitoindex" type="method" display="CellRefAsciiToIndex"/>]+;
[<memberdata name="getchecksum" type="method" display="GetCheckSum"/>]+;
[<memberdata name="getstringrecord" type="method" display="GetStringRecord"/>]+;
[<memberdata name="initalizeidvalues" type="method" display="InitalizeIdValues"/>]+;
[<memberdata name="insertcell" type="method" display="InsertCell"/>]+;
[<memberdata name="addclassdefinednumericformats" type="method" display="AddClassDefinedNumericFormats"/>]+;
[<memberdata name="addcustomnumericformat" type="method" display="AddCustomNumericFormat"/>]+;
[<memberdata name="addindexcolor" type="method" display="AddIndexColor"/>]+;
[<memberdata name="addinlinefontobject" type="method" display="AddInlineFontObject"/>]+;
[<memberdata name="addmrucolor" type="method" display="AddMruColor"/>]+;
[<memberdata name="addnamedrange" type="method" display="AddNamedRange"/>]+;
[<memberdata name="addnumericformat" type="method" display="AddNumericFormat"/>]+;
[<memberdata name="addsheet" type="method" display="AddSheet"/>]+;
[<memberdata name="addstringvalue" type="method" display="AddStringValue"/>]+;
[<memberdata name="addstyleborders" type="method" display="AddStyleBorders"/>]+;
[<memberdata name="addstylefill" type="method" display="AddStyleFill"/>]+;
[<memberdata name="addstylefont" type="method" display="AddStyleFont"/>]+;
[<memberdata name="addstylehorizalignment" type="method" display="AddStyleHorizAlignment"/>]+;
[<memberdata name="addstyleindent" type="method" display="AddStyleIndent"/>]+;
[<memberdata name="addstylenumericformat" type="method" display="AddStyleNumericFormat"/>]+;
[<memberdata name="addstyletextrotation" type="method" display="AddStyleTextRotation"/>]+;
[<memberdata name="addstylevertalignment" type="method" display="AddStyleVertAlignment"/>]+;
[<memberdata name="addstylewordwrap" type="method" display="AddStyleWordWrap"/>]+;
[<memberdata name="cellformatpainter" type="method" display="CellFormatPainter"/>]+;
[</VFPData>]
Name = "vfpxworkbookxlsx"
*-- Stores the last used border Id for a workbook
Dimension LastBorderId[1,2]
*-- Stores the last used CellXfs Id for a workbook
Dimension LastCellXfsId[1,2]
*-- Stores the last used fill Id for a workbook
Dimension LastFillId[1,2]
*-- Stores the last used font Id for a workbook
Dimension LastFontId[1,2]
*-- Last used IndexedColor Id
Dimension LastIndexColorId[1,2]
*-- Last used mruColor Id value
Dimension LastMruColorId[1,2]
*-- Last used NumFmt Id
Dimension LastNumFmtId[1,2]
*-- Last used Relationship Id
Dimension LastRelationId[1,2]
*-- Last used sheet Id
Dimension LastSheetId[1,2]
*-- Stores the last used string Id for a workbook
Dimension LastStringId[1,2]
*-- Returns the next string index value by workbook ***Integer
Dimension StringNdx[1]
*-- Adds pre-defined numeric cell formats
Protected Procedure AddClassDefinedNumericFormats
Lparameters tnWB, tnFormatId
Local llAdded, lcTextFormat, lcXMLFormat
llAdded = Inlist(tnFormatId, CELL_FORMAT_CURRENCY_RED, CELL_FORMAT_ACC_CURR_POUNDS, CELL_FORMAT_ACC_CURR_EURO,;
CELL_FORMAT_CURR_POUNDS_RED, CELL_FORMAT_CURR_EURO_RED)
Do Case
Case tnFormatId = CELL_FORMAT_CURRENCY_RED
lcTextFormat = '"$"#,##0.00;[Red]("$"#,##0.00)'
lcXMLFormat = This.GetXMLString(lcTextFormat)
Case tnFormatId = CELL_FORMAT_ACC_CURR_POUNDS
lcXMLFormat = '_-[$£-809]* #,##0.00_-;-[$£-809]* #,##0.00_-;_-[$£-809]* "-"??_-;_-@_-'
lcTextFormat = '"£"#,##0.00;[Red]("£"#,##0.00)'
Case tnFormatId = CELL_FORMAT_ACC_CURR_EURO
lcXMLFormat = '_-[$€-2]* .00_-;-[$€-2]* .00_-;_-[$€-2]* "-"??_-;_-@_-'
lcTextFormat = '"€"#,##0.00;[Red]("€"#,##0.00)'
Case tnFormatId = CELL_FORMAT_CURR_POUNDS_RED
lcXMLFormat = '[$£-809]#,##0.00;[Red]\-[$£-809]#,##0.00'
lcTextFormat = '"£"#,##0.00;[Red]("£"#,##0.00)'
Case tnFormatId = CELL_FORMAT_CURR_EURO_RED
lcXMLFormat = '[$€-2]\ #,##0.00_);[Red]\([$€-2]\ #,##0.00\)'
lcTextFormat = '"€"#,##0.00;[Red]("€"#,##0.00)'
Endcase
If llAdded
Insert Into xl_numFmts (workbook, Id, formatxml, formatcode, applydec) ;
VALUES (tnWB, tnFormatId, lcXMLFormat, lcTextFormat, False)
Endif
Return llAdded
*-- Adds a new custom defined numeric format
Procedure AddCustomNumericFormat
Lparameters tnWB, tcPosFormat, tcNegFormat, tcZeroFormat, tcTextFormat, tlApplyDec
Local lnId, lcFormatCode, lnNdx, lcSetPoint, lcSeparator
If Vartype(tcPosFormat) != "C"
Return 0
Endif
Do Case
Case Pcount() = 1
tcNegFormat = ""
tcZeroFormat = ""
tcTextFormat = ""
tlApplyDec = False
Case Pcount() = 2
tcZeroFormat = ""
tcTextFormat = ""
tlApplyDec = False
Case Pcount() = 3
tcTextFormat = ""
tlApplyDec = False
Case Pcount() = 4
tlApplyDec = False
Endcase
If Vartype(tcNegFormat) != "C"
tcNegFormat = ""
Endif
If Vartype(tcZeroFormat) != "C"
tcZeroFormat = ""
Endif
If Vartype(tcTextFormat) != "C"
tcTextFormat = ""
Endif
If Occurs(";", tcPosFormat) > 0 Or Occurs(";", tcNegFormat) > 0 Or Occurs(";", tcZeroFormat) > 0 Or Occurs(";", tcTextFormat) > 0
Return 0
Endif
*-* If not US standard for separator and decimal point, then change to US standard
lcSetPoint = Set("POINT")
lcSeparator = Set("SEPARATOR")
If lcSetPoint != "." Or lcSeparator != ","
tcPosFormat = Chrtran(Chrtran(tcPosFormat, lcSetPoint + lcSeparator, Tab + CR), Tab + CR, ".,")
tcNegFormat = Chrtran(Chrtran(tcNegFormat, lcSetPoint + lcSeparator, Tab + CR), Tab + CR, ".,")
tcZeroFormat = Chrtran(Chrtran(tcZeroFormat, lcSetPoint + lcSeparator, Tab + CR), Tab + CR, ".,")
tcTextFormat = Chrtran(Chrtran(tcTextFormat, lcSetPoint + lcSeparator, Tab + CR), Tab + CR, ".,")
Endif
*-* Build expression from parts
lcFormatCode = tcPosFormat
If !Empty(tcNegFormat)
lcFormatCode = lcFormatCode + ";" + tcNegFormat
Endif
If !Empty(tcZeroFormat)
lcFormatCode = lcFormatCode + ";" + tcZeroFormat
Endif
If !Empty(tcTextFormat)
lcFormatCode = lcFormatCode + ";" + tcTextFormat
Endif
lnId = This.GetNextId(tnWB, "xl_numfmts")
Insert Into xl_numFmts (workbook, Id, formatxml, formatcode, applydec) ;
VALUES (tnWB, lnId, This.GetXMLString(lcFormatCode), lcFormatCode, tlApplyDec)
Return lnId
*-- Adds a new indexed color definition
Procedure AddIndexColor
Lparameters tnWB, tnRGBColor
Local lnIndexId, lcHexColor
If Pcount() != 2 Or Vartype(tnRGBColor) != "N"
Return 0
Endif
lcHexColor = This.ConvertColorToHex(tnRGBColor)
If Seek(BinToC(tnWB)+lcHexColor, "xl_ndxcolors", "rgbcolor")
lnIndexId = xl_ndxcolors.indexid
Else
lnIndexId = This.GetNextId(tnWB, "xl_ndxcolors")
Insert Into xl_ndxcolors (workbook, indexid, rgbcolor) Values (tnWB, lnIndexId, lcHexColor)
Endif
Return lnIndexId
*-- Adds an object for setting the inline formatting of cell text
Procedure AddInlineFontObject
Lparameters toInline, tnBeg, tnLen, tcFontName, tnFontSize, tnFontColor, tlFontBold, tlFontItalic, tcULine, tlStrkThru, tlSubscript, tlSuperscript
Local loCharacter
If Pcount() < 4 Or Vartype(toInline) != "O"
Return Null
Endif
Do Case
Case Pcount() = 4
tnFontSize = 10
tnFontColor = Rgb(0,0,0)
tlFontBold = False
tlFontItalic = False
tcULine = UNDERLINE_NONE
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 5
tnFontColor = Rgb(0,0,0)
tlFontBold = False
tlFontItalic = False
tcULine = UNDERLINE_NONE
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 6
tlFontBold = False
tlFontItalic = False
tcULine = UNDERLINE_NONE
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 7
tlFontItalic = False
tcULine = UNDERLINE_NONE
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 8
tcULine = UNDERLINE_NONE
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 9
tlStrkThru = False
tlSubscript = False
tlSuperscript = False
Case Pcount() = 10
tlSubscript = False
tlSuperscript = False
Case Pcount() = 11
tlSuperscript = False
Endcase
loCharacter = Createobject("Empty")
AddProperty(loCharacter, "BegPos", tnBeg)
AddProperty(loCharacter, "Length", tnLen)
AddProperty(loCharacter, "FontName", tcFontName)
AddProperty(loCharacter, "FontSize", tnFontSize)
AddProperty(loCharacter, "FontBold", tlFontBold)
AddProperty(loCharacter, "FontItalic", tlFontItalic)
AddProperty(loCharacter, "FontColor", tnFontColor)
AddProperty(loCharacter, "Underline", tcULine)
AddProperty(loCharacter, "StrikeThru", tlStrkThru)
AddProperty(loCharacter, "SubScript", tlSubscript)
AddProperty(loCharacter, "SuperScript", tlSuperscript)
toInline.Count = toInline.Count + 1
Dimension toInline.Characters[toInline.Count]
toInline.Characters[toInline.Count] = loCharacter
Return toInline
*-- Adds a custom defined MRU color to the workbook
Procedure AddMruColor
Lparameters tnWB, tnRGBColor
Local lnIndexId, lcHexColor
If Pcount() != 2 Or Vartype(tnRGBColor) != "N"
Return 0
Endif
With This
lcHexColor = .ConvertColorToHex(tnRGBColor)
If Seek(BinToC(tnWB)+lcHexColor, "xl_mrucolors", "rgbcolor")
lnIndexId = xl_mrucolors.indexid
Else
lnIndexId = .GetNextId(tnWB, "xl_mrucolors")
Insert Into xl_mrucolors (workbook, indexid, rgbcolor) Values (tnWB, lnIndexId, lcHexColor)
Endif
Endwith
Return lnIndexId
*-- Adds a name range to the workbook
Procedure AddNamedRange
Lparameters tnWB, tnSheet, tcName, tnScope, tcComment, tnBegRow, tnBegCol, tnEndRow, tnEndCol
Local lcRangeName
If Pcount() < 8 Or Vartype(tcName) != "C" Or Len(tcName) > 50 Or Len(tcComment) > 254 Or tnBegRow > tnEndRow Or tnBegCol > tnEndCol
Return ""
Endif
If Vartype(tnScope) != "N"
tnScope = 0
Endif
If Vartype(tcComment) != "C"
tcComment = ""
Endif
lcRangeName = Chrtran(Alltrim(tcName), " !@#$%^&*()+={}[]|<>,.'?/" + Chr(34), Replicate("_", 25))
If !Isalpha(lcRangeName) And !Inlist(Left(lcRangeName, 1), "_", "\")
lcRangeName = "_" + lcRangeName
Endif
If Seek(BinToC(tnWB), "xl_workbooks", "workbook") And !Seek(BinToC(tnWB)+Padl(lcRangeName, 254, " "), "xl_namerange", "wbrname")
Insert Into xl_namerange (workbook, Sheet, rname, scope, Comment, begrow, begcol, endrow, endcol) ;
VALUES (tnWB, tnSheet, lcRangeName, tnScope, tcComment, tnBegRow, tnBegCol, tnEndRow, tnEndCol)
Else
lcRangeName = ""
Endif
Return lcRangeName
*-- Adds a numeric format expression to be used
Procedure AddNumericFormat
Lparameters tnWB, tcFormatCode
Local lcPosCode, lcNegCode, lcZerCode
If Pcount() < 2 Or (Vartype(tnWB) != "N" And tnWB > 0) Or Vartype(tcFormatCode) != "C"
Return 0
Endif
With This
lcPosCode = .ParseString(tcFormatCode, 1, ";")
lcNegCode = .ParseString(tcFormatCode, 2, ";")
lcZerCode = .ParseString(tcFormatCode, 3, ";")
Return .AddCustomNumericFormat(tnWB, lcPosCode, lcNegCode, lcZerCode)
Endwith
*-- Adds a new sheet to the workbook
Procedure AddSheet
Lparameters tnWB, tcSheetName, tnState
Local lnShId, lnRelId
lnShId = 0
With This
If Pcount() > 0 And Seek(BinToC(tnWB), "xl_workbooks", "workbook")
lnShId = .GetNextId(tnWB, "xl_sheets")
Do Case
Case Pcount() = 0
Case Pcount() = 1
tcSheetName = "Sheet" + Transform(lnShId + 1) && Bug fix identified by Dan Lauer
tnState = VISIBLE_SHEET_STATE
Case Pcount() = 2
If Vartype(tcSheetName) != "C"
tcSheetName = "Sheet" + Transform(lnShId + 1)
Endif
tnState = VISIBLE_SHEET_STATE
Otherwise
If Vartype(tcSheetName) != "C"
tcSheetName = "Sheet" + Transform(lnShId + 1)
Endif
If Vartype(tnState) != "N"
tnState = VISIBLE_SHEET_STATE
Endif
Endcase
If Empty(tcSheetName)
tcSheetName = "Sheet" + Transform(lnShId + 1)
Endif
tcSheetName = Chrtran(tcSheetName, ":\/?*", "_____")
If Len(tcSheetName) > LIMITS_MAX_SH_NAME
If .AutoTrimSheetName
tcSheetName = Left(Alltrim(tcSheetName), LIMITS_MAX_SH_NAME)
Else
.SetLastId(tnWB, lnShId-1, "xl_sheets")
Return 0
Endif
Endif
If Seek(BinToC(tnWB)+Upper(Padr(tcSheetName, Len(xl_sheets.shname))), "xl_sheets", "shname") && Change recommendation by Doug Hennig
lnShId = 0
Else
Insert Into xl_sheets (workbook, Sheet, shname, state, mleft, mright, mtop, mbot, mheader, mfooter, shdeleted, xsplit, ysplit, prnorient, ;
papersize, Paperwidth, paperheight, Scale, fittowidth, fittoheight) ;
VALUES (tnWB, lnShId, tcSheetName, tnState, 0.75, 0.75, 0.75, 0.75, 0.3, 0.3, False, 0, 0, PORTRAIT_PRINT_ORIENTATION, 0, 0, 0, 100, 0, 0)
lnRelId = This.GetNextId(tnWB, "xl_relationships")
Insert Into xl_relationships (workbook, Sheet, relid, reltype, Target) Values (tnWB, lnShId, lnRelId, "worksheet", "worksheets/sheet" + Transform(lnShId) + ".xml")
Endif
Endif
Endwith
Return lnShId
*-- Adds a string value to the internal cursor
Protected Procedure AddStringValue
Lparameters tnWB, tcString, tlInLine
Local lcStringXml, lnStringId, llPresrvSp, lcCheckSum
With This
If tlInLine
llPresrvSp = Iif(Left(tcString, 1) = " ", True, False)
lcStringXml = .GetXMLString(tcString)
lnStringId = .GetNextId(tnWB, "xl_strings")
lcCheckSum = .GetCheckSum(tcString)
Insert Into xl_strings (Id, workbook, checksum, stringxml, stringval, presvspace, Formatted) ;
VALUES (lnStringId, tnWB, lcCheckSum, lcStringXml, tcString, llPresrvSp, True)
Else
If .GetStringRecord(tnWB, tcString) And xl_strings.Formatted = False
lnStringId = xl_strings.Id
Else
llPresrvSp = Iif(Left(tcString, 1) = " " Or Right(tcString, 1) = " ", True, False)
lcStringXml = .GetXMLString(tcString)
lnStringId = .GetNextId(tnWB, "xl_strings")
lcCheckSum = .GetCheckSum(tcString)
Insert Into xl_strings (Id, workbook, checksum, stringxml, stringval, presvspace, Formatted) ;
VALUES (lnStringId, tnWB, lcCheckSum, lcStringXml, tcString, llPresrvSp, False)
Endif
Endif
Endwith
Return lnStringId
*-- Adds to the cell style a border definition
Procedure AddStyleBorders
Lparameters tnWB, tnCellXfsId, tnBorders, tcStyle, tnColor
Local lnDiagDn, lnDiagUp, lcLStyle, lnLColor, lcRStyle, lnRColor, lcTStyle, lnTColor, lcBStyle, lnBColor
Local lcDStyle, lnDColor, lnBorderId
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tnBorders) != 'N' Or Empty(tnBorders) Or !Between(tnBorders, 0, 63)
tnBorders = 0
Endif
If !This.IsValidBorderStyle(tcStyle)
tcStyle = BORDER_STYLE_NONE
Endif
If Vartype(tnColor) != 'N' Or !Between(tnColor, 0, Rgb(255,255,255)) Or Empty(tnColor)
tnColor = Rgb(0, 0, 0)
Endif
Store 0 To lnDiagDn, lnDiagUp
Store Rgb(0,0,0) To lnLColor, lnRColor, lnTColor, lnBColor, lnDColor
Store BORDER_STYLE_NONE To lcLStyle, lcRStyle, lcTStyle, lcBStyle, lcDStyle
If Bittest(tnBorders, 0)
lcLStyle = tcStyle
lnLColor = tnColor
Endif
If Bittest(tnBorders, 1)
lcRStyle = tcStyle
lnRColor = tnColor
Endif
If Bittest(tnBorders, 2)
lcTStyle = tcStyle
lnTColor = tnColor
Endif
If Bittest(tnBorders, 3)
lcBStyle = tcStyle
lnBColor = tnColor
Endif
If Bittest(tnBorders, 4)
lcDStyle = tcStyle
lnDColor = tnColor
lnDiagDn = 1
Endif
If Bittest(tnBorders, 5)
lcDStyle = tcStyle
lnDColor = tnColor
lnDiagUp = 1
Endif
If This.GetBordersRecord(tnWB, lcLStyle, lnLColor, lcRStyle, lnRColor, lcTStyle, lnTColor, lcBStyle, lnBColor, lcDStyle, lnDColor, lnDiagDn, lnDiagUp)
lnBorderId = xl_borders.Id
Else
lnBorderId = This.GetNextId(tnWB, 'xl_borders')
Insert Into xl_borders (workbook, Id, lstyle, lcolor, rstyle, rcolor, tstyle, tcolor, bstyle, bcolor, dstyle, dcolor, diagdn, diagup) ;
VALUES (tnWB, lnBorderId, lcLStyle, lnLColor, lcRStyle, lnRColor, lcTStyle, lnTColor, lcBStyle, lnBColor, lcDStyle, lnDColor, lnDiagDn, lnDiagUp)
Endif
*-* Set the border id to the style definition
Replace xl_cellxfs.borderid With lnBorderId In xl_cellxfs
Return True
Else
Return False
Endif
*-- Adds to the cell style a fill definition
Procedure AddStyleFill
Lparameters tnWB, tnCellXfsId, tnFColor, tnBColor, tcPatternType
Local lnFillId, lnFIndex, lnBIndex
Do Case
Case Pcount() < 3
Return False
Case Pcount() = 3
tnBColor = tnFColor
tcPatternType = FILL_STYLE_SOLID
Case Pcount() = 4
tcPatternType = FILL_STYLE_SOLID
Endcase
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tnFColor) = 'N' And Between(tnFColor, 0, Rgb(255, 255, 255))
tnFColor = Floor(tnFColor)
lnFIndex = 0
Else
tnFColor = 0
lnFIndex = 64
Endif
If Vartype(tnBColor) = 'N' And Between(tnBColor, 0, Rgb(255, 255, 255))
tnBColor = Floor(tnBColor)
lnBIndex = 0
Else
tnBColor = tnFColor
lnBIndex = lnFIndex
Endif
If Vartype(tcPatternType) != "C" Or Empty(tcPatternType)
tcPatternType = FILL_STYLE_SOLID
Endif
*-* Get the fill record id
If Seek(BinToC(tnWB)+BinToC(tnFColor)+BinToC(tnBColor)+tcPatternType, "xl_fills", "fillcolor")
lnFillId = xl_fills.Id
Replace xl_fills.Theme With 0, ;
xl_fills.tint With 0, ;
xl_fills.fgindexed With lnFIndex, ;
xl_fills.bgindexed With lnBIndex In xl_fills
Else
lnFillId = This.GetNextId(tnWB, 'xl_fills')
Insert Into xl_fills (workbook, Id, fgcolor, bgcolor, patttype, Theme, tint, fgindexed, bgindexed) ;
VALUES (tnWB, lnFillId, tnFColor, tnBColor, tcPatternType, 0, 0, lnFIndex, lnBIndex)
Endif
*-* Set the fill id to the style definition
Replace xl_cellxfs.fillid With lnFillId In xl_cellxfs
Return True
Else
Return False
Endif
*-- Adds to the cell style a font definition
Procedure AddStyleFont
Lparameters tnWB, tnCellXfsId, tcFName, tnFSize, tlBold, tlItalic, tnFColor, tcULine, tlStrikThr, tcVPos
Local lnFontId
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tcFName) != 'C' Or Empty(tcFName)
tcFName = 'Calibri'
Endif
tcFName = Proper(Alltrim(tcFName))
tnFSize = Iif(Vartype(tnFSize) != 'N' Or Empty(tnFSize) Or tnFSize < 0.5, 11, Int(tnFSize))
If Vartype(tlBold) != 'L'
tlBold = False
Endif
If Vartype(tlItalic) != 'L'
tlItalic = False
Endif
tnFColor = Iif(Vartype(tnFColor) = 'N' And Between(tnFColor, 0, Rgb(255, 255, 255)), Floor(tnFColor), Rgb(0, 0, 0))
If Vartype(tlStrikThr) != 'L'
tlStrikThr = False
Endif
If Vartype(tcULine) != 'C' Or !Inlist(tcULine, UNDERLINE_SINGLE, UNDERLINE_DOUBLE, UNDERLINE_SINGLEACCOUNTING, UNDERLINE_DOUBLEACCOUNTING, UNDERLINE_NONE)
tcULine = UNDERLINE_NONE
Endif
If Vartype(tcVPos) != 'C' Or !Inlist(tcVPos, FONT_VERTICAL_BASELINE, FONT_VERTICAL_SUBSCRIPT, FONT_VERTICAL_SUPERSCRIPT)
tcVPos = FONT_VERTICAL_BASELINE
Endif
*-* Get the font record id
If Seek(BinToC(tnWB)+Padr(tcFName, 100) + Str(tnFSize, 5, 1) + Transform(tlBold) + Transform(tlItalic) + Padl(tnFColor, 15) + Padr(tcULine, 16) + ;
TRANSFORM(tlStrikThr) + Padr(tcVPos, 11), "xl_fonts", "cellformat")
lnFontId = xl_fonts.Id
Replace xl_fonts.Theme With 0, ;
xl_fonts.tint With 0, ;
xl_fonts.indexed With 0 In xl_fonts
Else
lnFontId = This.GetNextId(tnWB, 'xl_fonts')
Insert Into xl_fonts (workbook, Id, fname, Fsize, fbold, fitalic, fcolor, uline, strkthr, fvpos) ;
VALUES (tnWB, lnFontId, tcFName, tnFSize, tlBold, tlItalic, tnFColor, tcULine, tlStrikThr, tcVPos)
Endif
*-* Set the font id to the style definition
Replace xl_cellxfs.fontid With lnFontId In xl_cellxfs
Return True
Else
Return False
Endif
*-- Adds to the cell style horizontal cell alignment
Procedure AddStyleHorizAlignment
Lparameters tnWB, tnCellXfsId, tcHorizAlign
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tcHorizAlign) = 'C' And Inlist(tcHorizAlign, CELL_HORIZ_ALIGN_LEFT, CELL_HORIZ_ALIGN_RIGHT, CELL_HORIZ_ALIGN_CENTER)
Replace xl_cellxfs.halign With tcHorizAlign In xl_cellxfs
Return True
Else
Return False
Endif
Else
Return False
Endif
*-- Adds to the style indent definition
Procedure AddStyleIndent
Lparameters tnWB, tnCellXfsId, tnIndent
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tnIndent) = 'N'
Replace xl_cellxfs.indent With tnIndent In xl_cellxfs
Return True
Else
Return False
Endif
Else
Return False
Endif
*-- Adds to the style definition numeric format
Procedure AddStyleNumericFormat
Lparameters tnWB, tnCellXfsId, tnNumFmtId
Local lnNumFmtId
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
Do Case
Case Vartype(tnNumFmtId) != 'N'
Return False
Case tnNumFmtId < START_NUMERIC_FORMAT_ID
lnNumFmtId = tnNumFmtId
Case tnNumFmtId >= START_NUMERIC_FORMAT_ID
If Seek(BinToC(tnWB)+BinToC(tnNumFmtId), "xl_numfmts", "id")
lnNumFmtId = xl_numFmts.Id
Else
If !This.AddClassDefinedNumericFormats(tnWB, tnNumFmtId)
Return False
Endif
lnNumFmtId = tnNumFmtId
Endif
Otherwise
Return False
Endcase
Replace xl_cellxfs.numfmtid With lnNumFmtId In xl_cellxfs
Return True
Else
Return False
Endif
*-- Adds to the style definition text rotation
Procedure AddStyleTextRotation
Lparameters tnWB, tnCellXfsId, tnRotation
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tnRotation) = 'N' And Between(tnRotation, -90, 90)
Replace xl_cellxfs.Rotation With tnRotation In xl_cellxfs
Return True
Else
Return False
Endif
Else
Return False
Endif
*-- Adds to the cell style vertical cell alignment
Procedure AddStyleVertAlignment
Lparameters tnWB, tnCellXfsId, tcVertAlign
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tcVertAlign) = 'C' And Inlist(tcVertAlign, CELL_VERT_ALIGN_TOP, CELL_VERT_ALIGN_BOTTOM, CELL_VERT_ALIGN_CENTER)
Replace xl_cellxfs.valign With tcVertAlign In xl_cellxfs
Return True
Else
Return False
Endif
Else
Return False
Endif
*-- Adds to the style definition word wrap
Procedure AddStyleWordWrap
Lparameters tnWB, tnCellXfsId, tlWordWrap
If Pcount() < 3
Return False
Endif
If Seek(BinToC(tnWB)+BinToC(tnCellXfsId), "xl_cellxfs", "id")
If Vartype(tlWordWrap) = 'L'
Replace xl_cellxfs.wraptext With Iif(tlWordWrap, 1, 0) In xl_cellxfs
Return True
Else
Return False
Endif
Else
Return False
Endif
*-- Copies the formatting of a source cell to other cells
Procedure CellFormatPainter
Lparameters tnWB, tnSheet, tnSrcRow, tnSrcCol, tnBegRow, tnBegCol, tnEndRow, tnEndCol
Local lnCellXfs, lnNumDec, llReturn, lnRow, lnCol
Do Case
Case Pcount() < 6
Return False
Case Pcount() = 6
tnEndRow = tnBegRow
tnEndCol = tnBegCol
Case Pcount() = 7
tnEndCol = tnBegCol
Endcase
If This.GetCellRecord(tnWB, tnSheet, tnSrcRow, tnSrcCol)
lnCellXfs = xl_cells.cellxfs
lnNumDec = xl_cells.numdec
For lnRow=tnBegRow To tnEndRow
For lnCol=tnBegCol To tnEndCol
If This.GetCellRecord(tnWB, tnSheet, lnRow, lnCol)
Replace xl_cells.cellxfs With lnCellXfs, ;
xl_cells.numdec With lnNumDec In xl_cells
Else
Insert Into xl_cells (workbook, Sheet, cellrow, cellcol, DataType, cellxfs, celldeleted, numdec, validndx) ;
VALUES (tnWB, tnSheet, lnRow, lnCol, DATA_TYPE_NONE, lnCellXfs, False, lnNumDec, 0)
Endif
Endfor
Endfor
llReturn = True
Else
llReturn = False
Endif
Return llReturn
*-- Converts a 'AA444' cell reference to the row and column index values
Procedure CellRefAsciiToIndex
Lparameters tcCellRef
Local lnCellCol, lnNdx, lnCellRow, loCellRef
lnCellCol = This.ColumnAsciiToIndex(tcCellRef)
lnCellRow = 0
For lnNdx=1 To Len(tcCellRef)
If Isdigit(Substr(tcCellRef, lnNdx))
lnCellRow = Int(Val(Substr(tcCellRef, lnNdx)))
Exit
Endif
Endfor
loCellRef = Createobject("Empty")
AddProperty(loCellRef, "Column", lnCellCol)
AddProperty(loCellRef, "Row", lnCellRow)
Return loCellRef
*-- Clears/deletes the named range from the workbook
Procedure ClearCellValidation
Lparameters tnWB, tcName
If Pcount() < 2
Return False
Endif
If Vartype(tcName) != "C"
Return False
Endif
If Seek(BinToC(tnWB)+Padl(tcName, 254, " "), "xl_namerange", "wbrname")
Delete In xl_namerange
llReturn = True
Else
llReturn = False
Endif
Return llReturn
*-- Clears the cell value
Procedure ClearCellValue
Lparameters tnWB, tnSheet, tnCellRow, tnCellCol
Local llReturn
If This.GetCellRecord(tnWB, tnSheet, tnCellRow, tnCellCol)
Replace xl_cells.celldeleted With True In xl_cells
llReturn = True
Else
llReturn = False
Endif
Return llReturn
*-- Converts Excel Ascii column value to index value
Procedure ColumnAsciiToIndex
Lparameters tcColIndex
Local lnIndex, lnNdx, lnChr
lnIndex = 0
For lnNdx=1 To Len(tcColIndex)
lnChr = Asc(Upper(Substr(tcColIndex, lnNdx, 1)))
If Between(lnChr, 65, 90)
lnIndex = (lnChr - 64) + (lnIndex * 26)
Else
Exit
Endif
Endfor
Return lnIndex
*-- Gets the column ASCII code for the column index
Procedure ColumnIndexToAscii
Lparameters tnCol
Local lnRemCol, lcColumn, lnPartCol
If Pcount() = 0 Or Vartype(tnCol) != "N"
Return ""
Endif
Try
lnRemCol = tnCol
lcColumn = ""
Do While lnRemCol > 0
lnPartCol = Mod(lnRemCol, 26)
If lnPartCol = 0
lnPartCol = 26
Endif
lnRemCol = (lnRemCol - lnPartCol) / 26
lcColumn = Chr(lnPartCol + 64) + lcColumn
Enddo
Catch To loException
Set Step On
Endtry
Return lcColumn
*-- Converts a color value (integer) to Hex representation
Protected Procedure ConvertColorToHex
Lparameters tnColor
Local lnRed, lnGreen, lnBlue, lcHexRed, lcHexGreen, lcHexBlue
With This
lnRed = .GetRGBValues(tnColor, 'R')
lnGreen = .GetRGBValues(tnColor, 'G')
lnBlue = .GetRGBValues(tnColor, 'B')
lcHexRed = Right(Transform(lnRed, '@0'), 2)
lcHexGreen = Right(Transform(lnGreen, '@0'), 2)
lcHexBlue = Right(Transform(lnBlue, '@0'), 2)
Endwith
Return "FF" + Upper(lcHexRed + lcHexGreen + lcHexBlue)
*-- ConvertHexStringToNumeric
Protected Procedure ConvertHexStringToNumeric
Lparameters tcHexNum
Local lcValue
If Empty(tcHexNum)
Return 0
Endif
If Len(tcHexNum) > 6
tcHexNum = Substr(tcHexNum, 3)
Endif
tcHexNum = Upper(Alltrim(tcHexNum))
tcHexNum = Right(tcHexNum, 2) + Substr(tcHexNum, 3, 2) + Left(tcHexNum, 2)
lcValue = "0x" + tcHexNum
Return Eval(lcValue)
*-- Converts Pixel measurement to Excel measurement
Procedure ConvertPixelsToExcelUnits
Lparameters tnPixels
Return tnPixels * 0.152542
*-- Creates the Excel file from the components
Protected Procedure CreateExcelFile
Lparameters tcTempPath, tcFilePath, tcWBName
Local loShell, loFolder, lcZipFile, loException, loFile, lnCountBefore, lcFile, lcZipPath, lnCount, lhFile
lcZipPath = Alltrim(tcFilePath)
If Empty(lcZipPath)
lcZipPath = Sys(5) + Sys(2003)
Endif
lcZipFile = Addbs(lcZipPath) + Forceext(Alltrim(tcWBName), "zip")
*-* Delete any prior file with same Excel name
If File(Forceext(lcZipFile, "xlsx"))
Try
Erase (Forceext(lcZipFile, "xlsx"))
llError = False
Catch To loException
This.ErrorLevelId = 10
Raiseevent(This, "OnShowErrorMessage", 10, "Error - unable to delete existing file" + CR + loException.Message)
llError = True
Endtry
If llError
Return False
Endif
Endif
*-* Validate that the file name and path are valid
Try
lhFile = Fcreate(lcZipFile, 0)
Fclose(lhFile)
Catch To loException
This.ErrorLevelId = 11
Raiseevent(This, "OnShowErrorMessage", 11, "CreateExcelFile - Failed to create Zip file"+ CR + loException.Message)
Return False
Endtry
*-* Create base zip file
Strtofile(Chr(80)+Chr(75)+Chr(5)+Chr(6)+Replicate(Chr(0), 18), lcZipFile, 0)
loShell = Createobject("shell.application")
loFolder = loShell.NameSpace(tcTempPath).Items
*-* Add the files to the zip
If Os(3)<'6' Or Os(3)='6' And Os(4)<'1'
Try
For Each loFile In loFolder
loShell.NameSpace(lcZipFile).MoveHere(loFile, FOF_SILENT)
apiSleep(100)
Endfor
Catch To loException
Set Step On
Endtry
llErr = True
Do While llErr
Try
apiMoveFile(lcZipFile, Forceext(lcZipFile, "xlsx"))
llErr = False
Catch
apiSleep(100)
Endtry
Enddo
Else
Try
For Each loFile In loFolder
lnCountBefore = loShell.NameSpace(tcTempPath).Items.Count
loShell.NameSpace(lcZipFile).MoveHere(loFile, FOF_SILENT)
Do While lnCountBefore = loShell.NameSpace(tcTempPath).Items.Count
apiSleep(50)
Enddo
Endfor
Catch To loException
This.ErrorLevelId = 12
Raiseevent(This, "OnShowErrorMessage", 12, "CreateExcelFile - Failed to add contents to Zip file"+ CR + loException.Message)
Set Step On
Erase (lcZipFile)
Do Case
Case loException.ErrorNo = 1943
Otherwise
Set Step On
Endcase
Endtry
Try
apiMoveFile(lcZipFile, Forceext(lcZipFile, "xlsx"))
Catch To loException
This.ErrorLevelId = 13
Raiseevent(This, "OnShowErrorMessage", 13, "CreateExcelFile - Rename failed"+ CR + loException.Message)
Set Step On
Endtry
Endif
Return True
*-- Creates a new format style definition
Procedure CreateFormatStyle
Lparameters tnWB
If Pcount() > 0 And Seek(BinToC(tnWB), "xl_workbooks", "workbook")
lnId = This.GetNextId(tnWB, "xl_cellxfs")
Insert Into xl_cellxfs (workbook, Id, numfmtid, fontid, fillid, borderid, halign, valign, indent, wraptext, Rotation) ;
VALUES (tnWB, lnId, 0, 0, 0, 0, "", "", 0, 0, 0)
Return lnId
Else
Return -1
Endif
*-- Creates an in-line text formatted definition
Procedure CreateInlineFormatText
Lparameters tnWB, tcCellText
Local loInline
loInline = Createobject("Empty")
AddProperty(loInline, "Workbook", tnWB)
AddProperty(loInline, "StringId", Null)
AddProperty(loInline, "StringValue", tcCellText)
AddProperty(loInline, "Count", 0)
AddProperty(loInline, "Characters[1]")
loInline.Characters[1] = Null
Return loInline
*-- Creates a new workbook object
Procedure CreateWorkbook
Lparameters tcName
Local lcWBName, lcPath, lnWB
If Pcount() = 1