Skip to content

Commit a891355

Browse files
Provide Table Explicit Properties - #1149 (#1921)
This is to address #1149 --------- Co-authored-by: Michael Bowen <10384982+mikeebowen@users.noreply.github.com>
1 parent 47c920c commit a891355

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using DocumentFormat.OpenXml.Framework;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
9+
namespace DocumentFormat.OpenXml.Wordprocessing
10+
{
11+
public partial class Table
12+
{
13+
/// <summary>
14+
/// Gets or sets the TableProperties.
15+
/// </summary>
16+
public TableProperties? TableProperties
17+
{
18+
get => GetElement(Wordprocessing.TableProperties.ElementType) as TableProperties;
19+
set => SetElement(value, Wordprocessing.TableProperties.ElementType);
20+
}
21+
22+
/// <summary>
23+
/// Gets or sets the TableGrid.
24+
/// </summary>
25+
public TableGrid? TableGrid
26+
{
27+
get => GetElement(Wordprocessing.TableGrid.ElementType) as TableGrid;
28+
set => SetElement(value, Wordprocessing.TableGrid.ElementType);
29+
}
30+
31+
/// <summary>
32+
/// Gets the collection of TableRow elements.
33+
/// </summary>
34+
public IEnumerable<TableRow> TableRows
35+
{
36+
get
37+
{
38+
return Elements<TableRow>()?.OfType<TableRow>() ?? Enumerable.Empty<TableRow>();
39+
}
40+
}
41+
}
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace DocumentFormat.OpenXml.Wordprocessing
8+
{
9+
/// <summary>
10+
/// Represents a table row in a Wordprocessing document.
11+
/// </summary>
12+
public partial class TableRow
13+
{
14+
/// <summary>
15+
/// Gets the collection of table cells within the table row.
16+
/// </summary>
17+
public IEnumerable<TableCell> TableCells
18+
{
19+
get
20+
{
21+
return Elements<TableCell>()?.OfType<TableCell>() ?? Enumerable.Empty<TableCell>();
22+
}
23+
}
24+
}
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace DocumentFormat.OpenXml.Wordprocessing
9+
{
10+
public class TableRowTest
11+
{
12+
[Fact]
13+
public void TableCellsTest()
14+
{
15+
// Arrange
16+
Table table = new Table();
17+
18+
// Create table rows and cells
19+
TableRow tr = table.AppendChild(new TableRow());
20+
TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties(
21+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
22+
tc1.AppendChild(new Paragraph(new Run(new Text("Text 0"))));
23+
24+
TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties(
25+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
26+
tc2.AppendChild(new Paragraph(new Run(new Text("Text 1"))));
27+
28+
int i = 0;
29+
30+
// List to store row cells
31+
List<TableCell> cells = new List<TableCell> { tc1, tc2 };
32+
33+
// Assert
34+
foreach (var cell in tr.TableCells.ToList())
35+
{
36+
Assert.IsType<TableCell>(cell);
37+
Assert.Equal(cells[i], cell);
38+
Assert.Equal("Text " + i.ToString(), cell.InnerText);
39+
i++;
40+
}
41+
}
42+
43+
[Fact]
44+
public void TableRowsShouldBeEmptyRowsAreAdded()
45+
{
46+
// Arrange
47+
Table table = new Table();
48+
TableRow tr = table.AppendChild(new TableRow());
49+
50+
// Assert
51+
Assert.NotNull(tr.TableCells);
52+
Assert.Empty(tr.TableCells);
53+
}
54+
}
55+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace DocumentFormat.OpenXml.Wordprocessing
9+
{
10+
public class TableTests
11+
{
12+
[Fact]
13+
public void TablePropertiesGetterAndSetterTest()
14+
{
15+
// Arrange
16+
var table = new Table();
17+
var tableProperties = new TableProperties(new TableBorders(
18+
new TopBorder()
19+
{
20+
Val =
21+
new EnumValue<BorderValues>(BorderValues.Single),
22+
Size = 24,
23+
},
24+
new BottomBorder()
25+
{
26+
Val =
27+
new EnumValue<BorderValues>(BorderValues.Single),
28+
Size = 24,
29+
}));
30+
31+
// Act
32+
table.AppendChild(tableProperties);
33+
34+
// Assert
35+
Assert.NotNull(table.TableProperties);
36+
Assert.Equal(tableProperties, table.TableProperties);
37+
}
38+
39+
[Fact]
40+
public void TableGridsGetterAndSetterTest()
41+
{
42+
// Arrange
43+
var table = new Table();
44+
var tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
45+
TableGrid tableGrid1 = new TableGrid();
46+
47+
// Act
48+
table.AppendChild(tableGrid);
49+
50+
// Assert
51+
Assert.NotNull(table.TableGrid);
52+
Assert.Equal(tableGrid, table.TableGrid);
53+
}
54+
55+
[Fact]
56+
public void TableRowsTest()
57+
{
58+
// Arrange
59+
Table table = new Table();
60+
61+
// Create table row and cells
62+
TableRow tr = table.AppendChild(new TableRow());
63+
TableCell tc1 = tr.AppendChild(new TableCell(new TableCellProperties(
64+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
65+
tc1.AppendChild(new Paragraph(new Run(new Text("Text 1"))));
66+
67+
// Specify the table cell content.
68+
TableCell tc2 = tr.AppendChild(new TableCell(new TableCellProperties(
69+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
70+
tc2.AppendChild(new Paragraph(new Run(new Text("Text 2"))));
71+
72+
// Create table row and cells
73+
TableRow tr2 = table.AppendChild(new TableRow());
74+
TableCell tc3 = tr2.AppendChild(new TableCell(new TableCellProperties(
75+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
76+
tc3.AppendChild(new Paragraph(new Run(new Text("Text 3"))));
77+
78+
// Specify the table cell content.
79+
TableCell tc4 = tr2.AppendChild(new TableCell(new TableCellProperties(
80+
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })));
81+
tc4.AppendChild(new Paragraph(new Run(new Text("Text 4"))));
82+
83+
// List to store table rows
84+
List<TableRow> rows = [tr, tr2];
85+
table.TableRows.ToList().Append(new TableRow());
86+
87+
// Assert
88+
Assert.NotNull(table.TableRows);
89+
Assert.Equal(rows.Count, table.TableRows.Count());
90+
int i = 0;
91+
foreach (var row in table.TableRows.ToList())
92+
{
93+
Assert.IsType<TableRow>(row);
94+
Assert.Equal(rows[i], row);
95+
i++;
96+
}
97+
98+
Assert.Equal(rows.Count, table.TableRows.Count());
99+
}
100+
101+
[Fact]
102+
public void TableRowsShouldBeEmptyWhenNoCellsAreAdded()
103+
{
104+
// Arrange
105+
Table table = new Table();
106+
107+
// Assert
108+
Assert.NotNull(table.TableRows);
109+
Assert.Empty(table.TableRows);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)