-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathExcelDrawing.cs
More file actions
2482 lines (2347 loc) · 101 KB
/
Copy pathExcelDrawing.cs
File metadata and controls
2482 lines (2347 loc) · 101 KB
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
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using OfficeOpenXml.Core.Worksheet;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Drawing.Controls;
using OfficeOpenXml.Drawing.OleObject;
using OfficeOpenXml.Drawing.Slicer;
using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.Packaging;
using OfficeOpenXml.Utils;
using OfficeOpenXml.Utils.Drawings;
using OfficeOpenXml.Utils.EnumUtils;
using OfficeOpenXml.Utils.FileUtils;
using OfficeOpenXml.Utils.XML;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
namespace OfficeOpenXml.Drawing
{
/// <summary>
/// Base class for drawings.
/// Drawings are Charts, Shapes and Pictures.
/// </summary>
public class ExcelDrawing : XmlHelper, IDisposable
{
internal ExcelDrawings _drawings;
internal ExcelGroupShape _parent;
internal string _topPath, _nvPrPath, _hyperLinkPath;
internal string _topPathUngrouped, _nvPrPathUngrouped;
internal int _id;
internal const float STANDARD_DPI = 96;
/// <summary>
/// The ratio between EMU and Pixels
/// </summary>
public const int EMU_PER_PIXEL = 9525;
/// <summary>
/// The ratio between EMU and Points
/// </summary>
public const int EMU_PER_POINT = 12700;
/// <summary>
/// The ratio between EMU and centimeters
/// </summary>
public const int EMU_PER_CM = 360000;
/// <summary>
/// The ratio between EMU and milimeters
/// </summary>
public const int EMU_PER_MM = 3600000;
/// <summary>
/// The ratio between EMU and US Inches
/// </summary>
public const int EMU_PER_US_INCH = 914400;
/// <summary>
/// The ratio between EMU and pica
/// </summary>
public const int EMU_PER_PICA = EMU_PER_US_INCH / 6;
internal double _width = double.MinValue, _height = double.MinValue, _top = double.MinValue, _left = double.MinValue;
internal static readonly string[] _schemaNodeOrderSpPr = new string[] { "xfrm", "custGeom", "prstGeom", "noFill", "solidFill", "gradFill", "pattFill", "grpFill", "blipFill", "ln", "effectLst", "effectDag", "scene3d", "sp3d" };
internal bool _doNotAdjust = false;
internal static string[] NamespacePrefixes = { "xdr", "cdr" };
internal readonly int _prefixIndex = 0;
internal readonly DrawingsCollectionType _collectionType;
internal ExcelDrawingCoordinate _frmXPosition;
internal ExcelDrawingSize _frmXSize;
internal ExcelDrawing(ExcelDrawings drawings, XmlNode node, string topPath, string nvPrPath, ExcelGroupShape parent = null, DrawingsCollectionType collectionType = DrawingsCollectionType.Worksheet) :
base(drawings.NameSpaceManager, node)
{
_drawings = drawings;
_parent = parent;
_prefixIndex = (int)collectionType;
_collectionType = collectionType;
TopNode = node;
AddSchemaNodeOrder(new string[] { "from", "pos", "to", "ext", "pic", "graphicFrame", "sp", "cxnSp ", "grpSp", "nvSpPr", "nvCxnSpPr", "nvGraphicFramePr", "spPr", "style", "AlternateContent", "clientData" }, _schemaNodeOrderSpPr);
_topPathUngrouped = topPath;
_nvPrPathUngrouped = nvPrPath;
if (_parent == null)
{
AdjustXPathsForGrouping(false);
CellAnchor = GetAnchorFromName(node.LocalName);
SetPositionProperties(drawings, node);
GetPositionSize(); //Get the drawing position and size, so we can adjust it upon save, if the normal font is changed
string relID = GetXmlNodeString(_hyperLinkPath + "/@r:id");
if (!string.IsNullOrEmpty(relID))
{
HypRel = drawings.Part.GetRelationship(relID);
if (HypRel.TargetUri == null)
{
if (!string.IsNullOrEmpty(HypRel.Target))
{
_hyperLink = new ExcelHyperLink(HypRel.Target.Substring(1), "");
}
}
else
{
if (HypRel.TargetUri.IsAbsoluteUri)
{
_hyperLink = new ExcelHyperLink(HypRel.TargetUri.AbsoluteUri);
}
else
{
_hyperLink = new ExcelHyperLink(HypRel.TargetUri.OriginalString, UriKind.Relative);
}
}
if (Hyperlink is ExcelHyperLink ehl)
{
ehl.ToolTip = GetXmlNodeString(_hyperLinkPath + "/@tooltip");
}
}
}
else
{
AdjustXPathsForGrouping(true);
SetPositionProperties(drawings, node);
GetPositionSize(); //Get the drawing position and size, so we can adjust it upon save, if the normal font is changed
}
if (DrawingType == eDrawingType.Control || DrawingType == eDrawingType.OleObject || drawings._nextDrawingId >= 1025)
{
_id = drawings.Worksheet._nextControlId++;
}
}
internal virtual void AdjustXPathsForGrouping(bool group)
{
if (group)
{
_topPath = _topPathUngrouped.IndexOf('/') > 0 ? _topPathUngrouped.Substring(_topPathUngrouped.IndexOf('/') + 1) : "";
if (_topPath == "")
{
_nvPrPath = _nvPrPathUngrouped;
}
else
{
_nvPrPath = _topPath + "/" + _nvPrPathUngrouped;
}
}
else
{
_topPath = _topPathUngrouped;
_nvPrPath = _topPath + "/" + _nvPrPathUngrouped;
}
_hyperLinkPath = $"{_nvPrPath}/a:hlinkClick";
}
internal void SetGroupChild(XmlNode offNode, XmlNode extNode)
{
CellAnchor = eEditAs.Absolute;
From = null;
To = null;
Position = new ExcelDrawingCoordinate(NameSpaceManager, offNode, GetPositionSize);
Size = new ExcelDrawingSize(NameSpaceManager, extNode, GetPositionSize);
}
internal void RemoveFromToNodes()
{
CellAnchor = eEditAs.Absolute;
From = null;
To = null;
}
private void SetPositionProperties(ExcelDrawings drawings, XmlNode node)
{
if (_parent == null) //Top level drawing
{
SetPositionPropertiesTopDrawing(drawings, node);
}
else //Child to Group shape
{
From = null;
To = null;
XmlNode posNode = GetXFrameNode(node, "a:off");
if (posNode != null)
{
Position = new ExcelDrawingCoordinate(drawings.NameSpaceManager, posNode, GetPositionSize);
}
posNode = GetXFrameNode(node, "a:ext");
if (posNode != null)
{
Size = new ExcelDrawingSize(drawings.NameSpaceManager, posNode, GetPositionSize);
}
}
}
private void SetPositionPropertiesTopDrawing(ExcelDrawings drawings, XmlNode node)
{
XmlNode posNode = node.SelectSingleNode(NamespacePrefixes[_prefixIndex] + ":from", drawings.NameSpaceManager);
if (posNode != null)
{
From = new ExcelPosition(drawings.NameSpaceManager, posNode, GetPositionSize, _prefixIndex);
Position = null;
}
else
{
posNode = node.SelectSingleNode("xdr:pos", drawings.NameSpaceManager);
if (posNode != null)
{
Position = new ExcelDrawingCoordinate(drawings.NameSpaceManager, posNode, GetPositionSize);
}
}
posNode = node.SelectSingleNode(NamespacePrefixes[_prefixIndex] + ":to", drawings.NameSpaceManager);
if (posNode != null)
{
To = new ExcelPosition(drawings.NameSpaceManager, posNode, GetPositionSize, _prefixIndex);
Size = null;
}
else
{
To = null;
posNode = node.SelectSingleNode(NamespacePrefixes[_prefixIndex] + ":ext", drawings.NameSpaceManager);
if (posNode != null)
{
Size = new ExcelDrawingSize(drawings.NameSpaceManager, posNode, GetPositionSize, _prefixIndex);
}
}
}
private XmlNode GetXFrameNode(XmlNode node, string child)
{
if (node.LocalName == "AlternateContent")
{
node = node.GetChildAtPosition(0).GetChildAtPosition(0);
}
if (node.LocalName == "grpSp")
{
return node.SelectSingleNode($"{NamespacePrefixes[_prefixIndex]}:grpSpPr/a:xfrm/{child}", NameSpaceManager);
}
else if (node.LocalName == "graphicFrame")
{
return node.SelectSingleNode($"{NamespacePrefixes[_prefixIndex]}:xfrm/{child}", NameSpaceManager);
}
else
{
return node.SelectSingleNode($"{NamespacePrefixes[_prefixIndex]}:spPr/a:xfrm/{child}", NameSpaceManager);
}
}
internal bool IsWithinColumnRange(int colFrom, int colTo)
{
if (CellAnchor == eEditAs.OneCell)
{
GetToColumnFromPixels(_width, out int col, out _);
return ((From.Column > colFrom - 1 || (From.Column == colFrom - 1 && From.ColumnOff == 0)) && (col <= colTo));
}
else if (CellAnchor == eEditAs.TwoCell)
{
return ((From.Column > colFrom - 1 || (From.Column == colFrom - 1 && From.ColumnOff == 0)) && (To.Column <= colTo));
}
else
{
return false;
}
}
internal bool IsWithinRowRange(int rowFrom, int rowTo)
{
if (CellAnchor == eEditAs.OneCell)
{
GetToRowFromPixels(_height, out int row, out int pixOff);
return ((From.Row > rowFrom - 1 || (From.Row == rowFrom - 1 && From.RowOff == 0)) && (row <= rowTo));
}
else if (CellAnchor == eEditAs.TwoCell)
{
return ((From.Row > rowFrom - 1 || (From.Row == rowFrom - 1 && From.RowOff == 0)) && (To.Row <= rowTo));
}
else
{
return false;
}
}
internal static eEditAs GetAnchorFromName(string topElementName)
{
switch (topElementName)
{
case "oneCellAnchor":
return eEditAs.OneCell;
case "absSizeAnchor": //For drawings inside a chart
case "absoluteAnchor":
return eEditAs.Absolute;
case "relSizeAnchor": //For drawings inside a chart
case "twoCellAnchor":
default:
return eEditAs.TwoCell;
}
}
/// <summary>
/// The type of drawing
/// </summary>
public virtual eDrawingType DrawingType
{
get
{
return eDrawingType.Drawing;
}
}
/// <summary>
/// The name of the drawing object
/// </summary>
public virtual string Name
{
get
{
try
{
if (_nvPrPath == "") return "";
return GetXmlNodeString(_nvPrPath + "/@name");
}
catch
{
return "";
}
}
set
{
try
{
if (_nvPrPath == "") throw new NotImplementedException();
SetXmlNodeString(_nvPrPath + "/@name", value);
if (this is ExcelSlicer<ExcelTableSlicerCache> ts)
{
SetXmlNodeString(_nvPrPath + "/../../a:graphic/a:graphicData/sle:slicer/@name", value);
ts.SlicerName = value;
}
else if (this is ExcelSlicer<ExcelPivotTableSlicerCache> pts)
{
SetXmlNodeString(_nvPrPath + "/../../a:graphic/a:graphicData/sle:slicer/@name", value);
pts.SlicerName = value;
}
}
catch
{
throw new NotImplementedException();
}
}
}
/// <summary>
/// A description of the drawing object
/// </summary>
public string Description
{
get
{
try
{
if (_nvPrPath == "") return "";
return GetXmlNodeString(_nvPrPath + "/@descr");
}
catch
{
return "";
}
}
set
{
try
{
if (_nvPrPath == "") throw new NotImplementedException();
SetXmlNodeString(_nvPrPath + "/@descr", value);
}
catch
{
throw new NotImplementedException();
}
}
}
/// <summary>
/// How Excel resize drawings when the column width is changed within Excel.
/// </summary>
public eEditAs EditAs
{
get
{
try
{
if (_parent != null && DrawingType == eDrawingType.Control)
{
return ((ExcelControl)this).GetCellAnchorFromWorksheetXml();
}
if (_parent != null && DrawingType == eDrawingType.OleObject)
{
return ((ExcelOleObject)this).GetCellAnchorFromWorksheetXml();
}
if (CellAnchor == eEditAs.TwoCell && _collectionType==DrawingsCollectionType.Worksheet)
{
string s = GetXmlNodeString("@editAs");
if (s == "")
{
return eEditAs.TwoCell;
}
else
{
return (eEditAs)Enum.Parse(typeof(eEditAs), s, true);
}
}
else
{
return CellAnchor;
}
}
catch
{
return eEditAs.TwoCell;
}
}
set
{
if (_parent != null)
{
if (DrawingType == eDrawingType.Control)
{
((ExcelControl)this).SetCellAnchor(value);
}
else if (DrawingType == eDrawingType.OleObject)
{
((ExcelOleObject)this).SetCellAnchor(value);
}
else
{
throw (new InvalidOperationException("EditAs can't be set when a drawing is a part of a group."));
}
}
else if (CellAnchor == eEditAs.TwoCell && _collectionType==DrawingsCollectionType.Worksheet)
{
string s = value.ToString();
SetXmlNodeString("@editAs", s.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + s.Substring(1, s.Length - 1));
}
else if (CellAnchor != value)
{
throw (new InvalidOperationException("EditAs can only be set when CellAnchor is set to TwoCellAnchor and the drawing does not have a parent drawing."));
}
}
}
const string lockedPath = "xdr:clientData/@fLocksWithSheet";
/// <summary>
/// Lock drawing
/// </summary>
public virtual bool Locked
{
get
{
return GetXmlNodeBool(lockedPath, true);
}
set
{
SetXmlNodeBool(lockedPath, value);
}
}
const string printPath = "xdr:clientData/@fPrintsWithSheet";
/// <summary>
/// Print drawing with sheet
/// </summary>
public virtual bool Print
{
get
{
return GetXmlNodeBool(printPath, true);
}
set
{
SetXmlNodeBool(printPath, value);
}
}
/// <summary>
/// Top Left position, if the shape is of the one- or two- cell anchor type
/// Otherwise this property is set to null
/// </summary>
public ExcelPosition From
{
get;
private set;
}
/// <summary>
/// Top Left position, if the shape is of the absolute anchor type
/// </summary>
public ExcelDrawingCoordinate Position
{
get;
internal set;
}
/// <summary>
/// The extent of the shape, if the shape is of the one- or absolute- anchor type.
/// Otherwise this property is set to null
/// </summary>
public ExcelDrawingSize Size
{
get;
internal set;
}
/// <summary>
/// Bottom right position
/// </summary>
public ExcelPosition To { get; private set; } = null;
Uri _hyperLink = null;
/// <summary>
/// Hyperlink
/// </summary>
public Uri Hyperlink
{
get
{
return _hyperLink;
}
set
{
if (_hyperLink != null)
{
DeleteNode(_hyperLinkPath);
if (HypRel != null)
{
_drawings.Part.DeleteRelationship(HypRel.Id);
}
}
if (value != null)
{
if (value is ExcelHyperLink el && !string.IsNullOrEmpty(el.ReferenceAddress))
{
HypRel = _drawings.Part.CreateRelationship("#" + new ExcelAddress(el.ReferenceAddress).FullAddress, Packaging.TargetMode.Internal, ExcelPackage.schemaHyperlink);
}
else
{
HypRel = _drawings.Part.CreateRelationship(value, Packaging.TargetMode.External, ExcelPackage.schemaHyperlink);
}
SetXmlNodeString(_hyperLinkPath + "/@r:id", HypRel.Id);
if (value is ExcelHyperLink excelLink)
{
SetXmlNodeString(_hyperLinkPath + "/@tooltip", excelLink.ToolTip);
}
}
_hyperLink = value;
}
}
ExcelDrawingAsType _as = null;
/// <summary>
/// Provides access to type conversion for all top-level drawing classes.
/// </summary>
public ExcelDrawingAsType As
{
get
{
if (_as == null)
{
_as = new ExcelDrawingAsType(this);
}
return _as;
}
}
internal Packaging.ZipPackageRelationship HypRel { get; set; }
/// <summary>
/// Add new Drawing types here
/// </summary>
/// <param name="drawings">The drawing collection</param>
/// <param name="node">Xml top node</param>
/// <param name="DrawingsType">The type of collection the drawing belongs to.</param>
/// <returns>The Drawing object</returns>
internal static ExcelDrawing GetDrawing(ExcelDrawings drawings, XmlNode node, DrawingsCollectionType DrawingsType = DrawingsCollectionType.Worksheet)
{
if (node.ChildNodes.Count < 3) return null; //Invalid formatted anchor node, ignore
XmlElement drawNode = (XmlElement)node.GetChildAtPosition(2);
return GetDrawingFromNode(drawings, node, drawNode, null, DrawingsType);
}
internal static ExcelDrawing GetDrawingFromNode(ExcelDrawings drawings, XmlNode node, XmlElement drawNode, ExcelGroupShape parent = null, DrawingsCollectionType DrawingsType = DrawingsCollectionType.Worksheet)
{
string fallbackDrawingPath = "";
string fallbackNvPrPath = "";
switch (drawNode.LocalName)
{
case "sp":
return GetShapeOrControl(drawings, node, drawNode, parent, DrawingsType);
case "pic":
var aPic = new ExcelPicture(drawings, node, parent, DrawingsType);
return aPic;
case "graphicFrame":
var c = ExcelChart.GetChart(drawings, node, parent);
if (c!=null) //If null, the drawing is not a chart. Might be a smart art, diagram or 3d model. We return a standard drawing to retain the drawing.
{
return c;
}
else
{
//While we do not know the exact type.
//It's a standard drawing with a graphic frame
//We assume the object has its name etc. in the same nodes as a chart
fallbackDrawingPath = "xdr:graphicFrame";
fallbackNvPrPath = "xdr:nvGraphicFramePr/xdr:cNvPr";
}
break;
case "grpSp":
return new ExcelGroupShape(drawings, node, parent, DrawingsType);
case "cxnSp":
return new ExcelConnectionShape(drawings, node, parent, DrawingsType);
case "contentPart":
//Not handled yet, return as standard drawing below
break;
case "AlternateContent":
XmlElement choice = drawNode.FirstChild as XmlElement;
if (choice != null && choice.LocalName == "Choice")
{
var req = choice.GetAttribute("Requires"); //NOTE:Can be space sparated. Might have to implement functinality for this.
var ns = drawNode.GetAttribute($"xmlns:{req}");
if (ns == "")
{
ns = choice.GetAttribute($"xmlns:{req}");
}
switch (ns)
{
case ExcelPackage.schemaChartEx2015_9_8:
case ExcelPackage.schemaChartEx2015_10_21:
case ExcelPackage.schemaChartEx2016_5_10:
return ExcelChart.GetChartEx(drawings, node, parent);
case ExcelPackage.schemaSlicer:
return new ExcelTableSlicer(drawings, node, parent);
case ExcelPackage.schemaDrawings2010:
if (choice.SelectSingleNode("xdr:graphicFrame/a:graphic/a:graphicData/@uri", drawings.NameSpaceManager)?.Value == ExcelPackage.schemaSlicer2010)
{
return new ExcelPivotTableSlicer(drawings, node, parent);
}
else if (choice.ChildNodes.Count > 0)
{
if (choice.FirstChild.LocalName == "sp")
{
return GetShapeOrControl(drawings, node, (XmlElement)choice.FirstChild, parent);
}
else if (choice.FirstChild.LocalName == "grpSp")
{
return new ExcelGroupShape(drawings, choice.FirstChild, parent);
}
}
break;
}
}
break;
}
return new ExcelDrawing(drawings, node, fallbackDrawingPath, fallbackNvPrPath, parent, DrawingsType);
}
private static ExcelDrawing GetShapeOrControl(ExcelDrawings drawings, XmlNode node, XmlElement drawNode, ExcelGroupShape parent, DrawingsCollectionType collectionType = DrawingsCollectionType.Worksheet)
{
var shapeId = GetControlShapeId(drawNode, drawings.NameSpaceManager, collectionType);
var control = drawings.Worksheet.Controls.GetControlByShapeId(shapeId);
var oleObject = control == null ? drawings.Worksheet.OleObjects.GetOleObjectByShapeId(shapeId) : null;
if (control != null)
{
return ControlFactory.GetControl(drawings, drawNode, control, parent);
}
else if (oleObject != null)
{
return OleObjectFactory.GetOleObject(drawings, drawNode, oleObject, parent);
}
else
{
return new ExcelShape(drawings, node, parent, collectionType);
}
}
private static int GetControlShapeId(XmlElement drawNode, XmlNamespaceManager nameSpaceManager, DrawingsCollectionType collectionType = DrawingsCollectionType.Worksheet)
{
var idNode = drawNode.SelectSingleNode(NamespacePrefixes[(int)collectionType] + ":nvSpPr/" + NamespacePrefixes[(int)collectionType] + ":cNvPr/@id", nameSpaceManager);
if (idNode != null)
{
return int.Parse(idNode.Value);
}
return -1;
}
internal int Id
{
get
{
try
{
if (_nvPrPath == "") return -1;
var val = GetXmlNodeInt(_nvPrPath + "/@id");
if (val > _drawings._nextDrawingId)
{
_drawings._nextDrawingId = val;
}
else if (val == int.MinValue)
{
val = _drawings._nextDrawingId;
}
return val;
}
catch
{
return -1;
}
}
set
{
try
{
if (_nvPrPath == "") throw new NotImplementedException();
if (Id > value)
{
_drawings._nextDrawingId = Id;
}
SetXmlNodeInt(_nvPrPath + "/@id", _drawings._nextDrawingId);
if (this is ExcelSlicer<ExcelTableSlicerCache> ts)
{
SetXmlNodeInt(_nvPrPath + "/../../a:graphic/a:graphicData/sle:slicer/@id", value);
//ts._id = value;
}
else if (this is ExcelSlicer<ExcelPivotTableSlicerCache> pts)
{
SetXmlNodeInt(_nvPrPath + "/../../a:graphic/a:graphicData/sle:slicer/@id", value);
//pts._id = value;
}
}
catch
{
throw new NotImplementedException();
}
}
}
#region "Internal sizing functions"
internal void GetFromBounds(out int fromRow, out int fromRowOff, out int fromCol, out int fromColOff)
{
if (CellAnchor == eEditAs.Absolute)
{
GetToRowFromPixels(Position.Y / (double)EMU_PER_PIXEL, out fromRow, out fromRowOff);
GetToColumnFromPixels(Position.X / (double)EMU_PER_PIXEL, out fromCol, out fromColOff);
}
else
{
fromRow = From.Row;
fromRowOff = From.RowOff;
fromCol = From.Column;
fromColOff = From.ColumnOff;
}
}
internal void GetToBounds(out int toRow, out int toRowOff, out int toCol, out int toColOff)
{
if (CellAnchor == eEditAs.Absolute)
{
GetToRowFromPixels((Position.Y + Size.Height) / EMU_PER_PIXEL, out toRow, out toRowOff);
GetToColumnFromPixels((Position.X + Size.Width) / EMU_PER_PIXEL, out toCol, out toColOff);
}
else
{
if (CellAnchor == eEditAs.TwoCell)
{
toRow = To.Row;
toRowOff = To.RowOff;
toCol = To.Column;
toColOff = To.ColumnOff;
}
else
{
GetToRowFromPixels(Size.Height / EMU_PER_PIXEL, out toRow, out toRowOff, From.Row, From.RowOff);
GetToColumnFromPixels(Size.Width / EMU_PER_PIXEL, out toCol, out toColOff, From.Column, From.ColumnOff);
}
}
}
internal int GetPixelLeft()
{
int pix = 0;
if (_collectionType == DrawingsCollectionType.Chart)
{
if (From != null)
{
return (int)(Math.Round(From.X * _drawings._screenWidth));
}
return 0;
}
if (CellAnchor == eEditAs.Absolute)
{
pix = Position.X / EMU_PER_PIXEL;
}
else
{
ExcelWorksheet ws = _drawings.Worksheet;
double mdw = ws.Workbook.MaxFontWidth;
pix = 0;
for (int col = 0; col < From.Column; col++)
{
pix += ws.GetColumnWidthPixels(col, mdw);
}
pix += From.ColumnOff / EMU_PER_PIXEL;
}
return pix;
}
internal int GetPixelTop()
{
int pix = 0;
if (_collectionType == DrawingsCollectionType.Chart)
{
if (From != null)
{
return (int)(Math.Round(From.Y * _drawings._screenHeight));
}
return 0;
}
if (CellAnchor == eEditAs.Absolute)
{
pix = Position.Y / EMU_PER_PIXEL;
}
else
{
if (From != null)
{
var cache = _drawings.Worksheet.RowHeightCache;
for (int row = 0; row < From.Row; row++)
{
lock (cache)
{
if (!cache.ContainsKey(row))
{
cache.Add(row, _drawings.Worksheet.GetRowHeight(row + 1));
}
}
pix += (int)(cache[row] / 0.75);
}
pix += From.RowOff / EMU_PER_PIXEL;
}
}
return pix;
}
internal double GetPixelWidth()
{
double pix = 0;
if (_collectionType == DrawingsCollectionType.Chart)
{
//var ext = (XmlElement)TopNode.SelectSingleNode("(cdr:sp|cdr:pic|cdr:cxnSp)/cdr:spPr/a:xfrm/a:ext", NameSpaceManager);
//if (ext == null)
// ext = (XmlElement)TopNode.SelectSingleNode("cdr:spPr/a:xfrm/a:ext", NameSpaceManager);
//if (ext != null)
//{
// var cx = ((XmlElement)ext).GetAttribute("cx");
// pix = (int)double.Parse(cx);
//}
if(To==null)
{
return Size.Width / EMU_PER_PIXEL;
}
return (To.X - From.X) * _drawings._screenWidth;
//return _frmXSize.Width / EMU_PER_PIXEL;
}
if (CellAnchor == eEditAs.TwoCell)
{
ExcelWorksheet ws = _drawings.Worksheet;
pix = -From.ColumnOff / (double)EMU_PER_PIXEL;
for (int col = From.Column + 1; col <= To.Column; col++)
{
pix += PixelHelper.GetColumnWidth(ws, col);
}
var w = PixelHelper.GetColumnWidth(ws, To.Column + 1);
pix += Math.Min(w, Convert.ToDouble(To.ColumnOff) / EMU_PER_PIXEL);
}
else
{
pix = Size.Width / (double)EMU_PER_PIXEL;
}
return pix;
}
internal double GetPixelHeight()
{
double pix = 0;
if (_collectionType == DrawingsCollectionType.Chart)
{
//var ext = (XmlElement)TopNode.SelectSingleNode("(cdr:sp|cdr:pic|cdr:cxnSp)/cdr:spPr/a:xfrm/a:ext", NameSpaceManager);
//if (ext == null)
// ext = (XmlElement)TopNode.SelectSingleNode("cdr:spPr/a:xfrm/a:ext", NameSpaceManager);
//if (ext != null)
//{
// var cy = ((XmlElement)ext).GetAttribute("cy");
// pix = (int)double.Parse(cy);
//}
if(Size==null)
{
return (To.Y - From.Y) * _drawings._screenHeight;
}
return Size.Height / (double)EMU_PER_PIXEL;
}
if (CellAnchor == eEditAs.TwoCell)
{
ExcelWorksheet ws = _drawings.Worksheet;
pix = -(From.RowOff / (double)EMU_PER_PIXEL);
for (int row = From.Row + 1; row <= To.Row; row++)
{
pix += PixelHelper.GetRowHeight(ws, row);
}
var h = PixelHelper.GetRowHeight(ws, To.Row + 1);
pix += Math.Min(h, Convert.ToDouble(To.RowOff) / EMU_PER_PIXEL);
}
else
{
pix = Size.Height / (double)EMU_PER_PIXEL;
}
return pix;
}
internal void SetPixelTop(double pixels)
{
_doNotAdjust = true;
if (CellAnchor == eEditAs.Absolute)
{
if (_collectionType == DrawingsCollectionType.Worksheet)
{
Position.Y = (int)(pixels * EMU_PER_PIXEL);
}
else
{
From.Y= (double)pixels/_drawings._screenHeight;
}
}
else
{
CalcRowFromPixelTop(pixels, out int row, out int rowOff);
From.Row = row;
From.RowOff = rowOff;
}
_top = pixels;
_doNotAdjust = false;
}
internal void CalcRowFromPixelTop(double pixels, out int row, out int rowOff)
{
ExcelWorksheet ws = _drawings.Worksheet;
double mdw = ws.Workbook.MaxFontWidth;
double prevPix = 0;
double pix = PixelHelper.GetRowHeight(ws, 1);
int r = 2;
while (pix < pixels && r <= ExcelPackage.MaxRows)
{
prevPix = pix;
pix += (int)PixelHelper.GetRowHeight(ws, r++);
}
if (pix == pixels)
{
row = r - 1;
rowOff = 0;
}
else
{
row = r - 2;
rowOff = (int)(pixels - prevPix) * EMU_PER_PIXEL;
}
}
internal void SetPixelLeft(double pixels)
{
_doNotAdjust = true;
if (CellAnchor == eEditAs.Absolute)
{
if (_collectionType == DrawingsCollectionType.Worksheet)
{
Position.X = (int)(pixels * EMU_PER_PIXEL);
}
else
{
From.X = (double)pixels / _drawings._screenWidth;
}
}
else
{
CalcColFromPixelLeft(pixels, out int col, out int colOff);
From.Column = col;
From.ColumnOff = colOff;
}
_doNotAdjust = false;
_left = pixels;
}
internal void CalcColFromPixelLeft(double pixels, out int column, out int columnOff)
{