Office 檔案匯入匯出
使用 Exchange 在 Office 檔案與協同 Unit 之間匯入匯出內容。
Office 檔案轉換由 @univerjs-pro/exchange-node 完成,不屬於 AI SDK 的職責。CLI 應用在檔案邊界呼叫 importFile() 和 exportToFile(),隨後的 Unit 操作交由 AI SDK 和 Collaboration SDK 處理。
保持 SDK 職責邊界
import { exportToFile, importFile } from "@univerjs-pro/exchange-node";| 能力 | 歸屬 |
|---|---|
| Office 檔案與 UnitData 互轉 | Exchange |
| Headless Runtime、內容檢查與 Facade 執行 | AI SDK |
| 快照、變更集、修訂版本與持久化 | Collaboration SDK |
| 路徑、格式策略、業務 API 與錯誤對映 | 業務應用程式 |
匯入協同 Unit
業務應用透過 Exchange 讀取 Office 檔案,再將得到的 UnitData 交給 Collaboration Server 建立 Unit:
Office 檔案→ importFile()→ UnitData→ 業務 Server API→ 協同 Unit初始 revision、業務後設資料和返回結果均由應用決定。Unit 建立之後,CLI 和 Web 都透過 Server 操作它。
匯出最新修訂版本
匯出時,CLI 透過 Collaboration Runtime 取得最新的 confirmed revision,再將完整的 UnitData 交給 Exchange:
協同 Unit→ 最新 UnitData→ exportToFile()→ Office 檔案匯出格式由輸出檔案的副檔名決定。
透過 Commander 提供檔案操作入口
AI SDK 沒有為 Office 檔案匯入匯出提供預設命令包。業務應用使用 Commander 將 Exchange、Collaboration Runtime 與自己的 Server API 組合起來:
import { Command } from "commander";interface IFileExchangeDependencies { importFile(path: string): Promise<unknown>; createUnit(unitData: unknown): Promise<unknown>; loadLatestUnitData(unitId: string): Promise<unknown>; exportFile(unitData: unknown, path: string): Promise<void>; writeResult(result: unknown): void;}function addFileExchangeCommands(program: Command, dependencies: IFileExchangeDependencies): void { program.addCommand( new Command("import").argument("<office-file>").action(async (sourcePath) => { const unitData = await dependencies.importFile(sourcePath); const created = await dependencies.createUnit(unitData); dependencies.writeResult(created); }), ); program.addCommand( new Command("export") .argument("<office-file>") .requiredOption("--unit <id>") .action(async (outputPath, { unit }) => { const unitData = await dependencies.loadLatestUnitData(unit); await dependencies.exportFile(unitData, outputPath); }), );}所有不屬於 Commander 的能力都顯式宣告在 IFileExchangeDependencies 中,命令不依賴隱藏的 helper。檔案轉換由應用呼叫 Exchange 的 importFile() 和 exportToFile() 完成,loadLatestUnitData() 基於 Collaboration Runtime 實現,Server API 和結果輸出也由應用注入。
支援格式
| Unit | 匯入 | 匯出 |
|---|---|---|
| Sheet | .xls, .xlsx, .xlsm, .csv, .tsv | .xlsx, .csv, .tsv |
| Doc | .doc, .docx | .docx |
| Slide | .ppt, .pptx, .pptm, .ppsx, .ppsm, .potx | .pptx |
副檔名與 Unit 型別不匹配時,應用應在生成檔案之前拒絕。
只使用本機 CLI
應用也可以不使用 Collaboration Server,將匯入的內容直接交給本地 Headless Runtime,再匯出 Office 檔案。這種形態適合一次性處理,但不提供共享 revision、即時 Web 預覽、History 和 Worktree。
命令名稱、路徑參數和結果展示由業務應用自行決定。協同 Unit 的完整路徑見文件內容載入與讀寫。檔案邊界建立之後,可繼續閱讀視覺檢查,由 Agent 檢查 UnitData 實際渲染出來的結果。
你覺得這篇文檔如何?