EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Cannot add multiple shapes/drawings to an excel chart sheet

Open jasunsjs opened this issue 1 month ago • 2 comments

EPPlus usage

Commercial use (I have a commercial license)

Environment

Windows

Epplus version

8.3.0

Spreadsheet application

Excel

Description

In Microsoft Excel, you can create a chart and move the chart to a new chart sheet, and you can add MULTIPLE shapes onto the chart sheet:

Image

sandbox.xlsx

However, the current EPPlus explicitly prohibits this. Calling ExcelDrawings.AddShape(String, eShapeStyle) more than once will throw an error:

System.InvalidOperationException : Chart worksheets can't have more than one drawing

Example:

var package = new ExcelPackage();
var chartSheet = package.Workbook.Worksheets.AddChart("Chart", eChartType.XYScatter);
var scatterChart = (ExcelScatterChart)chartSheet.Chart;

// Attempt to add 2 shapes to the chart
var shape1 = scatterChart.Drawings.AddShape("Shape 1", eShapeStyle.Rect);
var shape2 = scatterChart.Drawings.AddShape("Shape 2", eShapeStyle.Rect); // Error: Chart worksheets can't have more than one drawing

The error is thrown explicitly in the method in the source code, in ExcelDrawings.cs:

internal ExcelShape AddShape(string Name, eShapeStyle Style, object container = null)
{
    if (Worksheet is ExcelChartsheet && _drawingsList.Count > 0)
    {
        throw new InvalidOperationException("Chart worksheets can't have more than one drawing");
    }
    ...

Adding multiple shapes to a chart sheet should be allowed. This is allowed in Excel, and it is also allowed in the current EPPlus if you are adding the shapes to a normal work sheet instead of a chart sheet. Removing this error throw should solve the issue.

Pull Request: #2184

Thanks!

jasunsjs avatar Nov 26 '25 19:11 jasunsjs

Good find. I tested this and it appears to work just fine by removing the exceptions. I made a new PR for this since I found a few more places where the exception was thrown.

AdrianEPPlus avatar Nov 27 '25 09:11 AdrianEPPlus

Thanks. Although I would have appreciated if we instead just updated my original PR so I don't lose the credit... I'm pretty sure I enabled edits from maintainers.

jasunsjs avatar Nov 27 '25 14:11 jasunsjs