search/mag_sel search/close
Aspose::Words::Tables::TableCollection Class Reference

Provides typed access to a collection of Table nodes.

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::DynamicCast<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;
}

#include <Aspose.Words.Cpp/Tables/TableCollection.h>

+ Inheritance diagram for Aspose::Words::Tables::TableCollection:

Public Member Functions

virtual const TypeInfoGetType () const override
 
SharedPtr< Tableidx_get (int32_t index)
 Retrieves a Table at the given index. More...
 
virtual bool Is (const TypeInfo &target) const override
 
ArrayPtr< SharedPtr< Table > > ToArray ()
 Copies all tables from the collection to a new array of tables. More...
 
- Public Member Functions inherited from NodeCollection
void Add (SharedPtr< Node > node)
 Adds a node to the end of the collection. More...
 
void Clear ()
 Removes all nodes from this collection and from the document. More...
 
bool Contains (SharedPtr< Node > node)
 Determines whether a node is in the collection. More...
 
SharedPtr< CompositeNodeget_Container () override
 
int32_t get_Count ()
 Gets the number of nodes in the collection. More...
 
SharedPtr< NodeGetCurrentNode () override
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides a simple "foreach" style iteration over the collection of nodes. More...
 
SharedPtr< NodeGetNextMatchingNode (SharedPtr< Node > curNode) override
 
SharedPtr< Nodeidx_get (int32_t index)
 Retrieves a node at the given index. More...
 
int32_t IndexOf (SharedPtr< Node > node)
 Returns the zero-based index of the specified node. More...
 
void Insert (int32_t index, SharedPtr< Node > node)
 Inserts a node into the collection at the specified index. More...
 
void Remove (SharedPtr< Node > node)
 Removes the node from the collection and from the document. More...
 
void RemoveAt (int32_t index)
 Removes the node at the specified index from the collection and from the document. More...
 
ArrayPtr< SharedPtr< Node > > ToArray ()
 Copies all nodes from the collection to a new array of nodes. More...
 

Static Public Member Functions

static const TypeInfoType ()
 
- Static Public Member Functions inherited from NodeCollection
static const TypeInfoType ()
 

Member Function Documentation

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Tables::TableCollection::GetType ( ) const
overridevirtual

Reimplemented from Aspose::Words::NodeCollection.

◆ idx_get()

System::SharedPtr<Aspose::Words::Tables::Table> Aspose::Words::Tables::TableCollection::idx_get ( int32_t  index)

Retrieves a Table at the given index.

The index is zero-based.

Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.

If index is greater than or equal to the number of items in the list, this returns a null reference.

If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.

Parameters
indexAn index into the collection.
Examples

Shows how to iterate through all tables in the document and print the contents of each cell.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();
ASSERT_EQ(2, tables->ToArray()->get_Length());
for (int i = 0; i < tables->get_Count(); i++)
{
std::cout << "Start of Table " << i << std::endl;
SharedPtr<RowCollection> rows = tables->idx_get(i)->get_Rows();
// We can use the "ToArray" method on a row collection to clone it into an array.
ASPOSE_ASSERT_EQ(rows, rows->ToArray());
ASPOSE_ASSERT_NS(rows, rows->ToArray());
for (int j = 0; j < rows->get_Count(); j++)
{
std::cout << "\tStart of Row " << j << std::endl;
SharedPtr<CellCollection> cells = rows->idx_get(j)->get_Cells();
// We can use the "ToArray" method on a cell collection to clone it into an array.
ASPOSE_ASSERT_EQ(cells, cells->ToArray());
ASPOSE_ASSERT_NS(cells, cells->ToArray());
for (int k = 0; k < cells->get_Count(); k++)
{
String cellText = cells->idx_get(k)->ToString(SaveFormat::Text).Trim();
std::cout << "\t\tContents of Cell:" << k << " = \"" << cellText << "\"" << std::endl;
}
std::cout << "\tEnd of Row " << j << std::endl;
}
std::cout << "End of Table " << i << "\n" << std::endl;
}

◆ Is()

virtual bool Aspose::Words::Tables::TableCollection::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from Aspose::Words::NodeCollection.

◆ ToArray()

System::ArrayPtr<System::SharedPtr<Aspose::Words::Tables::Table> > Aspose::Words::Tables::TableCollection::ToArray ( )

Copies all tables from the collection to a new array of tables.

Returns
An array of tables.
Examples

Shows how to iterate through all tables in the document and print the contents of each cell.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();
ASSERT_EQ(2, tables->ToArray()->get_Length());
for (int i = 0; i < tables->get_Count(); i++)
{
std::cout << "Start of Table " << i << std::endl;
SharedPtr<RowCollection> rows = tables->idx_get(i)->get_Rows();
// We can use the "ToArray" method on a row collection to clone it into an array.
ASPOSE_ASSERT_EQ(rows, rows->ToArray());
ASPOSE_ASSERT_NS(rows, rows->ToArray());
for (int j = 0; j < rows->get_Count(); j++)
{
std::cout << "\tStart of Row " << j << std::endl;
SharedPtr<CellCollection> cells = rows->idx_get(j)->get_Cells();
// We can use the "ToArray" method on a cell collection to clone it into an array.
ASPOSE_ASSERT_EQ(cells, cells->ToArray());
ASPOSE_ASSERT_NS(cells, cells->ToArray());
for (int k = 0; k < cells->get_Count(); k++)
{
String cellText = cells->idx_get(k)->ToString(SaveFormat::Text).Trim();
std::cout << "\t\tContents of Cell:" << k << " = \"" << cellText << "\"" << std::endl;
}
std::cout << "\tEnd of Row " << j << std::endl;
}
std::cout << "End of Table " << i << "\n" << std::endl;
}

◆ Type()

static const System::TypeInfo& Aspose::Words::Tables::TableCollection::Type ( )
static