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

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

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

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.1.0`

- Source: [sheets/features/import-export.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/sheets/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-sheets-advanced",
      "locale": "@univerjs/preset-sheets-advanced/locales/ja-JP",
      "style": "@univerjs/preset-sheets-advanced/lib/index.css"
    }
  ],
  "plugins": [
    {
      "client": "@univerjs-pro/exchange-client",
      "facade": "@univerjs-pro/exchange-client/facade",
      "locale": "@univerjs-pro/exchange-client/locale/ja-JP",
      "style": "@univerjs-pro/exchange-client/lib/index.css"
    },
    {
      "client": "@univerjs-pro/sheets-exchange-client",
      "facade": "@univerjs-pro/sheets-exchange-client/facade",
      "locale": "@univerjs-pro/sheets-exchange-client/locale/ja-JP"
    }
  ],
  "license": true,
  "limitations": "CSV/TSV は一つのシートのみを書式なしで出力します。このブラウザー API は XLSM を受け付けません。",
  "headless": false,
  "server": true
}
```

## 対応形式

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

| インポート                           | エクスポート                  |
| ------------------------------- | ----------------------- |
| `.xls`, `.xlsx`, `.csv`, `.tsv` | `.xlsx`, `.csv`, `.tsv` |

## Facade API

### インポート

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

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

### `.xlsx` インポート

#### `.xlsx` ファイルをインポートして `unitId` を取得

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

共同編集を有効にすると、各ドキュメントには一意の `unitId` が割り当てられます。`univerAPI.importSheetToUnitIdAsync` に `file` パラメーターを渡すと `unitId` が返され、これを使ってドキュメントにアクセスできます。`file` には `File` オブジェクトまたはリモート ファイル URL を指定できます。

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

const unitId = await univerAPI.importSheetToUnitIdAsync(file)

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

#### `.xlsx` ファイルをインポートして `IWorkbookData` を取得

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

`univerAPI.importSheetToSnapshotAsync` で `.xlsx` ファイルをインポートすると、`IWorkbookData` 形式のドキュメント データが返されます。

```typescript
// File オブジェクトを受け取る
const snapshot = await univerAPI.importSheetToSnapshotAsync(file)
// またはリモート ファイル URL を受け取る
// const snapshot = await univerAPI.importSheetToSnapshotAsync('https://example.com/filename.xlsx');

// スナップショットから新しいワークブックを作成
if (snapshot) univerAPI.createWorkbook(snapshot)
```

### `.xlsx` エクスポート

#### `unitId` を使って `.xlsx` ファイルをエクスポート

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

共同編集を有効にした場合、各ドキュメントには一意の `unitId` があります。`univerAPI.exportSheetByUnitIdAsync` に `unitId` を渡すと `File` オブジェクトが返されます。

```typescript
const fWorkbook = univerAPI.getActiveWorkbook()
const unitId = fWorkbook.getId()
const file = await univerAPI.exportSheetByUnitIdAsync(unitId)

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

#### `IWorkbookData` を使って `.xlsx` ファイルをエクスポート

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

`univerAPI.exportSheetBySnapshotAsync` に `IWorkbookData` を渡すと `File` オブジェクトが返されます。

```typescript
const fWorkbook = univerAPI.getActiveWorkbook()
const snapshot = fWorkbook.save()
const file = await univerAPI.exportSheetBySnapshotAsync(snapshot)

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