diff --git a/src/EPPlus/ExcelWorksheets.cs b/src/EPPlus/ExcelWorksheets.cs index 789e50cf5..9878e1c68 100644 --- a/src/EPPlus/ExcelWorksheets.cs +++ b/src/EPPlus/ExcelWorksheets.cs @@ -470,15 +470,18 @@ public void Delete(int Index) worksheet.Drawings.ClearDrawings(); } - //Remove all comments - if (!(worksheet is ExcelChartsheet) && worksheet.Comments.Count > 0) + if (!(worksheet is ExcelChartsheet)) { - worksheet.Comments.Clear(); - } + //Remove all comments + if (worksheet.Comments.Count > 0) + { + worksheet.Comments.Clear(); + } - while(worksheet.PivotTables.Count>0) - { - worksheet.PivotTables.Delete(worksheet.PivotTables[0]); + while (worksheet.PivotTables.Count > 0) + { + worksheet.PivotTables.Delete(worksheet.PivotTables[0]); + } } //Delete any parts still with relations to the Worksheet. DeleteRelationsAndParts(worksheet.Part); diff --git a/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs b/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs new file mode 100644 index 000000000..8707aa7b6 --- /dev/null +++ b/src/EPPlusTest/Issues/ChartSheetDeleteTests.cs @@ -0,0 +1,35 @@ +/******************************************************************************* + * Required Notice: Copyright (C) EPPlus Software AB. + * https://epplussoftware.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + *******************************************************************************/ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OfficeOpenXml; +using OfficeOpenXml.Drawing.Chart; + +namespace EPPlusTest.Issues +{ + [TestClass] + public class ChartSheetDeleteTests + { + [TestMethod] + public void DeleteChartSheetShouldNotThrowNotSupportedException() + { + using (var package = new ExcelPackage()) + { + var ws = package.Workbook.Worksheets.Add("Data"); + var chartSheet = package.Workbook.Worksheets.AddChart("ChartSheet", eChartType.ColumnClustered); + + // This call previously threw NotSupportedException because it tried to access chartSheet.PivotTables + package.Workbook.Worksheets.Delete(chartSheet); + + Assert.AreEqual(1, package.Workbook.Worksheets.Count); + } + } + } +}