透過 CDN 使用 Web SDK
請先執行本頁的完整 HTML 範例,再將其複製到自己的頁面。範例已經包含可編輯表格和公式計算所需的依賴、樣式、語言套件與 Facade 註冊。
先體驗可編輯的試算表
- 開啟範例,或者下載下方的 HTML 並儲存為
index.html。 - 使用可聯網的現代瀏覽器開啟該檔案,不需要建置工具,也不需要伺服器端。範例介面和資料均為繁體中文。
- 筆記本這一行的合計為 36。把數量從 3 改為 4,合計變為 48。其他單元格同樣可以編輯,修改也可以撤銷。
完整 HTML 範例
UniverUIPlugin
containerstring | HTMLElement編輯器掛載的 DOM 元素或元素 ID。
範例使用 React 18.3.1 UMD。該全域性 React 物件不提供 Univer UMD 包所需的 jsx 和 jsxs,下方的兩行橋接程式碼透過公開的 React.createElement API 補齊這兩個入口。透過包管理器接入時,建置工具會解析 react/jsx-runtime,不應把這段 UMD 橋接複製到該環境中。
資源按依賴順序從 jsDelivr 載入。某個請求失敗時,範例會以相同版本在 unpkg 上重試,成功後再繼續載入。兩個源都失敗時,範例停止載入,並報告未能載入的資源。
HTML
<!doctype html><html lang="zh-TW"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Univer Sheets CDN 範例</title> <style> html, body, #root, #app { padding: 0; margin: 0; height: 100%; } </style> </head> <body> <div id="app"><p role="status" style="padding: 24px; font-family: system-ui">正在載入 Univer…</p></div> <script> const cdnBases = ['https://cdn.jsdelivr.net/npm/', 'https://unpkg.com/'] const app = document.getElementById('app') async function loadResource(path, tag = 'script') { for (const base of cdnBases) { const element = document.createElement(tag) const url = base + path element.crossOrigin = 'anonymous' if (tag === 'link') { element.rel = 'stylesheet' element.href = url } else { element.src = url } try { await new Promise((resolve, reject) => { element.onload = () => resolve() element.onerror = () => reject(new Error(url)) document.head.appendChild(element) }) return } catch { element.remove() } } throw new Error(`Could not load ${path} from ${cdnBases.join(' or ')}`) } const styles = [ '@univerjs/design@1.6.2/lib/index.css', '@univerjs/ui@1.6.2/lib/index.css', '@univerjs/docs-ui@1.6.2/lib/index.css', '@univerjs/sheets-ui@1.6.2/lib/index.css', '@univerjs/sheets-formula-ui@1.6.2/lib/index.css', '@univerjs/sheets-numfmt-ui@1.6.2/lib/index.css', ] const reactScripts = [ 'react@18.3.1/umd/react.production.min.js', 'react-dom@18.3.1/umd/react-dom.production.min.js', ] const sdkScripts = [ 'rxjs@7.8.2/dist/bundles/rxjs.umd.min.js', '@wendellhu/redi@1.1.3/dist/umd/index.js', '@wendellhu/redi@1.1.3/dist/umd/react-bindings/index.js', '@univerjs/themes@1.6.2/lib/umd/index.js', '@univerjs/protocol@1.6.2/lib/umd/index.js', '@univerjs/core@1.6.2/lib/umd/index.js', '@univerjs/network@1.6.2/lib/umd/index.js', '@univerjs/rpc@1.6.2/lib/umd/index.js', '@univerjs/design@1.6.2/lib/umd/index.js', '@univerjs/engine-render@1.6.2/lib/umd/index.js', '@univerjs/engine-formula@1.6.2/lib/umd/index.js', '@univerjs/drawing@1.6.2/lib/umd/index.js', '@univerjs/telemetry@1.6.2/lib/umd/index.js', '@univerjs/ui@1.6.2/lib/umd/index.js', '@univerjs/docs@1.6.2/lib/umd/index.js', '@univerjs/docs-ui@1.6.2/lib/umd/index.js', '@univerjs/sheets@1.6.2/lib/umd/index.js', '@univerjs/sheets-ui@1.6.2/lib/umd/index.js', '@univerjs/sheets-formula@1.6.2/lib/umd/index.js', '@univerjs/sheets-formula-ui@1.6.2/lib/umd/index.js', '@univerjs/sheets-numfmt@1.6.2/lib/umd/index.js', '@univerjs/sheets-numfmt-ui@1.6.2/lib/umd/index.js', '@univerjs/core@1.6.2/lib/umd/facade.js', '@univerjs/engine-formula@1.6.2/lib/umd/facade.js', '@univerjs/ui@1.6.2/lib/umd/facade.js', '@univerjs/docs@1.6.2/lib/umd/facade.js', '@univerjs/docs-ui@1.6.2/lib/umd/facade.js', '@univerjs/sheets@1.6.2/lib/umd/facade.js', '@univerjs/sheets-ui@1.6.2/lib/umd/facade.js', '@univerjs/sheets-formula@1.6.2/lib/umd/facade.js', '@univerjs/sheets-numfmt@1.6.2/lib/umd/facade.js', '@univerjs/design@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/ui@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/sheets@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/sheets-ui@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/docs-ui@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/sheets-formula-ui@1.6.2/lib/umd/locale/zh-TW.js', '@univerjs/sheets-numfmt-ui@1.6.2/lib/umd/locale/zh-TW.js', ] // Fetch ahead in parallel; main() still executes scripts in dependency order. for (const [as, paths] of [ ['style', styles], ['script', [...reactScripts, ...sdkScripts]], ]) { for (const path of paths) { const preload = document.createElement('link') preload.rel = 'preload' preload.as = as preload.crossOrigin = 'anonymous' preload.href = cdnBases[0] + path preload.onerror = () => { preload.onerror = null preload.href = cdnBases[1] + path } document.head.appendChild(preload) } } async function main() { for (const path of styles) await loadResource(path, 'link') for (const path of reactScripts) await loadResource(path) // Bridge the React 18 UMD global to the JSX runtime expected by Univer UMD bundles. React.jsx = (type, props, key) => React.createElement(type, key === undefined ? props : { ...props, key }) React.jsxs = React.jsx for (const path of sdkScripts) await loadResource(path) app.textContent = '' const { Univer, LocaleType, mergeLocales, UniverInstanceType } = UniverCore const { FUniver } = UniverCoreFacade const { UniverRenderEnginePlugin } = UniverEngineRender const { UniverFormulaEnginePlugin } = UniverEngineFormula const { UniverUIPlugin } = UniverUi const { UniverSheetsPlugin } = UniverSheets const { UniverSheetsUIPlugin } = UniverSheetsUi const { UniverDocsPlugin } = UniverDocs const { UniverDocsUIPlugin } = UniverDocsUi const { UniverSheetsFormulaPlugin } = UniverSheetsFormula const { UniverSheetsFormulaUIPlugin } = UniverSheetsFormulaUi const { UniverSheetsNumfmtPlugin } = UniverSheetsNumfmt const { UniverSheetsNumfmtUIPlugin } = UniverSheetsNumfmtUi const univer = new Univer({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( UniverDesignZhTW, UniverUiZhTW, UniverSheetsZhTW, UniverSheetsUiZhTW, UniverDocsUiZhTW, UniverSheetsFormulaUiZhTW, UniverSheetsNumfmtUiZhTW, ), }, }) univer.registerPlugin(UniverRenderEnginePlugin) univer.registerPlugin(UniverFormulaEnginePlugin) univer.registerPlugin(UniverUIPlugin, { container: 'app', }) univer.registerPlugin(UniverDocsPlugin) univer.registerPlugin(UniverDocsUIPlugin) univer.registerPlugin(UniverSheetsPlugin) univer.registerPlugin(UniverSheetsUIPlugin) univer.registerPlugin(UniverSheetsFormulaPlugin) univer.registerPlugin(UniverSheetsFormulaUIPlugin) univer.registerPlugin(UniverSheetsNumfmtPlugin) univer.registerPlugin(UniverSheetsNumfmtUIPlugin) univer.createUnit(UniverInstanceType.UNIVER_SHEET, {}) const univerAPI = FUniver.newAPI(univer) window.univerAPI = univerAPI univerAPI .getActiveWorkbook() .getActiveSheet() .getRange('A1:D3') .setValues([ ['品項', '數量', '單價', '合計'], ['筆記本', 3, 12, '=B2*C2'], ['原子筆', 5, 2, '=B3*C3'], ]) } main().catch((error) => { console.error(error) app.setAttribute('role', 'alert') app.style.cssText = 'padding: 24px; box-sizing: border-box; white-space: pre-wrap; overflow-wrap: anywhere; font: 14px/1.5 system-ui' app.textContent = '範例載入失敗,請檢查網路後重新整理頁面。' + '\n' + error.message }) </script> </body></html>命名空間規範
UMD 指令碼把模組匯出掛載為全域性物件。初始化程式碼透過這些名稱訪問外掛和 Facade API:
| 套件或入口 | 全域名稱 |
|---|---|
@univerjs/core | UniverCore |
@univerjs/docs-ui | UniverDocsUi |
@univerjs/core/facade | UniverCoreFacade |
@univerjs/docs-ui/locale/zh-CN | UniverDocsUiZhCN |
保持版本一致
範例中所有 Univer 包固定為 1.6.2,React 和 ReactDOM 為 18.3.1,RxJS 為 7.8.2。升級時需要一起更新所有 Univer URL,包括 Facade、CSS 和語言檔案。不要在已有應用旁重複載入這些庫。
疑難排解
| 現象 | 檢查方式 |
|---|---|
ITelemetryService | 先載入 @univerjs/telemetry,再載入依賴它的 @univerjs/sheets-ui。不要刪減依賴列表中的條目。 |
FDocument | 先載入 @univerjs/docs/facade,再載入 @univerjs/docs-ui/facade。Sheets 也會使用 Docs 的文字引擎。 |
[redi], t is not a function | 從完整範例開始,每個依賴只保留一份並保持版本一致。到瀏覽器的 Network 面板中檢查失敗的請求。 |
| 編輯器空白 | 為容器設定明確的高度。網路或 CSP 阻止 CDN 時,請自行託管依賴,或改用包管理器接入。 |
如果已有 React 或 Vue 應用,請改用包管理器接入。依賴由包管理器管理,無需自己維護一長串指令碼。
你覺得這篇文檔如何?