ผสานเซลล์ตาราง

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

วิธีผสานเซลล์ตาราง

ใน Aspose.Words เซลล์ที่ผสานจะแสดงด้วยคุณสมบัติของคลาส CellFormat ต่อไปนี้:

  • HorizontalMerge ซึ่งอธิบายว่าเซลล์เป็นส่วนหนึ่งของการผสานเซลล์ในแนวนอนหรือไม่
  • VerticalMerge ซึ่งอธิบายว่าเซลล์เป็นส่วนหนึ่งของการผสานเซลล์ในแนวตั้งหรือไม่

ค่าของคุณสมบัติเหล่านี้จะกำหนดพฤติกรรมการผสานของเซลล์:

  • เซลล์แรกในลำดับของเซลล์ที่ผสานจะมี CellMerge.First
  • เซลล์ที่รวมเข้าด้วยกันในเวลาต่อมาจะมี CellMerge.Previous
  • เซลล์ที่ไม่ถูกผสานจะมี CellMerge.None

ตรวจสอบว่าเซลล์ถูกผสานหรือไม่

หากต้องการตรวจสอบว่าเซลล์เป็นส่วนหนึ่งของลำดับของเซลล์ที่ผสานหรือไม่ เราเพียงตรวจสอบคุณสมบัติ HorizontalMerge และ VerticalMerge

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการพิมพ์ชนิดการผสานเซลล์แนวนอนและแนวตั้ง:

# 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 with merged cells.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
for row in table.rows:
for cell in row.as_row().cells:
print(self.print_cell_merge_type(cell.as_cell()))

ผสานเซลล์ตารางเมื่อใช้ DocumentBuilder

หากต้องการผสานเซลล์ในตารางที่สร้างด้วย DocumentBuilder คุณต้องตั้งค่าประเภทการผสานที่เหมาะสมสำหรับเซลล์แต่ละเซลล์ที่คาดว่าจะมีการผสาน - CellMerge.First แรกแล้วตามด้วย CellMerge.Previous

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

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

# 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.insert_cell()
builder.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST
builder.write("Text in merged cells.")
builder.insert_cell()
# This cell is merged to the previous and should be empty.
builder.cell_format.horizontal_merge = aw.tables.CellMerge.PREVIOUS
builder.end_row()
builder.insert_cell()
builder.cell_format.horizontal_merge = aw.tables.CellMerge.NONE
builder.write("Text in one cell.")
builder.insert_cell()
builder.write("Text in another cell.")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTables.horizontal_merge.docx")

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

# 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.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.FIRST
builder.write("Text in merged cells.")
builder.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE
builder.write("Text in one cell")
builder.end_row()
builder.insert_cell()
# This cell is vertically merged to the cell above and should be empty.
builder.cell_format.vertical_merge = aw.tables.CellMerge.PREVIOUS
builder.insert_cell()
builder.cell_format.vertical_merge = aw.tables.CellMerge.NONE
builder.write("Text in another cell")
builder.end_row()
builder.end_table()
doc.save(ARTIFACTS_DIR + "WorkingWithTables.vertical_merge.docx")

รวมเซลล์ตารางในกรณีอื่นๆ

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

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
@staticmethod
def merge_cells(start_cell: aw.tables.Cell, end_cell: aw.tables.Cell):
parent_table = start_cell.parent_row.parent_table
# Find the row and cell indices for the start and end cell.
start_cell_pos = drawing.Point(start_cell.parent_row.index_of(start_cell), parent_table.index_of(start_cell.parent_row))
end_cell_pos = drawing.Point(end_cell.parent_row.index_of(end_cell), parent_table.index_of(end_cell.parent_row))
# Create a range of cells to be merged based on these indices.
# Inverse each index if the end cell is before the start cell.
merge_range = drawing.Rectangle(
min(start_cell_pos.x, end_cell_pos.x),
min(start_cell_pos.y, end_cell_pos.y),
abs(end_cell_pos.x - start_cell_pos.x) + 1,
abs(end_cell_pos.y - start_cell_pos.y) + 1)
for row in parent_table.rows:
row = row.as_row()
for cell in row.cells:
cell = cell.as_cell()
current_pos = drawing.Point(row.index_of(cell), parent_table.index_of(row))
# Check if the current cell is inside our merge range, then merge it.
if merge_range.contains(current_pos):
cell.cell_format.horizontal_merge = aw.tables.CellMerge.FIRST if current_pos.x == merge_range.x else aw.tables.CellMerge.PREVIOUS
cell.cell_format.vertical_merge = aw.tables.CellMerge.FIRST if current_pos.y == merge_range.y else aw.tables.CellMerge.PREVIOUS
view raw merge-cells.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 + "Table with merged cells.docx")
table = doc.first_section.body.tables[0]
# We want to merge the range of cells found inbetween these two cells.
cell_start_range = table.rows[0].cells[0]
cell_end_range = table.rows[1].cells[1]
# Merge all the cells between the two specified cells into one.
self.merge_cells(cell_start_range, cell_end_range)
doc.save(ARTIFACTS_DIR + "WorkingWithTables.merge_cell_range.docx")

ขึ้นอยู่กับเวอร์ชันของ Framework ที่คุณใช้ คุณอาจต้องการปรับแต่งวิธีนี้โดยเปลี่ยนเป็นวิธีการขยาย ในกรณีนี้ คุณสามารถเรียกใช้เมธอดนี้ได้โดยตรงบนเซลล์เพื่อผสานช่วงของเซลล์ เช่น cell1.Merge(cell2)

แปลงเป็นเซลล์ที่ผสานในแนวนอน

บางครั้งก็เป็นไปไม่ได้ที่จะตรวจสอบว่าเซลล์ใดถูกผสาน เนื่องจาก Microsoft Word เวอร์ชันใหม่บางเวอร์ชันไม่ใช้แฟล็กผสานอีกต่อไปเมื่อเซลล์ถูกผสานในแนวนอน แต่สำหรับสถานการณ์ที่เซลล์ถูกผสานเข้ากับเซลล์ในแนวนอนตามความกว้างโดยใช้แฟล็กผสาน Aspose.Words จัดเตรียมวิธี ConvertToHorizontallyMergedCells เพื่อแปลงเซลล์ วิธีนี้จะแปลงตารางและเพิ่มเซลล์ใหม่ตามต้องการ

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

# 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 with merged cells.docx")
table = doc.first_section.body.tables[0]
# Now merged cells have appropriate merge flags.
table.convert_to_horizontally_merged_cells()