การทำงานกับข้อความในตาราง

บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด.

การเพิ่มข้อความหรือเนื้อหาอื่นๆลงในตารางจะดำเนินการโดยใช้วิธีการที่เหมาะสมของชั้นเรียน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/IEC29500 เมื่อบันทึกในรูปแบบก่อนหน้านี้กว่าISO/IEC29500 คุณสมบัติเหล่านี้จะถูกละเว้น.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าคุณสมบัติชื่อและคำอธิบายของตาราง:

// 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);