TableCollection class
TableCollection class
Provides typed access to a collection of Table nodes. To learn more, visit the Working with Tables documentation article.
Inheritance: TableCollection → NodeCollection
Properties
Name | Description |
---|---|
count | Gets the number of nodes in the collection. (Inherited from NodeCollection) |
this[] | (Inherited from NodeCollection) |
Methods
Name | Description |
---|---|
add(node) | Adds a node to the end of the collection. (Inherited from NodeCollection) |
clear() | Removes all nodes from this collection and from the document. (Inherited from NodeCollection) |
contains(node) | Determines whether a node is in the collection. (Inherited from NodeCollection) |
indexOf(node) | Returns the zero-based index of the specified node. (Inherited from NodeCollection) |
insert(index, node) | Inserts a node into the collection at the specified index. (Inherited from NodeCollection) |
remove(node) | Removes the node from the collection and from the document. (Inherited from NodeCollection) |
removeAt(index) | Removes the node at the specified index from the collection and from the document. (Inherited from NodeCollection) |
toArray() | Copies all tables from the collection to a new array of tables. |
Examples
Shows how to remove the first and last rows of all tables in a document.
let doc = new aw.Document(base.myDir + "Tables.docx");
let tables = doc.firstSection.body.tables.toArray();
expect(tables[0].rows.count).toEqual(5);
expect(tables[1].rows.count).toEqual(4);
for (var table of tables)
{
table.firstRow?.remove();
table.lastRow?.remove();
}
expect(tables[0].rows.count).toEqual(3);
expect(tables[1].rows.count).toEqual(2);
Shows how to find out if a tables are nested.
test('CalculateDepthOfNestedTables', () => {
let doc = new aw.Document(base.myDir + "Nested tables.docx");
let tableNodes = doc.getChildNodes(aw.NodeType.Table, true);
expect(tableNodes.count).toEqual(5);
for (let i = 0; i < tableNodes.count; i++)
{
let table = tableNodes.at(i).asTable();
// Find out if any cells in the table have other tables as children.
let count = getChildTableCount(table);
console.log("Table #{0} has {1} tables directly within its cells", i, count);
// Find out if the table is nested inside another table, and, if so, at what depth.
let tableDepth = getNestedDepthOfTable(table);
if (tableDepth > 0)
console.log("Table #{0} is nested inside another table at depth of {1}", i,
tableDepth);
else
console.log("Table #{0} is a non nested table (is not a child of another table)", i);
}
});
/// <summary>
/// Calculates what level a table is nested inside other tables.
/// </summary>
/// <returns>
/// An integer indicating the nesting depth of the table (number of parent table nodes).
/// </returns>
function getNestedDepthOfTable(table) {
let depth = 0;
let parent = table.getAncestor(aw.NodeType.Table);
while (parent != null)
{
depth++;
parent = parent.getAncestor(aw.NodeType.Table);
}
return depth;
}
/// <summary>
/// Determines if a table contains any immediate child table within its cells.
/// Do not recursively traverse through those tables to check for further tables.
/// </summary>
/// <returns>
/// Returns true if at least one child cell contains a table.
/// Returns false if no cells in the table contain a table.
/// </returns>
function getChildTableCount(table) {
let childTableCount = 0;
for (let row of table.rows.toArray())
{
for (let cell of row.cells.toArray())
{
if (cell.tables.count > 0)
childTableCount++;
}
}
return childTableCount;
}
See Also
- module Aspose.Words.Tables
- class NodeCollection