Werk Met Kolomme en Rye

Vir meer beheer oor hoe tabelle werk, leer hoe om kolomme en rye te manipuleer.

Vind Die Tabel Element Indeks

Kolomme, rye en selle word bestuur deur toegang tot die geselekteerde dokument node deur sy indeks. Die vind van die indeks van enige knoop behels die versameling van alle kind nodes van die element tipe van die ouer knoop, en dan die gebruik van die IndexOf metode om die indeks van die gewenste knoop in die versameling te vind.

Vind Die Indeks van’n Tabel in’n Dokument

Soms moet jy dalk veranderinge aan’n spesifieke tabel in’n dokument aanbring. Om dit te doen, kan jy na’n tabel verwys deur sy indeks.

Die volgende kode voorbeeld toon hoe om die indeks van’n tabel in’n dokument te haal:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<NodeCollection> allTables = doc->GetChildNodes(NodeType::Table, true);
int tableIndex = allTables->IndexOf(table);

Vind Die Indeks van’n Ry in’n Tabel

Net so sal jy dalk veranderinge aan’n spesifieke ry in’n geselekteerde tabel moet aanbring. Om dit te doen, kan jy ook na’n ry deur sy indeks verwys.

Die volgende kode voorbeeld toon hoe om die indeks van’n ry in’n tabel te haal:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
int rowIndex = table->IndexOf(table->get_LastRow());

Vind Die Indeks van’n Sel in’n Ry

Ten slotte moet jy dalk veranderinge aan’n spesifieke sel aanbring, en jy kan dit ook deur selindeks doen.

Die volgende kode voorbeeld toon hoe om die indeks van’n sel in’n ry te haal:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
int cellIndex = row->IndexOf(row->get_Cells()->idx_get(4));

Werk Met Kolomme

In die Aspose.Words Dokument Voorwerp Model (DOM), die Table knoop bestaan uit Row nodes en dan Cell nodes. So, in die Document Voorwerp Model van Aspose.Words, soos in Word dokumente, is daar geen konsep van’n kolom.

Deur ontwerp is die tabel rye in Microsoft Word en Aspose.Words heeltemal onafhanklik, en die basiese eienskappe en bedrywighede is slegs in die rye en selle van die tabel vervat. Dit gee tabelle die vermoë om’n paar interessante eienskappe:

  • Elke tabel ry kan’n heeltemal ander aantal selle
  • Vertikaal kan die selle van elke ry verskillende breedtes hê
  • Dit is moontlik om tabelle met verskillende ry formate en aantal selle saam te voeg

Enige operasies wat op kolomme uitgevoer word, is eintlik “kortpaaie” wat die operasie uitvoer deur ry selle kollektief te verander op so’n manier dat dit lyk asof hulle op kolomme toegepas word. Dit is, jy kan bedrywighede op kolomme uit te voer deur eenvoudig iterating oor dieselfde tabel ry sel indeks.

