ทำงานกับคอลัมน์และแถว
หากต้องการควบคุมวิธีการทำงานของตารางได้มากขึ้น โปรดเรียนรู้วิธีจัดการคอลัมน์และแถว
ค้นหาดัชนีองค์ประกอบตาราง
คอลัมน์ แถว และเซลล์ได้รับการจัดการโดยการเข้าถึงโหนดเอกสารที่เลือกตามดัชนี การค้นหาดัชนีของโหนดใดๆ เกี่ยวข้องกับการรวบรวมโหนดย่อยทั้งหมดของประเภทองค์ประกอบจากโหนดหลัก จากนั้นใช้วิธี IndexOf เพื่อค้นหาดัชนีของโหนดที่ต้องการในคอลเลกชัน
ค้นหาดัชนีของตารางในเอกสาร
บางครั้งคุณอาจต้องทำการเปลี่ยนแปลงตารางใดตารางหนึ่งในเอกสาร เมื่อต้องการทำเช่นนี้ คุณสามารถอ้างอิงถึงตารางตามดัชนีได้
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการดึงดัชนีของตารางในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
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-.NET.git. | |
int rowIndex = table.IndexOf(table.LastRow); |
ค้นหาดัชนีของเซลล์ในแถว
สุดท้ายนี้ คุณอาจต้องเปลี่ยนแปลงเซลล์ใดเซลล์หนึ่ง และคุณสามารถทำได้โดยใช้ดัชนีเซลล์เช่นกัน
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงดัชนีของเซลล์ในแถว:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
int cellIndex = row.IndexOf(row.Cells[4]); |
ทำงานกับคอลัมน์
ใน Aspose.Words Document Object Model (DOM) โหนด Table ประกอบด้วยโหนด Row และโหนด Cell ดังนั้นใน Document
Object Model ของ Aspose.Words เช่นเดียวกับในเอกสาร Word จึงไม่มีแนวคิดเกี่ยวกับคอลัมน์
ตามการออกแบบ แถวของตารางใน Microsoft Word และ Aspose.Words มีความเป็นอิสระอย่างสมบูรณ์ และคุณสมบัติพื้นฐานและการดำเนินการจะอยู่ในแถวและเซลล์ของตารางเท่านั้น สิ่งนี้ทำให้ตารางมีคุณลักษณะที่น่าสนใจบางอย่างได้:
- แต่ละแถวของตารางสามารถมีจำนวนเซลล์ที่แตกต่างกันโดยสิ้นเชิง
- ในแนวตั้ง เซลล์ของแต่ละแถวสามารถมีความกว้างต่างกันได้
- สามารถรวมตารางที่มีรูปแบบแถวและจำนวนเซลล์ต่างกันได้
การดำเนินการใดๆ ที่ทำกับคอลัมน์จริงๆ แล้วถือเป็น “ทางลัด” ที่ทำการดำเนินการโดยการเปลี่ยนเซลล์แถวร่วมกันในลักษณะที่ดูเหมือนว่ากำลังนำไปใช้กับคอลัมน์ นั่นคือ คุณสามารถดำเนินการกับคอลัมน์ได้โดยเพียงแค่วนซ้ำดัชนีเซลล์แถวตารางเดียวกัน
ตัวอย่างโค้ดต่อไปนี้ช่วยลดความยุ่งยากในการดำเนินการดังกล่าวโดยการพิสูจน์คลาสส่วนหน้าที่รวบรวมเซลล์ที่ประกอบเป็น “คอลัมน์” ของตาราง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
/// <summary> | |
/// Represents a facade object for a column of a table in a Microsoft Word document. | |
/// </summary> | |
internal class Column | |
{ | |
private Column(Table table, int columnIndex) | |
{ | |
mTable = table ?? throw new ArgumentException("table"); | |
mColumnIndex = columnIndex; | |
} | |
/// <summary> | |
/// Returns a new column facade from the table and supplied zero-based index. | |
/// </summary> | |
public static Column FromIndex(Table table, int columnIndex) | |
{ | |
return new Column(table, columnIndex); | |
} | |
/// <summary> | |
/// Returns the cells which make up the column. | |
/// </summary> | |
public Cell[] Cells => GetColumnCells().ToArray(); | |
/// <summary> | |
/// Returns the index of the given cell in the column. | |
/// </summary> | |
public int IndexOf(Cell cell) | |
{ | |
return GetColumnCells().IndexOf(cell); | |
} | |
/// <summary> | |
/// Inserts a brand new column before this column into the table. | |
/// </summary> | |
public Column InsertColumnBefore() | |
{ | |
Cell[] columnCells = Cells; | |
if (columnCells.Length == 0) | |
throw new ArgumentException("Column must not be empty"); | |
// Create a clone of this column. | |
foreach (Cell cell in columnCells) | |
cell.ParentRow.InsertBefore(cell.Clone(false), cell); | |
// This is the new column. | |
Column column = new Column(columnCells[0].ParentRow.ParentTable, mColumnIndex); | |
// We want to make sure that the cells are all valid to work with (have at least one paragraph). | |
foreach (Cell cell in column.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> | |
public void Remove() | |
{ | |
foreach (Cell cell in Cells) | |
cell.Remove(); | |
} | |
/// <summary> | |
/// Returns the text of the column. | |
/// </summary> | |
public string ToTxt() | |
{ | |
StringBuilder builder = new StringBuilder(); | |
foreach (Cell cell in Cells) | |
builder.Append(cell.ToString(SaveFormat.Text)); | |
return builder.ToString(); | |
} | |
/// <summary> | |
/// Provides an up-to-date collection of cells which make up the column represented by this facade. | |
/// </summary> | |
private List<Cell> GetColumnCells() | |
{ | |
List<Cell> columnCells = new List<Cell>(); | |
foreach (Row row in mTable.Rows) | |
{ | |
Cell cell = row.Cells[mColumnIndex]; | |
if (cell != null) | |
columnCells.Add(cell); | |
} | |
return columnCells; | |
} | |
private int mColumnIndex; | |
private readonly Table mTable; | |
} |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกคอลัมน์ว่างลงในตาราง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Column column = Column.FromIndex(table, 0); | |
// Print the plain text of the column to the screen. | |
Console.WriteLine(column.ToTxt()); | |
// Create a new column to the left of this column. | |
// This is the same as using the "Insert Column Before" command in Microsoft Word. | |
Column newColumn = column.InsertColumnBefore(); | |
foreach (Cell cell in newColumn.Cells) | |
cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell))); |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบคอลัมน์ออกจากตารางในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 1, true); | |
Column column = Column.FromIndex(table, 2); | |
column.Remove(); |
ระบุแถวเป็นแถวส่วนหัว
คุณสามารถเลือกที่จะทำซ้ำแถวแรกในตารางเป็นแถวส่วนหัวเฉพาะในหน้าแรกหรือในแต่ละหน้าได้หากตารางถูกแบ่งออกเป็นหลายรายการ ใน Aspose.Words คุณสามารถทำซ้ำแถวส่วนหัวในทุกหน้าโดยใช้คุณสมบัติ HeadingFormat
คุณยังสามารถทำเครื่องหมายแถวส่วนหัวได้หลายแถวหากแถวดังกล่าวอยู่ติดกันที่จุดเริ่มต้นของตาราง ในการดำเนินการนี้ คุณจะต้องใช้คุณสมบัติ HeadingFormat กับแถวเหล่านี้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการสร้างตารางซึ่งรวมถึงแถวส่วนหัวที่ทำซ้ำในหน้าถัดไป:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.StartTable(); | |
builder.RowFormat.HeadingFormat = true; | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.CellFormat.Width = 100; | |
builder.InsertCell(); | |
builder.Writeln("Heading row 1"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.Writeln("Heading row 2"); | |
builder.EndRow(); | |
builder.CellFormat.Width = 50; | |
builder.ParagraphFormat.ClearFormatting(); | |
for (int i = 0; i < 50; i++) | |
{ | |
builder.InsertCell(); | |
builder.RowFormat.HeadingFormat = false; | |
builder.Write("Column 1 Text"); | |
builder.InsertCell(); | |
builder.Write("Column 2 Text"); | |
builder.EndRow(); | |
} | |
doc.Save(ArtifactsDir + "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-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// Disable breaking across pages for all rows in the table. | |
foreach (Row row in table.Rows) | |
row.RowFormat.AllowBreakAcrossPages = false; | |
doc.Save(ArtifactsDir + "WorkingWithTables.RowFormatDisableBreakAcrossPages.docx"); |
ป้องกันไม่ให้โต๊ะแตกข้ามหน้า
หากต้องการหยุดตารางไม่ให้แยกข้ามหน้า เราต้องระบุว่าเราต้องการให้เนื้อหาที่อยู่ในตารางอยู่ด้วยกัน
ในการดำเนินการนี้ Aspose.Words จะใช้วิธีการซึ่งอนุญาตให้ผู้ใช้เลือกตารางและเปิดใช้งานพารามิเตอร์ KeepWithNext เป็น true สำหรับแต่ละย่อหน้าภายในเซลล์ตาราง ข้อยกเว้นคือย่อหน้าสุดท้ายในตาราง ซึ่งควรตั้งค่าเป็น false
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าตารางให้อยู่ด้วยกันในหน้าเดียวกัน:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table spanning two pages.docx"); | |
Table table = (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. | |
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true)) | |
{ | |
cell.EnsureMinimum(); | |
foreach (Paragraph para in cell.Paragraphs) | |
if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell)) | |
para.ParagraphFormat.KeepWithNext = true; | |
} | |
doc.Save(ArtifactsDir + "WorkingWithTables.KeepTableTogether.docx"); |