-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWorksheetFunctions.cs
3510 lines (3131 loc) · 154 KB
/
WorksheetFunctions.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using A = DocumentFormat.OpenXml.Drawing;
using Xdr = DocumentFormat.OpenXml.Drawing.Spreadsheet;
using C = DocumentFormat.OpenXml.Drawing.Charts;
using X14 = DocumentFormat.OpenXml.Office2010.Excel;
using SLA = SpreadsheetLight.Drawing;
namespace SpreadsheetLight
{
public partial class SLDocument
{
/// <summary>
/// Adds a new worksheet, and selects the new worksheet as the active one.
/// </summary>
/// <param name="WorksheetName">The name should not be blank, nor exceed 31 characters. And it cannot contain these characters: \/?*[] It cannot be the same as an existing name (case-insensitive). But there's nothing stopping you from using 3 spaces as a name.</param>
/// <returns>True if the name is valid and the worksheet is successfully added. False otherwise.</returns>
public bool AddWorksheet(string WorksheetName)
{
if (!SLTool.CheckSheetChartName(WorksheetName))
{
return false;
}
foreach (SLSheet sheet in slwb.Sheets)
{
if (sheet.Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
// if there's at least one worksheet, then there's at least a selected worksheet.
if (slwb.Sheets.Count > 0)
{
WriteSelectedWorksheet();
}
gsSelectedWorksheetName = WorksheetName;
++giWorksheetIdCounter;
// use an empty string for the Id first
slwb.Sheets.Add(new SLSheet(WorksheetName, (uint)giWorksheetIdCounter, string.Empty, SLSheetType.Worksheet));
giSelectedWorksheetID = (uint)giWorksheetIdCounter;
gsSelectedWorksheetRelationshipID = string.Empty;
slws = new SLWorksheet(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors, SimpleTheme.ThemeColumnWidth, SimpleTheme.ThemeColumnWidthInEMU, SimpleTheme.ThemeMaxDigitWidth, SimpleTheme.listColumnStepSize, SimpleTheme.ThemeRowHeight);
IsNewWorksheet = true;
return true;
}
/// <summary>
/// Select an existing worksheet. If the given name doesn't match an existing worksheet, the previously selected worksheet is used.
/// </summary>
/// <param name="WorksheetName">The name of an existing worksheet.</param>
/// <returns>True if there's an existing worksheet with that name and that worksheet is successfully selected. False otherwise.</returns>
public bool SelectWorksheet(string WorksheetName)
{
// if the current worksheet is already selected, no need to select again, right?
if (gsSelectedWorksheetName.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase))
{
return true;
// originally return false. A fellow developer contacted me that this doesn't quite
// fit the explanation of the return value. And I agree.
// Frankly, I didn't expect anyone to actually use the return value...
// I should've used a void as the return type, but now it's too late...
}
uint iNewlySelectedWorksheetID = 0;
string sNewlySelectedWorksheetRelationshipID = string.Empty;
bool bFound = false;
for (int i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase)
&& slwb.Sheets[i].SheetType == SLSheetType.Worksheet)
{
bFound = true;
iNewlySelectedWorksheetID = slwb.Sheets[i].SheetId;
sNewlySelectedWorksheetRelationshipID = slwb.Sheets[i].Id;
break;
}
}
if (!bFound)
{
return false;
}
// if there's at least one worksheet, then there's at least a selected worksheet.
if (slwb.Sheets.Count > 0)
{
WriteSelectedWorksheet();
}
giSelectedWorksheetID = iNewlySelectedWorksheetID;
gsSelectedWorksheetName = WorksheetName;
gsSelectedWorksheetRelationshipID = sNewlySelectedWorksheetRelationshipID;
LoadSelectedWorksheet();
IsNewWorksheet = false;
return true;
}
/// <summary>
/// Show the worksheet (a.k.a unhide worksheet). This includes chart sheets, dialog sheets and macro sheets.
/// </summary>
/// <param name="WorksheetName">The name of the worksheet.</param>
public void ShowWorksheet(string WorksheetName)
{
this.ShowHideWorksheet(WorksheetName, SheetStateValues.Visible);
}
/// <summary>
/// Hide the worksheet. This includes chart sheets, dialog sheets and macro sheets.
/// </summary>
/// <param name="WorksheetName">The name of the worksheet.</param>
public void HideWorksheet(string WorksheetName)
{
this.ShowHideWorksheet(WorksheetName, SheetStateValues.Hidden);
}
/// <summary>
/// Hide the worksheet. This includes chart sheets, dialog sheets and macro sheets.
/// </summary>
/// <param name="WorksheetName">The name of the worksheet.</param>
/// <param name="IsVeryHidden">True to set the worksheet as very hidden. False otherwise.</param>
public void HideWorksheet(string WorksheetName, bool IsVeryHidden)
{
this.ShowHideWorksheet(WorksheetName, IsVeryHidden ? SheetStateValues.VeryHidden : SheetStateValues.Hidden);
}
private void ShowHideWorksheet(string WorksheetName, SheetStateValues State)
{
for (int i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase))
{
slwb.Sheets[i].State = State;
break;
}
}
}
/// <summary>
/// Indicates if the worksheet is hidden. Note that if the worksheet name isn't of an existing worksheet, the return value is true. Think of a non-existent worksheet as very very hidden, like hidden in another dimension or something.
/// </summary>
/// <param name="WorksheetName">The name of the worksheet.</param>
/// <returns>True if the worksheet is hidden. False otherwise.</returns>
public bool IsWorksheetHidden(string WorksheetName)
{
bool result = true;
for (int i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase)
&& slwb.Sheets[i].State == SheetStateValues.Visible)
{
result = false;
break;
}
}
return result;
}
/// <summary>
/// Rename an existing worksheet. This includes chart sheets, dialog sheets and macro sheets.
/// </summary>
/// <param name="ExistingWorksheetName">The name of the existing worksheet.</param>
/// <param name="NewWorksheetName">The new name for the existing worksheet. The name should not be blank, nor exceed 31 characters. And it cannot contain these characters: \/?*[] It cannot be the same as an existing name (case-insensitive).</param>
/// <returns>True if renaming is successful. False otherwise.</returns>
public bool RenameWorksheet(string ExistingWorksheetName, string NewWorksheetName)
{
if (!SLTool.CheckSheetChartName(NewWorksheetName))
{
return false;
}
foreach (SLSheet sheet in slwb.Sheets)
{
if (sheet.Name.Equals(NewWorksheetName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
int i;
bool bFound = false;
for (i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].Name.Equals(ExistingWorksheetName, StringComparison.OrdinalIgnoreCase))
{
slwb.Sheets[i].Name = NewWorksheetName;
bFound = true;
break;
}
}
if (!bFound)
{
return false;
}
if (gsSelectedWorksheetName.Equals(ExistingWorksheetName, StringComparison.OrdinalIgnoreCase))
{
gsSelectedWorksheetName = NewWorksheetName;
}
// External reference
// =SUM([Budget.xlsx]Annual!C10:C25)
// When the source is not open, the external reference includes the entire path.
// External reference
// =SUM('C:\Reports\[Budget.xlsx]Annual'!C10:C25)
// need to check for invariant/case-insensitive?
// I'm going to partially ignore workbook references, meaning [Budget.xlsx]Annual! is still
// kind of ok..
// This is because if the sheet name is [Budget.xlsx]1999's Annual then it becomes
// "'[Budget.xlsx]1999''s Annual'". At least I think that's what it becomes.
// The rules for single quotes are ... obscure...
// Hopefully there aren't any workbook references at all.
string sPattern = @"(?<pre>^|[^\]])" + SLTool.FormatWorksheetNameForFormula(ExistingWorksheetName) + "!";
string sReplacement = "${pre}" + SLTool.FormatWorksheetNameForFormula(NewWorksheetName) + "!";
foreach (SLDefinedName dn in slwb.DefinedNames)
{
dn.Text = Regex.Replace(dn.Text, sPattern, sReplacement);
}
// TODO update for cells in other worksheets
// One of the few cases where having a shared formula repository is useful... (like shared strings)
SLCell c;
List<SLCellPoint> listCellKeys = slws.Cells.Keys.ToList<SLCellPoint>();
for (i = 0; i < listCellKeys.Count; ++i)
{
c = slws.Cells[listCellKeys[i]];
if (c.CellText != null && c.CellText.StartsWith("="))
{
c.CellText = Regex.Replace(c.CellText, sPattern, sReplacement);
}
if (c.CellFormula != null)
{
c.CellFormula.FormulaText = Regex.Replace(c.CellFormula.FormulaText, sPattern, sReplacement);
}
slws.Cells[listCellKeys[i]] = c;
}
// this updates an chart references
foreach (WorksheetPart wsp in xl.WorkbookPart.WorksheetParts)
{
if (wsp.DrawingsPart != null)
{
C.Chart chartOriginal;
C.Chart chartNew;
// I'm looking for something like the following:
//<c:f>AwesomeName!$A$2:$A$4</c:f>
// An alternative might be:
//<f>AwesomeName!$A$2:$A$4</f>
// In case the XML tag prefix is not there, hence the smiley face <:
// I'm replacing the entire inner XML, so it's best to be as specific as possible.
string sReplaceExisting = "(?<pre>[<:]f>)" + SLTool.FormatWorksheetNameForFormula(ExistingWorksheetName) + "(?<post>!\\$[A-Z]{1,3}\\$)";
string sReplaceNew = "${pre}" + SLTool.FormatWorksheetNameForFormula(NewWorksheetName) + "${post}";
foreach (ChartPart cp in wsp.DrawingsPart.ChartParts)
{
chartOriginal = cp.ChartSpace.Elements<C.Chart>().First();
chartNew = (C.Chart)chartOriginal.CloneNode(true);
if (Regex.IsMatch(chartNew.InnerXml, sReplaceExisting))
{
chartNew.InnerXml = Regex.Replace(chartNew.InnerXml, sReplaceExisting, sReplaceNew);
cp.ChartSpace.ReplaceChild<C.Chart>(chartNew, chartOriginal);
cp.ChartSpace.Save();
}
}
}
}
return true;
}
/// <summary>
/// Copy the contents of a worksheet to another worksheet. The current worksheet cannot be the source of the copy operation, nor the destination of the copy operation.
/// </summary>
/// <param name="ExistingWorksheetName">The worksheet to be copied from. This cannot be the current worksheet.</param>
/// <param name="NewWorksheetName">The worksheet to be copied to. If this doesn't exist, a new worksheet is created. If it's an existing worksheet, the contents of the existing worksheet will be overwritten. The new worksheet cannot be the currently selected worksheet.</param>
/// <returns>True if copying is successful. False otherwise</returns>
public bool CopyWorksheet(string ExistingWorksheetName, string NewWorksheetName)
{
if (ExistingWorksheetName.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase)
|| NewWorksheetName.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase)
|| ExistingWorksheetName.Equals(NewWorksheetName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
bool result = false;
bool bExistingFound = false;
string sExistingRelId = string.Empty;
bool bNewFound = false;
string sNewRelId = string.Empty;
foreach (SLSheet sheet in slwb.Sheets)
{
if (sheet.Name.Equals(ExistingWorksheetName, StringComparison.OrdinalIgnoreCase)
&& sheet.SheetType == SLSheetType.Worksheet)
{
sExistingRelId = sheet.Id;
bExistingFound = true;
}
else if (sheet.Name.Equals(NewWorksheetName, StringComparison.OrdinalIgnoreCase))
{
sNewRelId = sheet.Id;
bNewFound = true;
}
}
// no existing worksheet found! So just return. Nothing to copy!
if (!bExistingFound) return false;
// destination worksheet not an existing worksheet, but the name failed the test
if (!bNewFound && !SLTool.CheckSheetChartName(NewWorksheetName)) return false;
WorksheetPart wspExisting = (WorksheetPart)wbp.GetPartById(sExistingRelId);
WorksheetPart wspNew = wbp.AddNewPart<WorksheetPart>();
using (StreamReader sr = new StreamReader(wspExisting.GetStream()))
{
using (StreamWriter sw = new StreamWriter(wspNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
ImagePart imgpNew;
#region ControlPropertiesParts
if (wspExisting.ControlPropertiesParts != null)
{
ControlPropertiesPart cppNew;
foreach (ControlPropertiesPart cpp in wspExisting.ControlPropertiesParts)
{
cppNew = wspNew.AddNewPart<ControlPropertiesPart>(wspExisting.GetIdOfPart(cpp));
using (StreamReader sr = new StreamReader(cpp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(cppNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
#endregion
#region CustomPropertyParts
if (wspExisting.CustomPropertyParts != null)
{
CustomPropertyPart cppNew;
foreach (CustomPropertyPart cpp in wspExisting.CustomPropertyParts)
{
cppNew = wspNew.AddCustomPropertyPart(cpp.ContentType, wspExisting.GetIdOfPart(cpp));
using (StreamReader sr = new StreamReader(cpp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(cppNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
#endregion
#region DrawingsPart
if (wspExisting.DrawingsPart != null)
{
DrawingsPart dp = wspExisting.DrawingsPart;
DrawingsPart dpNew = wspNew.AddNewPart<DrawingsPart>(wspExisting.GetIdOfPart(dp));
using (StreamReader sr = new StreamReader(dp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(dpNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
#region DrawingsPart ChartParts
ChartPart cpNew;
foreach (ChartPart cp in dp.ChartParts)
{
cpNew = dpNew.AddNewPart<ChartPart>(dp.GetIdOfPart(cp));
this.FeedDataChartPart(cpNew, cp);
}
#endregion
#region DrawingsPart CustomXmlParts
if (dp.CustomXmlParts != null)
{
CustomXmlPart cxpNew;
foreach (CustomXmlPart cxp in dp.CustomXmlParts)
{
cxpNew = dpNew.AddCustomXmlPart(cxp.ContentType, dp.GetIdOfPart(cxp));
this.FeedDataCustomXmlPart(cxpNew, cxp);
}
}
#endregion
#region DrawingsPart DiagramColorsParts
if (dp.DiagramColorsParts != null)
{
DiagramColorsPart dcpNew;
foreach (DiagramColorsPart dcp in dp.DiagramColorsParts)
{
dcpNew = dpNew.AddNewPart<DiagramColorsPart>(dp.GetIdOfPart(dcp));
this.FeedDataDiagramColorsPart(dcpNew, dcp);
}
}
#endregion
#region DrawingsPart DiagramDataParts
if (dp.DiagramDataParts != null)
{
DiagramDataPart ddpNew;
foreach (DiagramDataPart ddp in dp.DiagramDataParts)
{
ddpNew = dpNew.AddNewPart<DiagramDataPart>(dp.GetIdOfPart(ddp));
this.FeedDataDiagramDataPart(ddpNew, ddp);
}
}
#endregion
#region DrawingsPart DiagramLayoutDefinitionParts
if (dp.DiagramLayoutDefinitionParts != null)
{
DiagramLayoutDefinitionPart dldpNew;
foreach (DiagramLayoutDefinitionPart dldp in dp.DiagramLayoutDefinitionParts)
{
dldpNew = dpNew.AddNewPart<DiagramLayoutDefinitionPart>(dp.GetIdOfPart(dldp));
this.FeedDataDiagramLayoutDefinitionPart(dldpNew, dldp);
}
}
#endregion
#region DrawingsPart DiagramPersistLayoutParts
if (dp.DiagramPersistLayoutParts != null)
{
DiagramPersistLayoutPart dplpNew;
foreach (DiagramPersistLayoutPart dplp in dp.DiagramPersistLayoutParts)
{
dplpNew = dpNew.AddNewPart<DiagramPersistLayoutPart>(dp.GetIdOfPart(dplp));
this.FeedDataDiagramPersistLayoutPart(dplpNew, dplp);
}
}
#endregion
#region DrawingsPart DiagramStyleParts
if (dp.DiagramStyleParts != null)
{
DiagramStylePart dspNew;
foreach (DiagramStylePart dsp in dp.DiagramStyleParts)
{
dspNew = dpNew.AddNewPart<DiagramStylePart>(dp.GetIdOfPart(dsp));
this.FeedDataDiagramStylePart(dspNew, dsp);
}
}
#endregion
#region DrawingsPart ImageParts
foreach (ImagePart imgp in dp.ImageParts)
{
imgpNew = dpNew.AddImagePart(imgp.ContentType, dp.GetIdOfPart(imgp));
this.FeedDataImagePart(imgpNew, imgp);
}
#endregion
}
#endregion
#region EmbeddedControlPersistenceBinaryDataParts
if (wspExisting.EmbeddedControlPersistenceBinaryDataParts != null)
{
EmbeddedControlPersistenceBinaryDataPart binNew;
foreach (EmbeddedControlPersistenceBinaryDataPart bin in wspExisting.EmbeddedControlPersistenceBinaryDataParts)
{
binNew = wspNew.AddEmbeddedControlPersistenceBinaryDataPart(bin.ContentType, wspExisting.GetIdOfPart(bin));
this.FeedDataEmbeddedControlPersistenceBinaryDataPart(binNew, bin);
}
}
#endregion
#region EmbeddedControlPersistenceParts
if (wspExisting.EmbeddedControlPersistenceParts != null)
{
EmbeddedControlPersistencePart ecppNew;
foreach (EmbeddedControlPersistencePart ecpp in wspExisting.EmbeddedControlPersistenceParts)
{
ecppNew = wspNew.AddEmbeddedControlPersistencePart(ecpp.ContentType, wspExisting.GetIdOfPart(ecpp));
this.FeedDataEmbeddedControlPersistencePart(ecppNew, ecpp);
}
}
#endregion
#region EmbeddedObjectParts
if (wspExisting.EmbeddedObjectParts != null)
{
EmbeddedObjectPart eopNew;
foreach (EmbeddedObjectPart eop in wspExisting.EmbeddedObjectParts)
{
eopNew = wspNew.AddEmbeddedObjectPart(eop.ContentType);
this.FeedDataEmbeddedObjectPart(eopNew, eop);
}
}
#endregion
#region EmbeddedPackageParts
if (wspExisting.EmbeddedPackageParts != null)
{
EmbeddedPackagePart eppNew;
foreach (EmbeddedPackagePart epp in wspExisting.EmbeddedPackageParts)
{
eppNew = wspNew.AddEmbeddedPackagePart(epp.ContentType);
this.FeedDataEmbeddedPackagePart(eppNew, epp);
}
}
#endregion
#region HyperlinkRelationships
if (wspExisting.HyperlinkRelationships.Count() > 0)
{
foreach (HyperlinkRelationship hlrel in wspExisting.HyperlinkRelationships)
{
wspNew.AddHyperlinkRelationship(hlrel.Uri, hlrel.IsExternal, hlrel.Id);
}
}
#endregion
#region ImageParts
if (wspExisting.ImageParts != null)
{
foreach (ImagePart imgp in wspExisting.ImageParts)
{
imgpNew = wspNew.AddImagePart(imgp.ContentType, wspExisting.GetIdOfPart(imgp));
this.FeedDataImagePart(imgpNew, imgp);
}
}
#endregion
#region PivotTableParts
if (wspExisting.PivotTableParts != null)
{
PivotTablePart ptpNew;
foreach (PivotTablePart ptp in wspExisting.PivotTableParts)
{
ptpNew = wspNew.AddNewPart<PivotTablePart>(wspExisting.GetIdOfPart(ptp));
using (StreamReader sr = new StreamReader(ptp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(ptpNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
if (ptp.PivotTableCacheDefinitionPart != null)
{
ptpNew.AddNewPart<PivotTableCacheDefinitionPart>(ptp.GetIdOfPart(ptp.PivotTableCacheDefinitionPart));
using (StreamReader sr = new StreamReader(ptp.PivotTableCacheDefinitionPart.GetStream()))
{
using (StreamWriter sw = new StreamWriter(ptpNew.PivotTableCacheDefinitionPart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
}
#endregion
#region QueryTableParts
if (wspExisting.QueryTableParts != null)
{
QueryTablePart qtpNew;
foreach (QueryTablePart qtp in wspExisting.QueryTableParts)
{
qtpNew = wspNew.AddNewPart<QueryTablePart>(wspExisting.GetIdOfPart(qtp));
using (StreamReader sr = new StreamReader(qtp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(qtpNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
#endregion
#region SingleCellTablePart
if (wspExisting.SingleCellTablePart != null)
{
wspNew.AddNewPart<SingleCellTablePart>(wspExisting.GetIdOfPart(wspExisting.SingleCellTablePart));
using (StreamReader sr = new StreamReader(wspExisting.SingleCellTablePart.GetStream()))
{
using (StreamWriter sw = new StreamWriter(wspNew.SingleCellTablePart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
#endregion
#region SlicersParts
if (wspExisting.SlicersParts != null)
{
SlicersPart spNew;
foreach (SlicersPart sp in wspExisting.SlicersParts)
{
spNew = wspNew.AddNewPart<SlicersPart>(wspExisting.GetIdOfPart(sp));
using (StreamReader sr = new StreamReader(sp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(spNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
#endregion
#region SpreadsheetPrinterSettingsParts
// What is inside this thing?!? An error occurs in the copied worksheet if the
// existing worksheet had tables. The copied worksheet doesn't need the printer
// settings of the original worksheet, right?
//if (wspExisting.SpreadsheetPrinterSettingsParts != null)
//{
// SpreadsheetPrinterSettingsPart spspNew;
// foreach (SpreadsheetPrinterSettingsPart spsp in wspExisting.SpreadsheetPrinterSettingsParts)
// {
// spspNew = wspNew.AddNewPart<SpreadsheetPrinterSettingsPart>(wspExisting.GetIdOfPart(spsp));
// using (StreamReader sr = new StreamReader(spsp.GetStream()))
// {
// using (StreamWriter sw = new StreamWriter(spspNew.GetStream(FileMode.Create)))
// {
// sw.Write(sr.ReadToEnd());
// }
// }
// }
//}
#endregion
#region TableDefinitionParts
if (wspExisting.TableDefinitionParts != null)
{
TableDefinitionPart tdpNew;
QueryTablePart qtpNew;
foreach (TableDefinitionPart tdp in wspExisting.TableDefinitionParts)
{
tdpNew = wspNew.AddNewPart<TableDefinitionPart>(wspExisting.GetIdOfPart(tdp));
using (StreamReader sr = new StreamReader(tdp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(tdpNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
slwb.RefreshPossibleTableId();
tdpNew.Table.Id = slwb.PossibleTableId;
tdpNew.Table.DisplayName = slwb.GetNextPossibleTableName();
tdpNew.Table.Name = tdpNew.Table.DisplayName.Value;
foreach (QueryTablePart qtp in tdp.QueryTableParts)
{
qtpNew = tdpNew.AddNewPart<QueryTablePart>(tdp.GetIdOfPart(qtp));
using (StreamReader sr = new StreamReader(qtp.GetStream()))
{
using (StreamWriter sw = new StreamWriter(qtpNew.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
}
}
#endregion
#region VmlDrawingParts
if (wspExisting.VmlDrawingParts != null)
{
VmlDrawingPart vdpNew;
foreach (VmlDrawingPart vdp in wspExisting.VmlDrawingParts)
{
vdpNew = wspNew.AddNewPart<VmlDrawingPart>(wspExisting.GetIdOfPart(vdp));
this.FeedDataVmlDrawingPart(vdpNew, vdp);
}
}
#endregion
#region WorksheetCommentsPart
if (wspExisting.WorksheetCommentsPart != null)
{
wspNew.AddNewPart<WorksheetCommentsPart>(wspExisting.GetIdOfPart(wspExisting.WorksheetCommentsPart));
using (StreamReader sr = new StreamReader(wspExisting.WorksheetCommentsPart.GetStream()))
{
using (StreamWriter sw = new StreamWriter(wspNew.WorksheetCommentsPart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
#endregion
#region WorksheetSortMapPart
if (wspExisting.WorksheetSortMapPart != null)
{
wspNew.AddNewPart<WorksheetSortMapPart>(wspExisting.GetIdOfPart(wspExisting.WorksheetSortMapPart));
using (StreamReader sr = new StreamReader(wspExisting.WorksheetSortMapPart.GetStream()))
{
using (StreamWriter sw = new StreamWriter(wspNew.WorksheetSortMapPart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
}
#endregion
if (!bNewFound)
{
++giWorksheetIdCounter;
slwb.Sheets.Add(new SLSheet(NewWorksheetName, (uint)giWorksheetIdCounter, wbp.GetIdOfPart(wspNew), SLSheetType.Worksheet));
}
else
{
wbp.DeletePart(sNewRelId);
foreach (SLSheet sheet in slwb.Sheets)
{
if (sheet.Name.Equals(NewWorksheetName, StringComparison.OrdinalIgnoreCase))
{
sheet.Id = wbp.GetIdOfPart(wspNew);
}
}
}
return result;
}
/// <summary>
/// Delete a worksheet. The currently selected worksheet cannot be deleted.
/// </summary>
/// <param name="WorksheetName">The name of the worksheet to be deleted.</param>
/// <returns>True if successful. False otherwise.</returns>
public bool DeleteWorksheet(string WorksheetName)
{
bool result = false;
if (!WorksheetName.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase))
{
string sRelId = string.Empty;
int i = 0;
for (i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].Name.Equals(WorksheetName, StringComparison.OrdinalIgnoreCase))
{
sRelId = slwb.Sheets[i].Id;
break;
}
}
if (sRelId.Length > 0)
{
result = true;
wbp.DeletePart(sRelId);
slwb.Sheets.RemoveAt(i);
}
}
return result;
}
/// <summary>
/// Get the worksheet name of the currently selected worksheet.
/// </summary>
/// <returns>The currently selected worksheet name.</returns>
public string GetCurrentWorksheetName()
{
return gsSelectedWorksheetName;
}
/// <summary>
/// Get a list of names of existing worksheets currently in the spreadsheet, excluding chart sheets, macro sheets and dialog sheets.
/// </summary>
/// <returns>A list of names of existing worksheets.</returns>
public List<string> GetSheetNames()
{
return this.GetSheetNames(false);
}
/// <summary>
/// Get a list of names of existing sheets currently in the spreadsheet.
/// </summary>
/// <param name="IncludeAll">True to include chart sheets, macro sheets and dialog sheets. False to limit to only worksheets.</param>
/// <returns>A list of names of existing sheets.</returns>
public List<string> GetSheetNames(bool IncludeAll)
{
List<string> list = new List<string>();
if (IncludeAll)
{
for (int i = 0; i < slwb.Sheets.Count; ++i)
{
list.Add(slwb.Sheets[i].Name);
}
}
else
{
// worksheets only
for (int i = 0; i < slwb.Sheets.Count; ++i)
{
if (slwb.Sheets[i].SheetType == SLSheetType.Worksheet) list.Add(slwb.Sheets[i].Name);
}
}
return list;
}
/// <summary>
/// Get statistical information on the currently selected worksheet. NOTE: The information is only current at point of retrieval.
/// </summary>
/// <returns>An SLWorksheetStatistics object with the information.</returns>
public SLWorksheetStatistics GetWorksheetStatistics()
{
SLWorksheetStatistics wsstats = new SLWorksheetStatistics();
List<SLCellPoint> listCellRefKeys = slws.Cells.Keys.ToList<SLCellPoint>();
listCellRefKeys.Sort(new SLCellReferencePointComparer());
List<int> intlist;
HashSet<int> hsRows = new HashSet<int>(listCellRefKeys.GroupBy(g => g.RowIndex).Select(s => s.Key).ToList<int>());
hsRows.UnionWith(slws.RowProperties.Keys.ToList<int>());
if (hsRows.Count > 0)
{
intlist = hsRows.ToList<int>();
intlist.Sort();
wsstats.iStartRowIndex = intlist[0];
wsstats.iEndRowIndex = intlist[intlist.Count - 1];
}
HashSet<int> hsColumns = new HashSet<int>(listCellRefKeys.GroupBy(g => g.ColumnIndex).Select(s => s.Key).ToList<int>());
hsColumns.UnionWith(slws.ColumnProperties.Keys.ToList<int>());
if (hsColumns.Count > 0)
{
intlist = hsColumns.ToList<int>();
intlist.Sort();
wsstats.iStartColumnIndex = intlist[0];
wsstats.iEndColumnIndex = intlist[intlist.Count - 1];
}
wsstats.iNumberOfRows = hsRows.Count;
wsstats.iNumberOfColumns = hsColumns.Count;
wsstats.iNumberOfCells = listCellRefKeys.Count;
wsstats.iNumberOfEmptyCells = 0;
SLCell c;
foreach (SLCellPoint pt in listCellRefKeys)
{
c = slws.Cells[pt];
if (c.CellText != null && c.CellText.Length == 0 && c.CellFormula == null)
{
++wsstats.iNumberOfEmptyCells;
}
}
return wsstats;
}
internal void FeedDataImagePart(ImagePart NewPart, ImagePart ExistingPart)
{
System.Drawing.Imaging.ImageFormat imgtype = SLTool.TranslateImageContentType(ExistingPart.ContentType);
using (System.Drawing.Bitmap bm = new System.Drawing.Bitmap(ExistingPart.GetStream()))
{
using (MemoryStream ms = new MemoryStream())
{
bm.Save(ms, imgtype);
ms.Position = 0;
NewPart.FeedData(ms);
}
}
}
internal void FeedDataSlidePart(SlidePart NewPart, SlidePart ExistingPart)
{
using (StreamReader sr = new StreamReader(ExistingPart.GetStream()))
{
using (StreamWriter sw = new StreamWriter(NewPart.GetStream(FileMode.Create)))
{
sw.Write(sr.ReadToEnd());
}
}
ImagePart imgpNew;
if (ExistingPart.ChartParts != null)
{
ChartPart cpNew;
foreach (ChartPart cp in ExistingPart.ChartParts)
{
cpNew = NewPart.AddNewPart<ChartPart>(ExistingPart.GetIdOfPart(cp));
this.FeedDataChartPart(cpNew, cp);
}
}
if (ExistingPart.CustomXmlParts != null)
{
CustomXmlPart cxpNew;
foreach (CustomXmlPart cxp in ExistingPart.CustomXmlParts)
{
cxpNew = NewPart.AddCustomXmlPart(cxp.ContentType, ExistingPart.GetIdOfPart(cxp));
this.FeedDataCustomXmlPart(cxpNew, cxp);
}
}
if (ExistingPart.DiagramColorsParts != null)
{
DiagramColorsPart dcpNew;
foreach (DiagramColorsPart dcp in ExistingPart.DiagramColorsParts)
{
dcpNew = NewPart.AddNewPart<DiagramColorsPart>(ExistingPart.GetIdOfPart(dcp));
this.FeedDataDiagramColorsPart(dcpNew, dcp);
}
}
if (ExistingPart.DiagramDataParts != null)
{
DiagramDataPart ddpNew;
foreach (DiagramDataPart ddp in ExistingPart.DiagramDataParts)
{
ddpNew = NewPart.AddNewPart<DiagramDataPart>(ExistingPart.GetIdOfPart(ddp));
this.FeedDataDiagramDataPart(ddpNew, ddp);
}
}
if (ExistingPart.DiagramLayoutDefinitionParts != null)
{
DiagramLayoutDefinitionPart dldpNew;
foreach (DiagramLayoutDefinitionPart dldp in ExistingPart.DiagramLayoutDefinitionParts)
{
dldpNew = NewPart.AddNewPart<DiagramLayoutDefinitionPart>(ExistingPart.GetIdOfPart(dldp));
this.FeedDataDiagramLayoutDefinitionPart(dldpNew, dldp);
}
}
if (ExistingPart.DiagramPersistLayoutParts != null)
{
DiagramPersistLayoutPart dplpNew;
foreach (DiagramPersistLayoutPart dplp in ExistingPart.DiagramPersistLayoutParts)
{
dplpNew = NewPart.AddNewPart<DiagramPersistLayoutPart>(ExistingPart.GetIdOfPart(dplp));
this.FeedDataDiagramPersistLayoutPart(dplpNew, dplp);
}
}
if (ExistingPart.DiagramStyleParts != null)
{
DiagramStylePart dspNew;
foreach (DiagramStylePart dsp in ExistingPart.DiagramStyleParts)
{
dspNew = NewPart.AddNewPart<DiagramStylePart>(ExistingPart.GetIdOfPart(dsp));
this.FeedDataDiagramStylePart(dspNew, dsp);
}
}
if (ExistingPart.EmbeddedControlPersistenceBinaryDataParts != null)
{
EmbeddedControlPersistenceBinaryDataPart binNew;
foreach (EmbeddedControlPersistenceBinaryDataPart bin in ExistingPart.EmbeddedControlPersistenceBinaryDataParts)
{
binNew = NewPart.AddEmbeddedControlPersistenceBinaryDataPart(bin.ContentType, ExistingPart.GetIdOfPart(bin));
this.FeedDataEmbeddedControlPersistenceBinaryDataPart(binNew, bin);
}
}
if (ExistingPart.EmbeddedControlPersistenceParts != null)
{
EmbeddedControlPersistencePart ecppNew;
foreach (EmbeddedControlPersistencePart ecpp in ExistingPart.EmbeddedControlPersistenceParts)
{
ecppNew = NewPart.AddEmbeddedControlPersistencePart(ecpp.ContentType, ExistingPart.GetIdOfPart(ecpp));
this.FeedDataEmbeddedControlPersistencePart(ecppNew, ecpp);
}
}
if (ExistingPart.EmbeddedObjectParts != null)
{
EmbeddedObjectPart eopNew;
foreach (EmbeddedObjectPart eop in ExistingPart.EmbeddedObjectParts)
{
eopNew = NewPart.AddEmbeddedObjectPart(ExistingPart.GetIdOfPart(eop));
this.FeedDataEmbeddedObjectPart(eopNew, eop);