通过 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-CN"> <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-CN.js', '@univerjs/ui@1.6.2/lib/umd/locale/zh-CN.js', '@univerjs/sheets@1.6.2/lib/umd/locale/zh-CN.js', '@univerjs/sheets-ui@1.6.2/lib/umd/locale/zh-CN.js', '@univerjs/docs-ui@1.6.2/lib/umd/locale/zh-CN.js', '@univerjs/sheets-formula-ui@1.6.2/lib/umd/locale/zh-CN.js', '@univerjs/sheets-numfmt-ui@1.6.2/lib/umd/locale/zh-CN.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_CN, locales: { [LocaleType.ZH_CN]: mergeLocales( UniverDesignZhCN, UniverUiZhCN, UniverSheetsZhCN, UniverSheetsUiZhCN, UniverDocsUiZhCN, UniverSheetsFormulaUiZhCN, UniverSheetsNumfmtUiZhCN, ), }, }) 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 应用,请改用包管理器接入。依赖由包管理器管理,无需自己维护一长串脚本。
你觉得这篇文档如何?