Die volgende kode voorbeeld vereenvoudig sulke bedrywighede deur die bewys van’n fasade klas wat die selle wat’n “kolom” van’n tabel vorm versamel:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
/// <summary>
/// Represents a facade object for a column of a table in a Microsoft Word document.
/// </summary>
class Column : public System::Object
{
public:
/// <summary>
/// Returns the cells which make up the column.
/// </summary>
ArrayPtr<SharedPtr<Cell>> get_Cells()
{
return GetColumnCells()->ToArray();
}
/// <summary>
/// Returns a new column facade from the table and supplied zero-based index.
/// </summary>
static SharedPtr<WorkingWithTables::Column> FromIndex(SharedPtr<Table> table, int columnIndex)
{
return WorkingWithTables::Column::MakeObject(table, columnIndex);
}
/// <summary>
/// Returns the index of the given cell in the column.
/// </summary>
int IndexOf(SharedPtr<Cell> cell)
{
return GetColumnCells()->IndexOf(cell);
}
/// <summary>
/// Inserts a brand new column before this column into the table.
/// </summary>
SharedPtr<WorkingWithTables::Column> InsertColumnBefore()
{
ArrayPtr<SharedPtr<Cell>> columnCells = get_Cells();
if (columnCells->get_Length() == 0)
{
throw System::ArgumentException(u"Column must not be empty");
}
// Create a clone of this column.
for (SharedPtr<Cell> cell : columnCells)
{
cell->get_ParentRow()->InsertBefore(cell->Clone(false), cell);
}
// This is the new column.
auto column = WorkingWithTables::Column::MakeObject(columnCells[0]->get_ParentRow()->get_ParentTable(), mColumnIndex);
// We want to make sure that the cells are all valid to work with (have at least one paragraph).
for (SharedPtr<Cell> cell : column->get_Cells())
{
cell->EnsureMinimum();
}
// Increase the index which this column represents since there is now one extra column in front.
mColumnIndex++;
return column;
}
/// <summary>
/// Removes the column from the table.
/// </summary>
void Remove()
{
for (SharedPtr<Cell> cell : get_Cells())
{
cell->Remove();
}
}
/// <summary>
/// Returns the text of the column.
/// </summary>
String ToTxt()
{
auto builder = System::MakeObject<System::Text::StringBuilder>();
for (SharedPtr<Cell> cell : get_Cells())
{
builder->Append(cell->ToString(SaveFormat::Text));
}
return builder->ToString();
}
private:
int mColumnIndex;
SharedPtr<Table> mTable;
Column(SharedPtr<Table> table, int columnIndex) : mColumnIndex(0)
{
if (table == nullptr)
{
throw System::ArgumentException(u"table");
}
mTable = table;
mColumnIndex = columnIndex;
}
MEMBER_FUNCTION_MAKE_OBJECT(Column, CODEPORTING_ARGS(SharedPtr<Table> table, int columnIndex), CODEPORTING_ARGS(table, columnIndex));
/// <summary>
/// Provides an up-to-date collection of cells which make up the column represented by this facade.
/// </summary>
SharedPtr<System::Collections::Generic::List<SharedPtr<Cell>>> GetColumnCells()
{
SharedPtr<System::Collections::Generic::List<SharedPtr<Cell>>> columnCells =
System::MakeObject<System::Collections::Generic::List<SharedPtr<Cell>>>();
for (const auto& row : System::IterateOver<Row>(mTable->get_Rows()))
{
SharedPtr<Cell> cell = row->get_Cells()->idx_get(mColumnIndex);
if (cell != nullptr)
{
columnCells->Add(cell);
}
}
return columnCells;
}
};
view raw column-class.h hosted with ❤ by GitHub

Die volgende kode voorbeeld toon hoe om’n leë kolom in’n tabel in te voeg:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<WorkingWithTables::Column> column = WorkingWithTables::Column::FromIndex(table, 0);
// Print the plain text of the column to the screen.
std::cout << column->ToTxt() << std::endl;
// Create a new column to the left of this column.
// This is the same as using the "Insert Column Before" command in Microsoft Word.
SharedPtr<WorkingWithTables::Column> newColumn = column->InsertColumnBefore();
for (SharedPtr<Cell> cell : newColumn->get_Cells())
{
cell->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, String(u"Column Text ") + newColumn->IndexOf(cell)));
}

Die volgende kode voorbeeld toon hoe om’n kolom van’n tabel in’n dokument te verwyder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 1, true));
SharedPtr<WorkingWithTables::Column> column = WorkingWithTables::Column::FromIndex(table, 2);
column->Remove();
view raw remove-column.h hosted with ❤ by GitHub

Spesifiseer Rye as Kopryne

U kan kies om die eerste ry in die tabel as Die Kopry slegs op die eerste bladsy of op elke bladsy te herhaal as die tabel in verskeie verdeel is. In Aspose.Words kan jy die Kopry Op elke bladsy herhaal deur die HeadingFormat eienskap te gebruik.

