Bekerja dengan Kolom dan Baris
Untuk kontrol lebih besar atas cara kerja tabel, pelajari cara memanipulasi kolom dan baris.
Temukan Indeks Elemen Tabel
Kolom, baris, dan sel dikelola dengan mengakses simpul dokumen yang dipilih berdasarkan indeksnya. Menemukan indeks dari setiap node melibatkan pengumpulan semua node turunan dari tipe elemen dari node induk, dan kemudian menggunakan metode IndexOf untuk menemukan indeks dari node yang diinginkan dalam koleksi.
Temukan Indeks Tabel dalam Dokumen
Terkadang Anda mungkin perlu membuat perubahan pada tabel tertentu dalam dokumen. Untuk melakukan ini, Anda dapat merujuk ke tabel berdasarkan indeksnya.
Contoh kode berikut menunjukkan cara mengambil indeks tabel dalam dokumen:
// 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); |
Temukan Indeks Baris dalam Tabel
Demikian pula, Anda mungkin perlu membuat perubahan pada baris tertentu dalam tabel yang dipilih. Untuk melakukan ini, Anda juga dapat merujuk ke baris berdasarkan indeksnya.
Contoh kode berikut menunjukkan cara mengambil indeks baris dalam tabel:
// 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()); |
Temukan Indeks Sel dalam Satu Baris
Terakhir, Anda mungkin perlu membuat perubahan pada sel tertentu, dan Anda juga dapat melakukannya dengan indeks sel.
Contoh kode berikut menunjukkan cara mengambil indeks sel dalam satu baris:
// 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)); |
Bekerja dengan Kolom
Dalam Model Objek Dokumen Aspose.Words (DOM), simpul Table terdiri dari Row simpul dan kemudian Cell simpul. Jadi, dalam Model Objek Document
dari Aspose.Words, seperti pada dokumen Word, tidak ada konsep kolom.
Secara desain, baris tabel di Microsoft Word dan Aspose.Words sepenuhnya independen, dan properti serta operasi dasar hanya terdapat di baris dan sel tabel. Ini memberi tabel kemampuan untuk memiliki beberapa atribut yang menarik:
- Setiap baris tabel dapat memiliki jumlah sel yang sama sekali berbeda
- Secara vertikal, sel setiap baris dapat memiliki lebar yang berbeda
- Dimungkinkan untuk menggabungkan tabel dengan format baris dan jumlah sel yang berbeda
Setiap operasi yang dilakukan pada kolom sebenarnya adalah “pintasan” yang melakukan operasi dengan mengubah sel baris secara kolektif sedemikian rupa sehingga terlihat seperti diterapkan ke kolom. Artinya, Anda dapat melakukan operasi pada kolom hanya dengan mengulangi indeks sel baris tabel yang sama.
Contoh kode berikut menyederhanakan operasi tersebut dengan membuktikan kelas facade yang mengumpulkan sel-sel yang membentuk “kolom” dari sebuah tabel:
// 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; | |
} | |
}; |
Contoh kode berikut menunjukkan cara menyisipkan kolom kosong ke dalam tabel:
// 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))); | |
} | |
Contoh kode berikut menunjukkan cara menghapus kolom dari tabel dalam dokumen:
// 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(); |
Tentukan Baris sebagai Baris Header
Anda dapat memilih untuk mengulang baris pertama dalam tabel sebagai Baris Header hanya di halaman pertama atau di setiap halaman jika tabel dibagi menjadi beberapa. Di Aspose.Words, Anda dapat mengulang Baris Header di setiap halaman menggunakan properti HeadingFormat.
Anda juga dapat menandai beberapa baris header jika baris tersebut ditempatkan satu demi satu di awal tabel. Untuk melakukan ini, Anda perlu menerapkan properti HeadingFormat ke baris ini.
Contoh kode berikut menunjukkan cara membuat tabel yang menyertakan Baris Header yang berulang di halaman berikutnya:
// 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"); |
Jaga agar Tabel dan Baris tidak Pecah Di Seluruh Halaman
Ada kalanya konten tabel tidak boleh dibagi menjadi beberapa halaman. Misalnya, jika judul berada di atas tabel, judul dan tabel harus selalu disatukan pada halaman yang sama untuk mempertahankan tampilan yang tepat.
Ada dua teknik terpisah yang berguna untuk mencapai fungsionalitas ini:
Allow row break across pages
, yang diterapkan ke baris tabelKeep with next
, yang diterapkan pada paragraf dalam sel tabel
Secara default, properti di atas dinonaktifkan.
Jaga agar Baris tidak Melintasi Halaman
Ini melibatkan pembatasan konten di dalam sel baris agar tidak dipisah di seluruh halaman. Di Microsoft Word, ini dapat ditemukan di bawah Properti Tabel sebagai opsi “Izinkan baris untuk melintasi halaman”. Dalam Aspose.Words ini ditemukan di bawah objek RowFormat dari Row sebagai properti RowFormat.AllowBreakAcrossPages.
Contoh kode berikut menunjukkan cara menonaktifkan pemutusan baris di seluruh halaman untuk setiap baris dalam tabel:
// 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"); |
Jaga agar Tabel tidak Pecah Di Seluruh Halaman
Untuk menghentikan tabel agar tidak terbelah di seluruh halaman, kita perlu menentukan bahwa kita ingin konten yang terkandung di dalam tabel tetap bersama.
Untuk melakukannya, Aspose.Words menggunakan metode, yang memungkinkan pengguna memilih tabel dan mengaktifkan parameter KeepWithNext menjadi true untuk setiap paragraf di dalam sel tabel. Pengecualian adalah paragraf terakhir dalam tabel, yang harus disetel ke false.
Contoh kode berikut menunjukkan cara mengatur tabel agar tetap bersama di halaman yang sama:
// 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"); |