Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/DocumentFormat.OpenXml/Schema/Wordprocessing/Table.cs
Original file line number Diff line number Diff line change
@@ -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 System.Collections.Generic;

namespace DocumentFormat.OpenXml.Wordprocessing
{
public partial class Table
{
/// <summary>
/// Gets or sets the TableProperties.
/// </summary>
public TableProperties? TableProperties
{
get => GetElement(Wordprocessing.TableProperties.ElementType) as TableProperties;
set => SetElement(value, Wordprocessing.TableProperties.ElementType);
}

/// <summary>
/// Gets or sets the TableGrid.
/// </summary>
public TableGrid? TableGrid
{
get => GetElement(Wordprocessing.TableGrid.ElementType) as TableGrid;
set => SetElement(value, Wordprocessing.TableGrid.ElementType);
}

/// <summary>
/// Gets the collection of TableRow elements.
/// </summary>
public IEnumerable<TableRow>? TableRow
{
get
{
foreach (var element in Elements<TableRow>())
{
yield return element;
}
}
}
}
}
27 changes: 27 additions & 0 deletions src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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;

namespace DocumentFormat.OpenXml.Wordprocessing
{
/// <summary>
/// Represents a table row in a Wordprocessing document.
/// </summary>
public partial class TableRow
{
/// <summary>
/// Gets the collection of table cells within the table row.
/// </summary>
public IEnumerable<TableCell>? TableCell
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is an IEnumerable, I think plural TableCells would be a better name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And do we currently return nullable enumerables? I'd prefer to return an empty enumerable, but we should follow whatever the pattern is

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twsouthwick I applied changes you and Mike recommended can I have this PR re reviewed/approved Thanks

{
get
{
foreach (var element in Elements<TableCell>())
{
yield return element;
}
}
}
}
}
32 changes: 32 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Linq;
using Xunit;

namespace DocumentFormat.OpenXml.Wordprocessing
{
public class TableRowTest
{
[Fact]
public void TableCellTest()
{
// 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 to test"))));

// Assert
foreach (var cell in tr.TableCell.ToList())
{
Assert.IsType<TableCell>(cell);
Assert.Equal(tc1, cell);
Assert.Equal("Text to test", cell.InnerText);
}
}
}
}
100 changes: 100 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
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>(BorderValues.Single),
Size = 24,
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(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());

// Act
table.AppendChild(tableGrid);

// Assert
Assert.NotNull(table.TableGrid);
Assert.Equal(tableGrid, table.TableGrid);
}

[Fact]
public void TableRowTest()
{
// 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 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 rows 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<TableRow> rows = [tr, tr2];

// Assert
Assert.NotNull(table.TableRow);
TableRow tableRow = new TableRow();
Assert.Equal(rows.Count, table.TableRow.Count());
int i = 0;
foreach (var row in table.TableRow.ToList())
{
Console.WriteLine(row.InnerText);
Assert.IsType<TableRow>(row);
Assert.Equal(rows[i], row);
i++;
}
}
}
}