diff --git a/ClosedXML/Excel/Tables/IXLTableRange.cs b/ClosedXML/Excel/Tables/IXLTableRange.cs index 3e6f3f9..5296941 100644 --- a/ClosedXML/Excel/Tables/IXLTableRange.cs +++ b/ClosedXML/Excel/Tables/IXLTableRange.cs @@ -1,27 +1,38 @@ +// Keep this file CodeMaid organised and cleaned using System; -using System.Collections.Generic; + namespace ClosedXML.Excel { - public interface IXLTableRange : IXLRange { + IXLTable Table { get; } + IXLTableRow FirstRow(Func predicate = null); + IXLTableRow FirstRowUsed(Boolean includeFormats, Func predicate = null); + IXLTableRow FirstRowUsed(Func predicate = null); + + new IXLTableRows InsertRowsAbove(int numberOfRows); + + new IXLTableRows InsertRowsBelow(int numberOfRows); + IXLTableRow LastRow(Func predicate = null); + IXLTableRow LastRowUsed(Boolean includeFormats, Func predicate = null); + IXLTableRow LastRowUsed(Func predicate = null); new IXLTableRow Row(int row); + IXLTableRows Rows(Func predicate = null); + new IXLTableRows Rows(int firstRow, int lastRow); + new IXLTableRows Rows(string rows); + IXLTableRows RowsUsed(Boolean includeFormats, Func predicate = null); + IXLTableRows RowsUsed(Func predicate = null); - - IXLTable Table { get; } - - new IXLTableRows InsertRowsAbove(int numberOfRows); - new IXLTableRows InsertRowsBelow(int numberOfRows); } -} \ No newline at end of file +} diff --git a/ClosedXML/Excel/Tables/IXLTableRows.cs b/ClosedXML/Excel/Tables/IXLTableRows.cs index 5e10d8a..27eda87 100644 --- a/ClosedXML/Excel/Tables/IXLTableRows.cs +++ b/ClosedXML/Excel/Tables/IXLTableRows.cs @@ -1,3 +1,4 @@ +// Keep this file CodeMaid organised and cleaned using System; using System.Collections.Generic; @@ -5,6 +6,8 @@ { public interface IXLTableRows : IEnumerable { + IXLStyle Style { get; set; } + /// /// Adds a table row to this group. /// @@ -27,14 +30,14 @@ /// if set to true will return all cells with a value or a style different than the default. IXLCells CellsUsed(Boolean includeFormats); - IXLStyle Style { get; set; } - /// /// Clears the contents of these rows. /// /// Specify what you want to clear. IXLTableRows Clear(XLClearOptions clearOptions = XLClearOptions.All); + void Delete(); + void Select(); } } diff --git a/ClosedXML/Excel/Tables/XLTableRow.cs b/ClosedXML/Excel/Tables/XLTableRow.cs index 3acff51..e16206f 100644 --- a/ClosedXML/Excel/Tables/XLTableRow.cs +++ b/ClosedXML/Excel/Tables/XLTableRow.cs @@ -110,7 +110,6 @@ public new void Delete() { Delete(XLShiftDeletedCells.ShiftCellsUp); - _tableRange.Table.ExpandTableRows(-1); } } } diff --git a/ClosedXML/Excel/Tables/XLTableRows.cs b/ClosedXML/Excel/Tables/XLTableRows.cs index f02dffa..9676504 100644 --- a/ClosedXML/Excel/Tables/XLTableRows.cs +++ b/ClosedXML/Excel/Tables/XLTableRows.cs @@ -1,19 +1,20 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; namespace ClosedXML.Excel { - using System.Collections; - internal class XLTableRows : XLStylizedBase, IXLTableRows, IXLStylized { private readonly List _ranges = new List(); - + public XLTableRows(IXLStyle defaultStyle) : base((defaultStyle as XLStyle).Value) { } #region IXLStylized Members + public override IEnumerable Styles { get @@ -63,9 +64,15 @@ return this; } - public void Add(IXLTableRow range) + public void Delete() { - _ranges.Add((XLTableRow)range); + _ranges.OrderByDescending(r => r.RowNumber()).ForEach(r => r.Delete()); + _ranges.Clear(); + } + + public void Add(IXLTableRow tableRow) + { + _ranges.Add((XLTableRow)tableRow); } public IEnumerator GetEnumerator() diff --git a/ClosedXML_Tests/Excel/Tables/TablesTests.cs b/ClosedXML_Tests/Excel/Tables/TablesTests.cs index 193b38f..8cd32a8 100644 --- a/ClosedXML_Tests/Excel/Tables/TablesTests.cs +++ b/ClosedXML_Tests/Excel/Tables/TablesTests.cs @@ -484,7 +484,9 @@ using (var wb = new XLWorkbook()) { var ws = wb.AddWorksheet("Sheet1"); - var table = ws.FirstCell().InsertTable(l); + var table = ws.Cell("B2").InsertTable(l); + + Assert.AreEqual("B2:E4", table.RangeAddress.ToString()); table.Field("SomeFieldNotProperty").Delete(); @@ -495,6 +497,37 @@ Assert.AreEqual("UnOrderedColumn", table.Fields.Last().Name); Assert.AreEqual(2, table.Fields.Last().Index); + + Assert.AreEqual("B2:D4", table.RangeAddress.ToString()); + } + } + + [Test] + public void CanDeleteTableRows() + { + var l = new List() + { + new TestObjectWithAttributes() { Column1 = "a", Column2 = "b", MyField = 4, UnOrderedColumn = 999 }, + new TestObjectWithAttributes() { Column1 = "c", Column2 = "d", MyField = 5, UnOrderedColumn = 777 }, + new TestObjectWithAttributes() { Column1 = "e", Column2 = "f", MyField = 6, UnOrderedColumn = 555 }, + new TestObjectWithAttributes() { Column1 = "g", Column2 = "h", MyField = 7, UnOrderedColumn = 333 } + }; + + using (var wb = new XLWorkbook()) + { + var ws = wb.AddWorksheet("Sheet1"); + var table = ws.Cell("B2").InsertTable(l); + + Assert.AreEqual("B2:E6", table.RangeAddress.ToString()); + + table.DataRange.Rows(3, 4).Delete(); + + Assert.AreEqual(2, table.DataRange.Rows().Count()); + + Assert.AreEqual("b", table.DataRange.FirstCell().Value); + Assert.AreEqual(777, table.DataRange.LastCell().Value); + + Assert.AreEqual("B2:E4", table.RangeAddress.ToString()); } }