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

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

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

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

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
all_tables = doc.get_child_nodes(aw.NodeType.TABLE, True)
table_index = all_tables.index_of(table)

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
row_index = table.index_of(table.last_row)

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

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
cell_index = row.index_of(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-Python-via-.NET.git.
class Column:
"""Represents a facade object for a column of a table in a Microsoft Word document."""
def __init__(self, table: aw.tables.Table, column_index: int):
if table is None:
raise ValueError("table")
self.table = table
self.column_index = column_index
def index_of(self, cell: aw.tables.Cell):
"""Returns the index of the given cell in the column."""
return self.get_column_cells().index(cell)
def insert_column_before(self):
"""Inserts a brand new column before this column into the table."""
column_cells = self.get_column_cells()
if len(column_cells) == 0:
raise ValueError("Column must not be empty")
# Create a clone of this column.
for cell in column_cells:
cell.parent_row.insert_before(cell.clone(False), cell)
# This is the new column.
column = self.__class__(column_cells[0].parent_row.parent_table, self.column_index)
# We want to make sure that the cells are all valid to work with (have at least one paragraph).
for cell in column.get_column_cells():
cell.ensure_minimum()
# Increase the index which this column represents since there is now one extra column in front.
self.column_index += 1
return column
def remove(self):
"""Removes the column from the table."""
for cell in self.get_column_cells():
cell.remove()
def to_txt(self):
"""Returns the text of the column."""
return "".join(cell.to_string(aw.SaveFormat.TEXT) for cell in self.get_column_cells())
def get_column_cells(self):
"""Provides an up-to-date collection of cells which make up the column represented by this facade."""
column_cells = []
for row in self.table.rows:
cell = row.as_row().cells[self.column_index]
if cell is not None:
column_cells.append(cell)
return column_cells
view raw column-class.py hosted with ❤ by GitHub

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
column = self.Column(table, 0)
# Print the plain text of the column to the screen.
print(column.to_txt())
# Create a new column to the left of this column.
# This is the same as using the "Insert Column Before" command in Microsoft Word.
new_column = column.insert_column_before()
for cell in new_column.get_column_cells():
cell.first_paragraph.append_child(aw.Run(doc, "Column Text " + str(new_column.index_of(cell))))

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
table = doc.get_child(aw.NodeType.TABLE, 1, True).as_table()
column = self.Column(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-Python-via-.NET.git.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.start_table()
builder.row_format.heading_format = True
builder.paragraph_format.alignment = aw.ParagraphAlignment.CENTER
builder.cell_format.width = 100
builder.insert_cell()
builder.writeln("Heading row 1")
builder.end_row()
builder.insert_cell()
builder.writeln("Heading row 2")
builder.end_row()
builder.cell_format.width = 50
builder.paragraph_format.clear_formatting()
for _ in range(50):
builder.insert_cell()
builder.row_format.heading_format = False
builder.write("Column 1 Text")
builder.insert_cell()
builder.write("Column 2 Text")
builder.end_row()
doc.save(ARTIFACTS_DIR + "WorkingWithTables.repeat_rows_on_subsequent_pages.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-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Table spanning two pages.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# Disable breaking across pages for all rows in the table.
for row in table.rows:
row.as_row().row_format.allow_break_across_pages = False
doc.save(ARTIFACTS_DIR + "WorkingWithTables.row_format_disable_break_across_pages.docx")

ป้องกันไม่ให้โต๊ะแตกข้ามหน้า

หากต้องการหยุดตารางไม่ให้แยกข้ามหน้า เราต้องระบุว่าเราต้องการให้เนื้อหาที่อยู่ในตารางอยู่ด้วยกัน

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Table spanning two pages.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# 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 cell in table.get_child_nodes(aw.NodeType.CELL, True):
cell = cell.as_cell()
cell.ensure_minimum()
for para in cell.paragraphs:
para = para.as_paragraph()
if not (cell.parent_row.is_last_row and para.is_end_of_cell):
para.paragraph_format.keep_with_next = True
doc.save(ARTIFACTS_DIR + "WorkingWithTables.keep_table_together.docx")