# インポートとエクスポート

- Human documentation: [https://docs.univer.ai/ja-JP/guides/docs/features/import-export](https://docs.univer.ai/ja-JP/guides/docs/features/import-export)

- Agent Markdown: [https://docs.univer.ai/ja-JP/guides/docs/features/import-export.md](https://docs.univer.ai/ja-JP/guides/docs/features/import-export.md)

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.1.0`

- Source: [docs/features/import-export.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/features/import-export.ja-JP.mdx)

---

以下のブラウザー API を使用する前に[インポート・エクスポート統合](https://docs.univer.ai/ja-JP/server/import-export.md)を完了してください。アップロード、変換、タスク照会、認可されたダウンロードの契約をアプリ側で実装します。スナップショット API にも変換バックエンドが必要です。[Exchange サンプル](https://docs.univer.ai/ja-JP/server/collaboration/examples.md#exchange)は Sheets を対象とするため、他の製品や形式の対応状況は個別に確認してください。

#### Package metadata

```json
{
  "preset": [
    {
      "client": "@univerjs/preset-docs-advanced",
      "locale": "@univerjs/preset-docs-advanced/locales/ja-JP",
      "style": "@univerjs/preset-docs-advanced/lib/index.css"
    }
  ],
  "plugins": [
    {
      "client": "@univerjs-pro/exchange-client",
      "locale": "@univerjs-pro/exchange-client/locale/ja-JP",
      "style": "@univerjs-pro/exchange-client/lib/index.css",
      "facade": "@univerjs-pro/exchange-client/facade"
    },
    {
      "client": "@univerjs-pro/docs-exchange-client",
      "facade": "@univerjs-pro/docs-exchange-client/facade",
      "locale": "@univerjs-pro/docs-exchange-client/locale/ja-JP"
    }
  ],
  "license": true,
  "server": true
}
```

## 対応形式

以下は本ページのブラウザー API が対応する形式です。Office ファイルの変換には、同じドキュメント種類と形式に対応したバックエンドが必要です。

| インポート           | エクスポート  |
| --------------- | ------- |
| `.doc`, `.docx` | `.docx` |

## Facade API

### インポート

> [!INFO: プラグインモード注意]
> Facade パッケージの手動 import が必要なのはプラグインモードのみです。プリセットモードには対応する Facade
> パッケージが内蔵されているため、追加の import は不要です。

```typescript
import '@univerjs-pro/exchange-client/facade'
import '@univerjs-pro/docs-exchange-client/facade'
```

### `.docx` のインポート

#### `.docx` をインポートして `unitId` を取得

> [!WARNING: 注意事項]
> この方法は共同編集が必要です。サーバーにドキュメントを作成し `unitId`
> を返します。共同編集がない場合はスナップショットを使用してください。

共同編集が有効な場合、各ドキュメントは一意の `unitId` を持ちます。`univerAPI.importDocToUnitIdAsync` に `file` を渡すと `unitId` が返され、その `unitId` でドキュメントにアクセスできます。`file` は `File` オブジェクトまたはリモート URL を指定できます。

```typescript
import '@univerjs-pro/collaboration-client/facade'

const unitId = await univerAPI.importDocToUnitIdAsync(file)

if (unitId) {
  await univerAPI.getCollaboration().loadDocAsync(unitId)
}
```

#### `.docx` をインポートして `IDocumentData` を取得

> [!NOTE]
> スナップショットのインポートは共同編集不要です。共同編集ドキュメントは 
> 
> `unitId`
> 
>  を使用してください。

`univerAPI.importDocToSnapshotAsync` を使用すると、`.docx` を `IDocumentData` 形式で取得できます。

```typescript
// File オブジェクトを受け取る
const snapshot = await univerAPI.importDocToSnapshotAsync(file)
// リモート URL を受け取る
// const snapshot = await univerAPI.importDocToSnapshotAsync('https://example.com/filename.docx');

// スナップショットから新規ドキュメントを作成
if (snapshot) univerAPI.createDocument(snapshot)
```

### `.docx` のエクスポート

#### `unitId` で `.docx` をエクスポート

> [!WARNING: 注意事項]
> `unitId` でのエクスポートは共同編集が必要です。共同編集がない場合はスナップショットを使用してください。

共同編集が有効な場合、各ドキュメントには一意の `unitId` があります。`univerAPI.exportDocByUnitIdAsync` に `unitId` を渡すと `File` が返ります。

```typescript
const fDocument = univerAPI.getActiveDocument()
const unitId = fDocument.getId()
const file = await univerAPI.exportDocByUnitIdAsync(unitId)

// Facade API でファイルをダウンロード
if (file) univerAPI.downloadFile(file, 'univer', 'docx')
```

#### `IDocumentData` で `.docx` をエクスポート

> [!NOTE]
> スナップショットのエクスポートは共同編集不要で、ローカル処理向きです。

`univerAPI.exportDocBySnapshotAsync` に `IDocumentData` を渡すと `File` が返ります。

```typescript
const fDocument = univerAPI.getActiveDocument()
const snapshot = fDocument.save()
const file = await univerAPI.exportDocBySnapshotAsync(snapshot)

// Facade API でファイルをダウンロード
if (file) univerAPI.downloadFile(file, 'univer', 'docx')
```
