Is it possible to create a copy/clone of a shape in an Excel worksheet using the EPPlus library?
I know I can get an existing object with
var shapeExisting = ws.Drawings["ShapeName"];
(ws
being the Worksheet object)
and a create new shape with
var shapeNew = ws.Drawings.AddShape("NewName", eShapeStyle.RtTriangle);
However, I'm unable to find a way to clone shapeExisting
.
Seems like there's no built-in functionality, so until I find a better solution, I added the following method to EPPlus\Drawings\ExcelDrawings.cs
public ExcelShape CloneShape(string SourceName, string TargetName)
{
if ( _drawingNames.ContainsKey(TargetName.ToLower()))
{
throw new Exception("Target name already exists in the drawings collection");
}
if (!_drawingNames.ContainsKey(SourceName.ToLower()))
{
throw new Exception("Source shape does not exist in the drawings collection");
}
ExcelShape shape = new ExcelShape(this, this._drawingsXml,
(ExcelShape) this[SourceName]);
shape.Name = TargetName;
_drawings.Add(shape);
_drawingNames.Add(TargetName.ToLower(), _drawings.Count - 1);
return shape;
}
and also this constructor in ExcelShape.cs
:
internal ExcelShape(ExcelDrawings drawings, XmlDocument DrawingsXml, ExcelShape shapeSource) :
base(drawings, shapeSource._topNode.Clone(), "xdr:sp/xdr:nvSpPr/xdr:cNvPr/@name")
{
this.init();
XmlNode colNode = DrawingsXml.SelectSingleNode("//xdr:wsDr", NameSpaceManager);
colNode.AppendChild(this._topNode);
}