diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs new file mode 100644 index 000000000..668f81fed --- /dev/null +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using DocumentFormat.OpenXml.Framework; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public partial class Table + { + /// + /// Gets or sets the TableProperties. + /// + public TableProperties? TableProperties + { + get => GetElement(Wordprocessing.TableProperties.ElementType) as TableProperties; + set => SetElement(value, Wordprocessing.TableProperties.ElementType); + } + + /// + /// Gets or sets the TableGrid. + /// + public TableGrid? TableGrid + { + get => GetElement(Wordprocessing.TableGrid.ElementType) as TableGrid; + set => SetElement(value, Wordprocessing.TableGrid.ElementType); + } + + /// + /// Gets the collection of TableRow elements. + /// + public IEnumerable TableRows + { + get + { + return Elements()?.OfType() ?? Enumerable.Empty(); + } + } + } +} diff --git a/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs new file mode 100644 index 000000000..b305c3dbd --- /dev/null +++ b/src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Linq; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + /// + /// Represents a table row in a Wordprocessing document. + /// + public partial class TableRow + { + /// + /// Gets the collection of table cells within the table row. + /// + public IEnumerable TableCells + { + get + { + return Elements()?.OfType() ?? Enumerable.Empty(); + } + } + } +} diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs new file mode 100644 index 000000000..514601a09 --- /dev/null +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public class TableRowTest + { + [Fact] + public void TableCellsTest() + { + // Arrange + Table table = new Table(); + + // Create table rows and cells + TableRow tr = table.AppendChild(new TableRow()); + TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc1.AppendChild(new Paragraph(new Run(new Text("Text 0")))); + + TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc2.AppendChild(new Paragraph(new Run(new Text("Text 1")))); + + int i = 0; + + // List to store row cells + List cells = new List { tc1, tc2 }; + + // Assert + foreach (var cell in tr.TableCells.ToList()) + { + Assert.IsType(cell); + Assert.Equal(cells[i], cell); + Assert.Equal("Text " + i.ToString(), cell.InnerText); + i++; + } + } + + [Fact] + public void TableRowsShouldBeEmptyRowsAreAdded() + { + // Arrange + Table table = new Table(); + TableRow tr = table.AppendChild(new TableRow()); + + // Assert + Assert.NotNull(tr.TableCells); + Assert.Empty(tr.TableCells); + } + } +} diff --git a/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs new file mode 100644 index 000000000..8c781c7d2 --- /dev/null +++ b/test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs @@ -0,0 +1,112 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace DocumentFormat.OpenXml.Wordprocessing +{ + public class TableTests + { + [Fact] + public void TablePropertiesGetterAndSetterTest() + { + // Arrange + var table = new Table(); + var tableProperties = new TableProperties(new TableBorders( + new TopBorder() + { + Val = + new EnumValue(BorderValues.Single), + Size = 24, + }, + new BottomBorder() + { + Val = + new EnumValue(BorderValues.Single), + Size = 24, + })); + + // Act + table.AppendChild(tableProperties); + + // Assert + Assert.NotNull(table.TableProperties); + Assert.Equal(tableProperties, table.TableProperties); + } + + [Fact] + public void TableGridsGetterAndSetterTest() + { + // Arrange + var table = new Table(); + var tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn()); + TableGrid tableGrid1 = new TableGrid(); + + // Act + table.AppendChild(tableGrid); + + // Assert + Assert.NotNull(table.TableGrid); + Assert.Equal(tableGrid, table.TableGrid); + } + + [Fact] + public void TableRowsTest() + { + // Arrange + Table table = new Table(); + + // Create table row and cells + TableRow tr = table.AppendChild(new TableRow()); + TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc1.AppendChild(new Paragraph(new Run(new Text("Text 1")))); + + // Specify the table cell content. + TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc2.AppendChild(new Paragraph(new Run(new Text("Text 2")))); + + // Create table row and cells + TableRow tr2 = table.AppendChild(new TableRow()); + TableCell tc3 = tr2.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc3.AppendChild(new Paragraph(new Run(new Text("Text 3")))); + + // Specify the table cell content. + TableCell tc4 = tr2.AppendChild(new TableCell(new TableCellProperties( + new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }))); + tc4.AppendChild(new Paragraph(new Run(new Text("Text 4")))); + + // List to store table rows + List rows = [tr, tr2]; + table.TableRows.ToList().Append(new TableRow()); + + // Assert + Assert.NotNull(table.TableRows); + Assert.Equal(rows.Count, table.TableRows.Count()); + int i = 0; + foreach (var row in table.TableRows.ToList()) + { + Assert.IsType(row); + Assert.Equal(rows[i], row); + i++; + } + + Assert.Equal(rows.Count, table.TableRows.Count()); + } + + [Fact] + public void TableRowsShouldBeEmptyWhenNoCellsAreAdded() + { + // Arrange + Table table = new Table(); + + // Assert + Assert.NotNull(table.TableRows); + Assert.Empty(table.TableRows); + } + } +}