Skip to content

Provide Table Explicit Properties - #1149 #1921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 DocumentFormat.OpenXml.Framework;
using System;
using System.Collections.Generic;
using System.Linq;

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> TableRows
{
get
{
return Elements<TableRow>()?.OfType<TableRow>() ?? Enumerable.Empty<TableRow>();
}
}
}
}
25 changes: 25 additions & 0 deletions src/DocumentFormat.OpenXml/Schema/Wordprocessing/TableRow.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <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> TableCells
{
get
{
return Elements<TableCell>()?.OfType<TableCell>() ?? Enumerable.Empty<TableCell>();
}
}
}
}
55 changes: 55 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableRowTest.cs
Original file line number Diff line number Diff line change
@@ -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<TableCell> cells = new List<TableCell> { tc1, tc2 };

// Assert
foreach (var cell in tr.TableCells.ToList())
{
Assert.IsType<TableCell>(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);
}
}
}
112 changes: 112 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Wordprocessing/TableTests.cs
Original file line number Diff line number Diff line change
@@ -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>(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());
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<TableRow> 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<TableRow>(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);
}
}
}
Loading