メインコンテンツまでスキップ

Word API

Word ドキュメントを開いた iframe には Office.word(iframe) を使用します。アプリハンドルはドキュメントルートを公開し、最上位メソッドを委譲するため、 word.getBody() は word.getDocument().getBody() と同じです。

まず Editor SDK クイックスタートの配布ファイル・import map・HTTP サーバーの設定を完了し、 このドキュメント種類を office-frame iframe で開きます。以下の JavaScript ブロックは同じモジュールスクリプト内で順番に 使用します。書き込みの例はドキュメントを変更するため、サンプルまたはコピーで実行してください。

オブジェクトモデル​

ルートハンドルは Word.WordDocument です。現在のカタログの主なオブジェクト型:Body、Section、Paragraph、Range、Table、 TableRow、TableCell、Shape、Image、Chart、Bookmark、Hyperlink、List、DocumentInfo。

すべてのハンドルメソッドは Thinkfree Office との往復通信であり、Promise を返します。別のオブジェクトを返すメソッドは新しいハンドルを返します。

テキストの読み取りと書き込み​

import { Office, Word } from "@thinkfree.dev/tfo-sdk";

const iframe = document.querySelector("#office-frame");
const word = Office.word(iframe);
await word.whenReady();

const doc = word.getDocument();
const body = await doc.getBody();
console.log(await body.getText());

const title = await body.insertParagraph("Quarterly summary");
await title.setStyle(Word.Style.HEADING_1);

const para = await body.insertParagraph("Revenue grew 15% over the previous quarter.");
await para.setParagraphFormat({ align: Word.TextAlign.JUSTIFY, lineSpacing: 1.5 });

定数は Word 名前空間にあります(例:Word.Style、Word.TextAlign、Word.ListType)。Office と一緒に名前空間を import してください。

範囲の検索と書式設定​

const hits = await body.search("review");
for (const range of hits) {
await range.setTextFormat({ highlightColor: "#FFFF00", bold: true });
}

特定の領域を編集するときは、範囲と段落のハンドルを使用します。1.0.0 正式版までは、API は Thinkfree Office の配布ファイルとともにバージョンが 管理されるプレビューであるため、メソッドを使用する前に生成された API カタログを確認してください。

表とリスト​

const table = await body.insertTable(3, 2, {
values: [["Item", "Value"], ["Revenue", "120"], ["Cost", "80"]],
});

const first = await body.insertParagraph("Lock the plan");
const last = await body.insertParagraph("Start development");
const range = await doc.getRange({ startParaId: await first.getId(), endParaId: await last.getId() });
await range.setList(Word.ListType.NUMBER_DECIMAL); // apply once over the whole range to keep one numbering sequence

エラー​

失敗したドキュメント操作は SDKError で reject されます。Thinkfree Office の構造化された応答は error.data にあります。

try {
await body.insertParagraph("…");
} catch (error) {
if (error.data?.error) console.warn(error.data.error.code, error.data.error.message);
}

エラーの一覧はドキュメントツールとエラーを参照してください。

API リファレンス​

現在の SDK ビルドの Word ハンドルで使用できるオブジェクト型ごとに、リファレンスページが 1 つずつあります。SDK の型定義から生成され、 メソッドの概要表に続いて、メソッドごとの TypeScript シグネチャ、パラメーター、戻り値が記載されています。すべてのメソッドは Promise を返します。 以下の表またはサイドバーからオブジェクト型を選んでください。

オブジェクト型メソッド取得方法
Word.Document37app.getDocument()
Word.Body25Document.getBody(), Section.getBody(), Section.getFooter(), …
Word.Section19Document.getSection(), Document.getSections(), Range.getParentSection()
Word.Paragraph49Document.getParagraph(), Body.getParagraph(), Body.getParagraphs(), …
Word.Range54Document.getRange(), Document.getSelection(), Body.getRange(), …
Word.Table26Document.getTable(), Body.getTables(), Body.insertTable(), …
Word.TableRow6Table.getRow(), Table.insertRow()
Word.TableCell15Paragraph.getParentTableCell(), Table.getCell(), TableRow.getCell(), …
Word.Shape24Document.getShape(), Document.getShapes(), Paragraph.insertShape(), …
Word.Image24Paragraph.insertImage(), Paragraph.insertWebVideo(), Range.insertImage(), …
Word.Chart40Document.getChart(), Document.getCharts()
Word.Bookmark5Document.getBookmark(), Document.getBookmarks(), Range.getBookmarks(), …
Word.Hyperlink8Document.getHyperlink(), Document.getHyperlinks(), Range.getHyperlink(), …
Word.List4Paragraph.getList()
Word.Comment7Document.getComment(), Document.getComments(), Body.getComments(), …
Word.Note7Document.getEndnotes(), Document.getFootnotes(), Document.getNote(), …
Word.Style3Document.getStyle(), Document.getStyles()
Word.ChangeTracking5Document.getChangeTracking(), Document.getChangeTrackings(), Body.getChangeTrackings(), …
Word.DocumentInfo0-

定数メンバーは Word.* ページにあります。