Office 文件导入导出
使用 Exchange 在 Office 文件与 Collaboration 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 |
| Snapshot、changeset、revision 与持久化 | Collaboration SDK |
| 路径、格式策略、业务 API 与错误映射 | 业务应用 |
导入为协同 Unit
业务应用通过 Exchange 读取 Office 文件,再将得到的 UnitData 交给 Collaboration Server 创建 Unit:
Office 文件→ importFile()→ UnitData→ 业务 Server API→ Collaboration Unit初始 revision、业务元数据和返回结果均由应用决定。Unit 创建之后,CLI 和 Web 都通过 Server 操作它。
导出最新 Revision
导出时,CLI 通过 Collaboration Runtime 取得最新的 confirmed revision,再将完整的 UnitData 交给 Exchange:
Collaboration 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 实际渲染出来的结果。
你觉得这篇文档如何?