Thinkfree Office ドキュメントに接続
Thinkfree Office の iframe を埋め込み、@thinkfree.dev/tfo-sdk を接続し、Thinkfree Office の準備ができたら開いているドキュメントの内容を読み取ります。
結果: SDK が iframe に接続され、開いているドキュメントのデータを返します。
始める前に
- Node.js と npm を用意します。
- Word、Spreadsheet、または Presentation ドキュメントの Thinkfree Office ドキュメント open URL を用意します。
- ホストページがその Thinkfree Office の origin を埋め込めることを確認します。
- 本番環境では HTTPS の origin を使用します。
npm で公開されている @thinkfree.dev/tfo-sdk をプロジェクトディレクトリにインストールします。
npm install @thinkfree.dev/tfo-sdk
通常の HTML で実行するには、index.html の <head> に次の import map を記述し、インストールされた ESM ファイルを紐付けます。以降の例の JavaScript は、iframe の後にある <script type="module"> 内に順番に記述します。バンドラーを使うプロジェクトでは、import map なしでパッケージ名で import できます。
<script type="importmap">
{"imports":{"@thinkfree.dev/tfo-sdk":"./node_modules/@thinkfree.dev/tfo-sdk/dist/index.js"}}
</script>
Python 3 がインストールされている場合は、プロジェクトディレクトリで次のコマンドを実行してローカルサーバーを起動し、
http://localhost:3000 を開きます。file:// で HTML を直接開かないでください。
python3 -m http.server 3000 --bind 127.0.0.1
1. Thinkfree Office を埋め込む
まず iframe の利用条件を確認してください。HTTP の Thinkfree Office は、HTTP のお客様のサービスページと同じサイトにある必要があります。別のサイトに埋め込むには、HTTPS の Thinkfree Office と対応する Cookie 設定を使用してください。iframe を使用できない場合はドキュメントを新しいタブで開く必要があり、以下の iframe SDK の例はそのタブを自動的には制御しません。
<iframe
id="office-frame"
title="Thinkfree Office document"
src="YOUR_OFFICE_DOCUMENT_OPEN_URL"
></iframe>
open URL は Thinkfree Office とストレージの統合が発行します。SDK は作成しません。
2. Thinkfree Office に接続
open URL のドキュメント種類に合ったエントリーポイントを呼び出します - Office.word、Office.cell(Spreadsheet)、Office.show(Presentation)。
import { Office } from "@thinkfree.dev/tfo-sdk";
const iframe = document.querySelector("#office-frame");
const word = Office.word(iframe); // origin is derived from iframe.src
Thinkfree Office の origin は正確である必要があり、* にはできません。SDK は他の origin からのメッセージを無視します。iframe にまだ src がない場合や、
data:/blob: URL の場合は、origin を明示してください:Office.word(iframe, { frameworkOrigin })。
3. Thinkfree Office を待ってからドキュメントを読み取る
whenReady() は Thinkfree Office の SDK ブリッジが応答するまでポーリングするため、iframe の読み込みが始まった直後にすぐ呼び出せます。
try {
await word.whenReady(); // default limit 60 s
const doc = word.getDocument(); // document handle; no round trip
const body = await doc.getBody();
const text = await body.getText();
console.log("Document text:", text);
} catch (error) {
console.error("Editor SDK request failed", error.code, error.message);
}
期待される結果:コンソールに Word ドキュメントのテキストが出力されます。タイムアウトは、Thinkfree Office の準備ができていない、origin が 誤っている、または Thinkfree Office のビルドが要求された SDK コマンドを公開していないことを意味します。
ドキュメントメソッドはアプリハンドルではなくドキュメントハンドルにあります。getDocument() は往復通信なしでそのハンドルを返すため、変数に保持しておき、
getBody() をはじめとするドキュメントメソッドをそのハンドルで呼び出してください。
4. 接続の解除
window.addEventListener("pagehide", () => word.disconnect());
disconnect() は冪等です。メッセージリスナーを削除し、待機中のリクエストを DESTROYED エラーで reject します。Thinkfree Office とドキュメントには
影響しません。Office.word(iframe) を再度呼び出すと新しいハンドルが返されます。iframe の src を別のドキュメントやモジュールに変更する
前に、接続を解除してください。
SDK に Thinkfree Office を開かせる
0.4.0 以降は、SDK が iframe を代わりに作成できます。Office.word.load()(および Office.cell.load()、Office.show.load())は
Thinkfree Office を非表示のまま開き、ドキュメントの読み込みが完了するとアプリハンドルで resolve するため、ユーザーには Thinkfree Office が表示されるまでの過程が見えません。
open() は同じオプションを受け取り、最初から Thinkfree Office を表示します。
import { Office } from "@thinkfree.dev/tfo-sdk";
const word = await Office.word.load({ container: "#editor", url: "YOUR_OFFICE_DOCUMENT_OPEN_URL" }); // hidden until ready
const text = await (await word.getDocument().getBody()).getText(); // works while hidden
word.show();
| オプション | 説明 |
|---|---|
container または iframe | どちらか一方だけを渡します。container(セレクターまたは要素)には新しい iframe を作成して追加し、iframe は既存の要素を使用します |
url | open URL。app= パラメーターがある場合はエントリーポイントと一致する必要があります(Word は Office.word) |
frame | SDK が作成する iframe の width、height、className、title、allow |
frameworkOrigin、timeoutMs | Office.word(iframe, options) と同じです。origin は url から導出されます |
readyLimitMs | Thinkfree Office の準備を待つ最大時間(既定 60 秒)。超えると TIMEOUT で reject されます |
signal | 読み込みをキャンセルする AbortSignal |
TIMEOUT またはキャンセル時は、SDK が作成した iframe は削除され、渡された iframe は表示状態だけが元に戻ります。ハンドルには show()、
hide()、visible、iframe、close() が追加されます。close() は接続を解除し、SDK が作成した iframe のみを削除します。
このフローが動作したら、Word、Spreadsheet、Presentation の API リファレンスで正確なメソッド、パラメーター、戻り値の型を確認します。
モジュールを試す
以下のコードは、上記の HTML・import map・サーバー設定を使用します。例を 1 つ選んでモジュールスクリプトに記述し、iframe に対応するドキュメント種類を読み込みます。Word と Presentation の例はドキュメントを変更するため、サンプルまたはコピーで実行してください。
Word
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();
const para = await body.insertParagraph("Hello from tfo-sdk");
await para.setStyle(Word.Style.HEADING_1);
Spreadsheet
// Navigation calls build a path; the terminal call makes the request.
import { Office } from "@thinkfree.dev/tfo-sdk";
const iframe = document.querySelector("#office-frame");
const cell = Office.cell(iframe);
await cell.whenReady();
const ws = await cell.getWorkbook().getWorksheet(0);
const values = await ws.getRange("A1:B2").getValues();
console.log("A1:B2 values:", values);
Presentation
import { Office, ShapePreset } from "@thinkfree.dev/tfo-sdk";
const iframe = document.querySelector("#office-frame");
const show = Office.show(iframe);
await show.whenReady();
const slides = await show.getDocument().getSlides();
if (slides.length === 0) throw new Error("Open a presentation with at least one slide.");
const shape = await slides[0].insertShape(ShapePreset.RECT);
1 つの iframe は 1 つのモジュールにバインドされます。Spreadsheet や Presentation を開くには、対応する open URL を iframe(または別の iframe)に
読み込み、その iframe に Office.cell または Office.show を使用してください。
トラブルシューティング
| 症状 | 確認と対処 |
|---|---|
INVALID_ARGUMENT | iframe 要素とドキュメント種類を確認します。別のモジュールに接続した iframe は disconnect() してから新たに接続します。 |
TIMEOUT | まず iframe 内の open エラーを確認します。ドキュメントが正常に表示された後、正確な origin と Thinkfree Office・SDK のバージョンの組み合わせを確認して再試行します。 |
FRAMEWORK_ERROR | Thinkfree Office の操作が返した error.data.error.code |
| メッセージを受信しない | ホストと iframe の origin が構成された統合と一致している必要があります |
モジュールガイド - Word API、Spreadsheet API、Presentation API - に進むか、本番アプリケーションに SDK を追加する前にアーキテクチャとライフサイクルをお読みください。