Aspose::Words::Tables::TableCollection class

TableCollection class

Provides typed access to a collection of Table nodes. To learn more, visit the Working with Tables documentation article.

class TableCollection : public Aspose::Words::NodeCollection

Methods

MethodDescription
Add(const System::SharedPtr<Aspose::Words::Node>&)Adds a node to the end of the collection.
Clear()Removes all nodes from this collection and from the document.
Contains(const System::SharedPtr<Aspose::Words::Node>&)Determines whether a node is in the collection.
get_Count()Gets the number of nodes in the collection.
GetEnumerator() overrideProvides a simple “foreach” style iteration over the collection of nodes.
GetType() const override
idx_get(int32_t)Retrieves a Table at the given index.
IndexOf(const System::SharedPtr<Aspose::Words::Node>&)Returns the zero-based index of the specified node.
Insert(int32_t, const System::SharedPtr<Aspose::Words::Node>&)Inserts a node into the collection at the specified index.
Is(const System::TypeInfo&) const override
Remove(const System::SharedPtr<Aspose::Words::Node>&)Removes the node from the collection and from the document.
RemoveAt(int32_t)Removes the node at the specified index from the collection and from the document.
ToArray()Copies all tables from the collection to a new array of tables.
static Type()

Examples

Shows how to remove the first and last rows of all tables in a document.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");

SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();

ASSERT_EQ(5, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(4, tables->idx_get(1)->get_Rows()->get_Count());

for (const auto& table : System::IterateOver(tables->LINQ_OfType<SharedPtr<Table>>()))
{
    if (table->get_FirstRow() != nullptr)
    {
        table->get_FirstRow()->Remove();
    }
    if (table->get_LastRow() != nullptr)
    {
        table->get_LastRow()->Remove();
    }
}

ASSERT_EQ(3, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(2, tables->idx_get(1)->get_Rows()->get_Count());

Shows how to find out if a tables are nested.

void CalculateDepthOfNestedTables()
{
    auto doc = MakeObject<Document>(MyDir + u"Nested tables.docx");
    SharedPtr<NodeCollection> tables = doc->GetChildNodes(NodeType::Table, true);

    for (int i = 0; i < tables->get_Count(); i++)
    {
        auto table = System::ExplicitCast<Table>(tables->idx_get(i));

        // Find out if any cells in the table have other tables as children.
        int count = GetChildTableCount(table);
        std::cout << "Table #" << i << " has " << count << " tables directly within its cells" << std::endl;

        // Find out if the table is nested inside another table, and, if so, at what depth.
        int tableDepth = GetNestedDepthOfTable(table);

        if (tableDepth > 0)
        {
            std::cout << "Table #" << i << " is nested inside another table at depth of " << tableDepth << std::endl;
        }
        else
        {
            std::cout << "Table #" << i << " is a non nested table (is not a child of another table)" << std::endl;
        }
    }
}

static int GetNestedDepthOfTable(SharedPtr<Table> table)
{
    int depth = 0;
    SharedPtr<Node> parent = table->GetAncestor(table->get_NodeType());

    while (parent != nullptr)
    {
        depth++;
        parent = parent->GetAncestorOf<SharedPtr<Table>>();
    }

    return depth;
}

static int GetChildTableCount(SharedPtr<Table> table)
{
    int childTableCount = 0;

    for (const auto& row : System::IterateOver(table->get_Rows()->LINQ_OfType<SharedPtr<Row>>()))
    {
        for (const auto& Cell : System::IterateOver(row->get_Cells()->LINQ_OfType<SharedPtr<Cell>>()))
        {
            SharedPtr<TableCollection> childTables = Cell->get_Tables();

            if (childTables->get_Count() > 0)
            {
                childTableCount++;
            }
        }
    }

    return childTableCount;
}

See Also