テーブル内のテキストを操作する

以前の記事で述べたように、テーブルには通常プレーンテキストが含まれますが、画像や他のテーブルなどの他のコンテンツはテーブルセルに配置できま

テーブルへのテキストまたはその他のコンテンツの追加は、DocumentBuilderクラスの適切なメソッドを使用して実行され、**“テーブルの作成”**記事で説明されています。 この記事では、既存のテーブルのテキストを操作する方法について説明します。

テーブル内のテキストを置換する

テーブルはAspose.Words内の他のノードと同様に、Rangeオブジェクトにアクセスできます。 Table 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 titletable descriptionがあります。

Aspose.Wordsでは、TitleプロパティとDescriptionプロパティを使用してテーブルのタイトルと説明を追加することもできます。 これらの特性はISO/IEC29500に準拠するDOCX文書にとって意味があります。 ISO/IEC29500より前の形式で保存する場合、これらのプロパティは無視されます。

テーブルのtitleプロパティとdescriptionプロパティを設定する方法を次のコード例に示します:

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