ทำงานกับคอลัมน์และแถว

สำหรับการควบคุมการทำงานของตารางให้เรียนรู้วิธีจัดการคอลัมน์และแถว.

ค้นหาดัชนีองค์ประกอบของตาราง

คอลัมน์แถวและเซลล์จะถูกจัดการโดยการเข้าถึงโหนดเอกสารที่เลือกโดยดัชนี จากโหนดพาเรนต์แล้วใช้เมธอดIndexOfเพื่อค้นหาดัชนีของโหนดที่ต้องการในคอลเล็กชัน.

ค้นหาดัชนีของตารางในเอกสาร

บางครั้งคุณอาจจำเป็นต้องทำการเปลี่ยนแปลงตารางเฉพาะในเอกสาร มารถดูตารางโดยดัชนีของตน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงดัชนีของตารางในเอกสาร:

// 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);

ค้นหาดัชนีของแถวในตาราง

ในทำนองเดียวกันคุณอาจต้องทำการเปลี่ยนแปลงในแถวที่เฉพาะเจาะจงในตารางที่เลือ รทำเช่นนี้คุณยังสามารถอ้างถึงแถวโดยดัชนีของมัน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงดัชนีของแถวในตาราง:

// 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());

ค้นหาดัชนีของเซลล์ในแถว

ในที่สุดคุณอาจจำเป็นต้องทำการเปลี่ยนแปลงไปยังเซลล์ที่เฉพาะเจาะจงและคุณสามารถทำเช่นนี้โดยดัชนีเซลล์เช่นกัน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงดัชนีของเซลล์ในแถว:

// 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));

ทำงานกับคอลัมน์

ในAspose.Wordsรูปแบบวัตถุเอกสาร(DOM)โหนดTableประกอบด้วยโหนดRowแล้วCellโหนด ดังนั้นในDocumentรูปแบบวัตถุของAspose.Wordsเช่นเดียวกับในเอกสารคำมีแนวคิดของคอลัมน์ไม่มี.

โดยการออกแบบแถวตารางในMicrosoft WordและAspose.Wordsเป็นอิสระอย่างสมบูรณ์และคุณสมบัติพื้นฐานและการดำ นี้จะช่วยให้ตารางความสามารถในการมีคุณลักษณะที่น่าสนใจบางอย่าง:

  • แต่ละแถวของตารางสามารถมีจำนวนที่แตกต่างกันอย่างสิ้นเชิงของเซลล์
  • ในแนวตั้งเซลล์ของแต่ละแถวสามารถมีความกว้างที่แตกต่างกัน
  • มันเป็นไปได้ที่จะเข้าร่วมตารางที่มีรูปแบบแถวที่แตกต่างกันและจำนวนของเซลล์

การดำเนินการใดๆที่ดำเนินการบนคอลัมน์เป็นจริง"ทางลัด"ที่ดำเนินการโดยรวมเปลี่ยนเซลล์แถวในลักษณะที่ดูเหมือนว่าพวกเขาจะถูกนำไปใช้กับคอลัมน์ นั่นคือคุณสามารถดำเนินการบนคอลัมน์โดยเพียงแค่ซ้ำกว่าดัชนีเซลล์แถวตารางเดียวกัน.

ตัวอย่างรหัสต่อไปนี้ช่วยลดความยุ่งยากในการดำเนินงานดังกล่าวโดยการพิสูจน์ชั้นอาคารที่รวบรวมเซลล์ที่สร้างขึ้น"คอลัมน์"ของตาราง:

// 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

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกคอลัมน์ว่างลงในตาราง:

// 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)));
}

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาคอลัมน์ออกจากตารางในเอกสาร:

// 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

ระบุแถวเป็นแถวส่วนหัว

คุณสามารถเลือกที่จะทำซ้ำแถวแรกในตารางเป็นแถวส่วนหัวเฉพาะในหน้าแรกหรือในแต่ ในAspose.Wordsคุณสามารถทำซ้ำแถวส่วนหัวในทุกหน้าโดยใช้คุณสมบัติHeadingFormat.

นอกจากนี้คุณยังสามารถทำเครื่องหมายแถวส่วนหัวหลายแถวหากแถวดังกล่าวอยู่หนึ่งหลัง เมื่อต้องการทำเช่นนี้คุณต้องใช้คุณสมบัติHeadingFormatกับแถวเหล่านี้.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างตารางซึ่งรวมถึงแถวส่วนหัวที่ซ้ำในหน้าถัดไป:

// 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");

เก็บตารางและแถวจากการทำลายหน้าเว็บ

มีบางครั้งที่ไม่ควรแบ่งเนื้อหาของตารางในหน้าเว็บ ชื่อเรื่องอยู่เหนือตารางชื่อเรื่องและตารางควรจะเก็บไว้ร่วมกันในหน้าเดียวกันเพื่อรักษา.

มีสองเทคนิคที่แยกต่างหากที่มีประโยชน์เพื่อให้บรรลุการทำงานนี้:

  • Allow row break across pagesซึ่งถูกนำไปใช้กับแถวตาราง
  • Keep with nextซึ่งถูกนำไปใช้กับย่อหน้าในเซลล์ตาราง

โดยค่าเริ่มต้นคุณสมบัติข้างต้นถูกปิดใช้งาน.

เก็บแถวไม่ให้แบ่งหน้า

การจำกัดเนื้อหาภายในเซลล์ของแถวไม่ให้ถูกแยกข้ามหน้าเว็บ ในMicrosoft Wordนี้สามารถพบได้ภายใต้คุณสมบัติของตารางเป็นตัวเลือก"อนุญาตให้แถวแบ่งหน้า" ในAspose.Wordsนี้พบภายใต้วัตถุRowFormatของRowเป็นคุณสมบัติRowFormat.AllowBreakAcrossPages.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีปิดใช้งานการแบ่งแถวในหน้าสำหรับแต่ละแถวในตาราง:

// 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");

เก็บตารางจากการแบ่งหน้า

เมื่อต้องการหยุดตารางไม่ให้แยกหน้าเราจำเป็นต้องระบุว่าเราต้องการให้เนื้อหาที่อยู่.

เมื่อต้องการทำเช่นนี้Aspose.Wordsใช้วิธีการซึ่งอนุญาตให้ผู้ใช้เลือกตารางและเปิดใช้พารามิเตอร์KeepWithNextเป็นจริงสำหรับแต่ละย่อหน้าภายในเซลล์ของตาราง ข้อยกเว้นเป็นย่อหน้าสุดท้ายในตารางซึ่งควรจะตั้งเป็นเท็จ.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าตารางให้อยู่ร่วมกันในหน้าเดียวกัน:

// 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");