範例

按能力執行 Collaboration SDK 範例,查看各範例指南、服務端與 Web 入口。

所有範例位於 univer-collaboration-examples,採用相同的最小 web/main.ts + server/main.ts 結構並獨立執行。先完成快速開始,再依整合任務選擇其他範例。

範例主要概念命令
快速開始核心協同流程pnpm example:quick-start
資料庫配接器資料庫配接器pnpm example:database-adapter
權限身分與授權pnpm example:permissions
歷史衍生版本歷史pnpm example:history
留言獨立留言pnpm example:comments
Worktree隔離編輯與合併pnpm example:worktree
檔案轉換Office 檔案轉換pnpm example:exchange
多節點部署水平擴展pnpm example:multi-node

快速開始

組合 Collaboration Client、Transport、Endpoint、Service 與 Memory Adapter。使用兩個瀏覽器驗證同一 Sheet 的 HTTP 載入、WebSocket Session、Room、ACK 與廣播。

Bash
pnpm example:quick-start

開啟 http://127.0.0.1:3010/?unit=quick-start-sheet。

資料庫配接器

展示內建 SQLite Adapter 的用法,以及自訂 Database Adapter 的實作。

Bash
pnpm example:database-adapter

開啟 http://127.0.0.1:3010/?unit=persistent-sheet。

權限

從 Cookie 建立可信身分,分別保護 Unit HTTP 讀取、即時 JOIN 與變更集提交。頁面可切換教學用 Alice · Editor 與唯讀 Bob · Viewer。

Bash
pnpm example:permissions

開啟 http://127.0.0.1:3010。

歷史

在核心之外加入 History Service、獨立 Adapter、History Endpoint 與瀏覽器歷史入口。透過 Unit 建立及變更集提交 Events,將 History 建為可重建的衍生索引。

Bash
pnpm example:history

開啟 http://127.0.0.1:3010/?unit=history-sheet,編輯 Sheet,再選取頁面頂部的 History。

留言

加入 Thread Comment Service、獨立 Adapter、Comment Endpoint 與前端留言入口。Comment Endpoint 重用主協同 Endpoint 的 Session 和 Unit Room。

Bash
pnpm example:comments

開啟 http://127.0.0.1:3010/?unit=comments-sheet,選取儲存格並使用留言入口。

Worktree

展示隔離草稿、Ready、Reopen 與逐 Unit Merge 的完整生命週期。切換主幹與草稿,觀察 AI Agent 的變更在合併前如何保持隔離。

Bash
pnpm example:worktree

開啟 http://127.0.0.1:3010/?unit=worktree-sheet&worktree=demo-worktree。

檔案轉換

組合 @univerjs-pro/exchange-node 與 Collaboration Service,實現服務端 Sheet 匯入匯出、任務輪詢、快照匯入與已確認修訂版本匯出。

Bash
pnpm example:exchange

開啟 http://127.0.0.1:3010/?unit=exchange-sheet。透過 File → Open (File) 匯入,透過 File → Save As 匯出。

多節點部署

在 Nginx 後執行兩個協同服務節點。Redis 在節點之間轉送即時更新並共用 Session Ticket,任何節點都能處理任何請求。需要 Docker Compose。

Bash
pnpm example:multi-node

開啟 http://127.0.0.1:3010/,點選 New Sheet 建立檔案。將完整 URL 複製到另一個分頁, 驗證同一文件的協同編輯;再建立幾份檔案,觀察它們在不同節點上的分配情況。

範例不是正式應用

固定使用者、教學授權、Memory Adapter、本機密鑰及記憶體檔案與任務儲存僅供教學。身分、授權、持久化、密鑰、檔案、部署與產品互動仍由應用負責。

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.