دمج خلايا الجدول

في بعض الأحيان، تتطلب صفوف معينة في الجدول عنوانًا أو كتلًا كبيرة من النص تشغل العرض الكامل للجدول. للحصول على تصميم مناسب للجدول، يمكن للمستخدم دمج عدة خلايا في جدول واحد. يدعم 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")

اعتمادًا على إصدار إطار العمل الذي تستخدمه، قد ترغب في تحسين هذه الطريقة عن طريق تحويلها إلى طريقة ملحقة. في هذه الحالة، يمكنك استدعاء هذه الطريقة مباشرة على خلية لدمج نطاق من الخلايا، مثل 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()