Skip to content

WASM Office ライブラリ — クイックスタート

office-oxide-wasm パッケージは office_oxide を WebAssembly にコンパイルしたものです。ネイティブ依存ゼロ、Rust コアの単一バイナリ、3 つのサブパスエントリで JavaScript が動く環境ならどこでも動きます — ブラウザ、Node.js、Vite/Webpack/Rollup などのバンドラー。

WASM のオーバーヘッドなしに Node.js で使うなら、ネイティブの office-oxide を選んでください。

インストール

npm install office-oxide-wasm

postinstall ステップなし。Node 18+、WebAssembly 対応のモダンブラウザ、ES モジュール / CommonJS ローダがある環境で動作します。

サブパスエクスポート

インポートパス ターゲット 形式
office-oxide-wasm(デフォルト) バンドラー向け ESM(Vite、Webpack、Rollup) ESM
office-oxide-wasm/node Node.js CommonJS
office-oxide-wasm/web <script type="module"> または import を使うブラウザ ESM、init() が必要

ランタイムに合わせて選んでください。

ドキュメントを読む

バンドラー(ESM)

import { WasmDocument } from 'office-oxide-wasm';

const res = await fetch('/report.docx');
const data = new Uint8Array(await res.arrayBuffer());
const doc = new WasmDocument(data, 'docx');
try {
  console.log(doc.plainText());
} finally {
  doc.free();
}

Node.js(CJS)

const { readFileSync } = require('node:fs');
const { WasmDocument } = require('office-oxide-wasm/node');

const data = readFileSync('report.docx');
const doc = new WasmDocument(data, 'docx');
try {
  console.log(doc.plainText());
} finally {
  doc.free();
}

ブラウザ(バンドラーなし)

<script type="module">
  import init, { WasmDocument } from 'https://cdn.jsdelivr.net/npm/office-oxide-wasm/web/office_oxide.js';
  await init();

  const res = await fetch('/report.docx');
  const data = new Uint8Array(await res.arrayBuffer());
  const doc = new WasmDocument(data, 'docx');
  try {
    document.body.textContent = doc.plainText();
  } finally {
    doc.free();
  }
</script>

ブラウザのみ: WasmDocument を構築する前に await init() が必須です。Node とバンドラー用エントリは初期化を内部で処理します。

コア API

WasmDocument は唯一のハンドルです — WASM ビルドには Document / EditableDocument の分離はありません(編集機能はネイティブバインディング側)。

import { WasmDocument } from 'office-oxide-wasm';

const doc = new WasmDocument(bytes, 'xlsx');
try {
  console.log(doc.formatName());   // "xlsx"
  console.log(doc.plainText());
  console.log(doc.toMarkdown());
  console.log(doc.toHtml());
  const ir = doc.toIr();           // パース済みオブジェクト
} finally {
  doc.free();   // 必須 — WASM メモリは GC 管理されません
}

メソッドはすべて camelCase: plainTexttoMarkdowntoHtmltoIrformatNamebytesUint8Arrayformat'docx' | 'xlsx' | 'pptx' | 'doc' | 'xls' | 'ppt' のいずれか。

レガシーバイナリ形式(docxlsppt)も WASM でパースされます。

編集(WASM では非対応)

EditableDocument — テキスト置換とセル書き込み — は WASM ビルドでは 公開されていません。編集が必要なら:

読み取り専用のテキスト / Markdown / HTML / IR ワークフローでは、WASM はフル機能です。

フォーマット非依存の IR

const doc = new WasmDocument(bytes, 'docx');
const ir = doc.toIr();
doc.free();

for (const section of ir.sections) {
  console.log(section.title);
  for (const el of section.elements) {
    // el.kind: "Heading" | "Paragraph" | "Table" | "List" | "Image" | ...
  }
}

IR スキーマは Rust の DocumentIR と完全に一致するので、サーバーサイドとクライアントサイドのパイプラインで同じプロセッサを共有できます。

バイト入力、バイト出力

WASM ビルドはバイト入力のみ — ファイル I/O サーフェスはありません。

// <input type="file"> から
const file = inputEl.files[0];
const data = new Uint8Array(await file.arrayBuffer());
const doc = new WasmDocument(data, file.name.split('.').pop().toLowerCase());

// fetch から
const res = await fetch(url);
const data = new Uint8Array(await res.arrayBuffer());

// Node から
const data = new Uint8Array(require('node:fs').readFileSync('file.docx'));

バンドラー

// Vite — 通常はゼロ設定
import { WasmDocument } from 'office-oxide-wasm';

// Webpack 5 — module.rules に追加:
//   { test: /\.wasm$/, type: 'asset/resource' }

TypeScript

型定義(office_oxide.d.ts)は各サブパスの JS グルーと一緒に配布されます。ルートからインポートするとバンドラー向けの型が自動的に効きます。import type { WasmDocument } from 'office-oxide-wasm/node' も動きます。

メモリ管理

const doc = new WasmDocument(bytes, 'docx');
try {
  // ... 作業
} finally {
  doc.free();
}

free() を忘れるとインスタンス破棄まで WASM メモリがリークします。

エラー

失敗は通常の Error インスタンスとして説明的なメッセージ付きで送出されます。WASM ビルドは JsValue::from_str を使うため、エラーコードは数値で公開されません — メッセージ文字列を確認してください。

トラブルシューティング

症状 対処
ReferenceError: WebAssembly is not defined ターゲットが WASM 非対応。ネイティブの office-oxide を使ってください。
ブラウザ: TypeError: ... before init() office-oxide-wasm/webawait init() を忘れています。
バンドラーが .wasm で文句 WASM アセットルールを追加するか、バンドラーターゲットを esnext に。
unsupported format: pdf 受け付けるのは 6 形式のみ: docxxlsxpptxdocxlsppt
ホットループでメモリが増え続ける 反復ごとに doc.free() を呼んでいません。

関連項目