עבודה עם טקסט בטבלה

כאמור במאמרים קודמים, טבלה מכילה בדרך כלל טקסט רגיל, אם כי ניתן להציב תוכן אחר כגון תמונות או אפילו טבלאות אחרות בתאי טבלה.

הוספת טקסט או תוכן אחר לטבלה מתבצעת בשיטות המתאימות של הכיתה DocumentBuilder ומתוארת במאמר “Create a Table”. במאמר זה, נדבר על איך לעבוד עם טקסט בטבלה קיימת כבר.

החלף טקסט בטבלה

לטבלה, כמו לכל צומת אחר ב - Aspose.Words, יש גישה לאובייקט Range. באמצעות אובייקט טווח הטבלה, באפשרותך להחליף טקסט בטבלה.

היכולת להשתמש בתווים מיוחדים בעת החלפה נתמכת כעת, כך שניתן להחליף טקסט קיים בטקסט רב פסקאות. לשם כך, עליך להשתמש במטא-תווים המיוחדים המתוארים בשיטת Replace המתאימה.

דוגמת הקוד הבאה מראה כיצד להחליף את כל המופעים של מחרוזת טקסט בתאים של טבלה שלמה:

// 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));
table->get_Range()->Replace(u"Carrots", u"Eggs", MakeObject<FindReplaceOptions>(FindReplaceDirection::Forward));
table->get_LastRow()->get_LastCell()->get_Range()->Replace(u"50", u"20", MakeObject<FindReplaceOptions>(FindReplaceDirection::Forward));
doc->Save(ArtifactsDir + u"FindAndReplace.ReplaceTextInTable.docx");
view raw replace-text.h hosted with ❤ by GitHub

חלץ טקסט רגיל מטבלה או תא

באמצעות האובייקט Range, אתה יכול גם לקרוא לשיטות בכל טווח הטבלה ולחלץ את הטבלה כטקסט רגיל. לשם כך, השתמש במאפיין Text.

דוגמת הקוד הבאה מראה כיצד להדפיס את טווח הטקסט של טבלה:

// 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));
// The range text will include control characters such as "\a" for a cell.
// You can call ToString and pass SaveFormat.Text on the desired node to find the plain text content.
std::cout << "Contents of the table: " << std::endl;
std::cout << table->get_Range()->get_Text() << std::endl;
view raw extract-text.h hosted with ❤ by GitHub

אותה טכניקה משמשת להפקת תוכן מתאי טבלה בודדים בלבד.

דוגמת הקוד הבאה מראה כיצד להדפיס טווח טקסט של רכיבי שורה וטבלה:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
std::cout << "\nContents of the row: " << std::endl;
std::cout << table->get_Rows()->idx_get(1)->get_Range()->get_Text() << std::endl;
std::cout << "\nContents of the cell: " << std::endl;
std::cout << table->get_LastRow()->get_LastCell()->get_Range()->get_Text() << std::endl;

עבודה עם טקסט טבלה חלופי

Microsoft Word לטבלאות יש table title ו table description המספקים ייצוג טקסטואלי חלופי של המידע הכלול בטבלה.

ב Aspose.Words, אתה יכול גם להוסיף כותרת טבלה ותיאור באמצעות המאפיינים Title ו Description. מאפיינים אלה משמעותיים עבור DOCX מסמכים התואמים את ISO/IEC 29500. בעת שמירה בפורמטים לפני ISO / IEC 29500, מאפיינים אלה מתעלמים.

דוגמת הקוד הבאה מראה כיצד להגדיר את מאפייני הכותרת והתיאור של טבלה:

// 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));
table->set_Title(u"Test title");
table->set_Description(u"Test description");
auto options = MakeObject<OoxmlSaveOptions>();
options->set_Compliance(OoxmlCompliance::Iso29500_2008_Strict);
doc->get_CompatibilityOptions()->OptimizeFor(Settings::MsWordVersion::Word2016);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.TableTitleAndDescription.docx", options);