العمل مع الأعمدة والصفوف
لمزيد من التحكم في كيفية عمل الجداول، تعرف على كيفية معالجة الأعمدة والصفوف.
ابحث عن فهرس عنصر الجدول
تتم إدارة الأعمدة والصفوف والخلايا عن طريق الوصول إلى عقدة المستند المحددة بواسطة فهرسها. يتضمن العثور على فهرس أي عقدة جمع جميع العقد الفرعية لنوع العنصر من العقدة الأم، ثم استخدام طريقة 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; | |
} | |
}; |
يوضح مثال التعليمات البرمجية التالية كيفية إدراج عمود فارغ في جدول:
// 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(); |
حدد الصفوف كصفوف رأس
يمكنك اختيار تكرار الصف الأول في الجدول كصف رأس فقط في الصفحة الأولى أو في كل صفحة إذا تم تقسيم الجدول إلى عدة. في 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"); |