U kan ook verskeie kopreëls merk as sulke rye een na die ander aan die begin van die tabel geleë is. Om dit te doen, moet jy die HeadingFormat eienskappe op hierdie rye toepas.

Die volgende kode voorbeeld toon hoe om’n tabel wat Kop Rye wat herhaal op daaropvolgende bladsye sluit bou:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->StartTable();
builder->get_RowFormat()->set_HeadingFormat(true);
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_CellFormat()->set_Width(100);
builder->InsertCell();
builder->Writeln(u"Heading row 1");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Heading row 2");
builder->EndRow();
builder->get_CellFormat()->set_Width(50);
builder->get_ParagraphFormat()->ClearFormatting();
for (int i = 0; i < 50; i++)
{
builder->InsertCell();
builder->get_RowFormat()->set_HeadingFormat(false);
builder->Write(u"Column 1 Text");
builder->InsertCell();
builder->Write(u"Column 2 Text");
builder->EndRow();
}
doc->Save(ArtifactsDir + u"WorkingWithTables.RepeatRowsOnSubsequentPages.docx");

Hou Tabelle en Rye Van Breek Oor Bladsye

Daar is tye wanneer die inhoud van’n tabel nie oor bladsye verdeel moet word nie. Byvoorbeeld, as’n titel bo’n tabel is, moet die titel en tabel altyd saam op dieselfde bladsy gehou word om die regte voorkoms te behou.

Daar is twee afsonderlike tegnieke wat nuttig is om hierdie funksie te bereik:

  • Allow row break across pages, wat toegepas word op tabel rye
  • Keep with next, wat toegepas word op paragrawe in tabel selle

By verstek is bogenoemde eiendomme gedeaktiveer.

Hou’n Ry Van Breek Oor Bladsye

Dit behels die beperking van inhoud binne die selle van’n ry om oor’n bladsy verdeel te word. In Microsoft Word, kan dit gevind word onder Tabel Eienskappe as die opsie “Laat ry om te breek oor bladsye”. In Aspose.Words word dit gevind onder die RowFormat voorwerp van a Row as die eienskap RowFormat.AllowBreakAcrossPages.

Die volgende kode voorbeeld toon hoe om breek rye oor bladsye vir elke ry in’n tabel uit te skakel:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// Disable breaking across pages for all rows in the table.
for (const auto& row : System::IterateOver<Row>(table->get_Rows()))
{
row->get_RowFormat()->set_AllowBreakAcrossPages(false);
}
doc->Save(ArtifactsDir + u"WorkingWithTables.RowFormatDisableBreakAcrossPages.docx");

Hou’n Tabel Van Breek Oor Bladsye

Om te keer dat die tabel oor bladsye verdeel, moet ons spesifiseer dat ons wil hê dat die inhoud wat in die tabel vervat is, saam moet bly.

Om dit te doen, gebruik Aspose.Words ‘n metode, wat gebruikers toelaat om’n tabel te kies en die KeepWithNext parameter in staat te stel om waar te wees vir elke paragraaf binne die tabel selle. Die uitsondering is die laaste paragraaf in die tabel, wat op vals gestel moet word.

Die volgende kode voorbeeld toon hoe om’n tabel te stel om saam te bly op dieselfde bladsy:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Table spanning two pages.docx");
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
// We need to enable KeepWithNext for every paragraph in the table to keep it from breaking across a page,
// except for the last paragraphs in the last row of the table.
for (const auto& cell : System::IterateOver<Cell>(table->GetChildNodes(NodeType::Cell, true)))
{
cell->EnsureMinimum();
for (const auto& para : System::IterateOver<Paragraph>(cell->get_Paragraphs()))
{
if (!(cell->get_ParentRow()->get_IsLastRow() && para->get_IsEndOfCell()))
{
para->get_ParagraphFormat()->set_KeepWithNext(true);
}
}
}
doc->Save(ArtifactsDir + u"WorkingWithTables.KeepTableTogether.docx");