CDN 経由での Web SDK のインポート
最初に完全な HTML の例を実行し、それをあなた自身のページにコピーします。 これにより、依存関係、スタイル、言語パック、およびフォーエイドの登録が、式で編集可能なスプレッドシートが要求されます。
編集できるシートを試す
- サンプルを開くか、下の HTML をダウンロードして
index.htmlとして保存します。 - インターネットアクセスでモダンなブラウザでファイルを開きます。ビルドツールやサーバーは必要ありません。インターフェイスとサンプルデータは英語です。
- Notebook 行は 36 を合計します。 3から 4への数量を変更し、合計は 48になります。他のセルも編集でき、変更は元に戻すことができます。
完全な HTML サンプル
UniverUIPlugin
containerstring | HTMLElementエディターを配置する DOM 要素またはその ID。
この例では、React 18.3.1 UMDの特長 を読み込みます。このグローバル React オブジェクトは、Univer UMD バンドルが期待する jsx または jsxs を提供していません。2 行のブリッジは、パブリック React.createElement API を介してエントリ ポイントを供給します。パッケージマネージャを介してインストールすると、ビルドツールは react/jsx-runtime を解決します。そのため、この UMD ブリッジは、そのセットアップにコピーする必要はありません。
リソースは、依存の順序で jsDelivr から読み込まれます。リクエストが失敗すると、例えば unpkg で同じバージョンを解放し、続行します。両方のプロバイダが失敗した場合は、リソースが読み込まれることができないことを停止し、報告します。
<!doctype html><html lang="ja-JP"> <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/ja-JP.js', '@univerjs/ui@1.6.2/lib/umd/locale/ja-JP.js', '@univerjs/sheets@1.6.2/lib/umd/locale/ja-JP.js', '@univerjs/sheets-ui@1.6.2/lib/umd/locale/ja-JP.js', '@univerjs/docs-ui@1.6.2/lib/umd/locale/ja-JP.js', '@univerjs/sheets-formula-ui@1.6.2/lib/umd/locale/ja-JP.js', '@univerjs/sheets-numfmt-ui@1.6.2/lib/umd/locale/ja-JP.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.JA_JP, locales: { [LocaleType.JA_JP]: mergeLocales( UniverDesignJaJP, UniverUiJaJP, UniverSheetsJaJP, UniverSheetsUiJaJP, UniverDocsUiJaJP, UniverSheetsFormulaUiJaJP, UniverSheetsNumfmtUiJaJP, ), }, }) 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スクリプトは、モジュールをグローバルオブジェクトとしてエクスポートします。初期化コードは、プラグインとファサード API にアクセスするために、これらの名前を使用します。
| パッケージまたはエントリー | グローバル名 |
|---|---|
@univerjs/core | UniverCore |
@univerjs/docs-ui | UniverDocsUi |
@univerjs/core/facade | UniverCoreFacade |
@univerjs/docs-ui/locale/en-US | UniverDocsUiEnUS |
バージョンをそろえる
詳しくはこちら Univer この例のパッケージはピン留めされます 1.6.2, React そしてReactDOMに 18.3.1および RxJS への 7.8.2. アップグレードするときは、すべてを更新して下さい Univer ファサード、CSS、および locale を含む URL をまとめました。 これらのライブラリの 2 番目のコピーを既存のアプリケーション バンドルと一緒に読み込まないでください。
トラブルシューティング
| 症状 | 確認すること |
|---|---|
ITelemetryService | @univerjs/telemetryを最初にロードし、それに依存する@univerjs/sheets-ui。 依存関係リストからエントリを削除しないでください。 |
FDocument | @univerjs/docs-ui/facadeの前に@univerjs/docs/facadeを読み込んでください。 シートはDocsのテキストエンジンを使用します。 |
[redi], t is not a function | 完全な例から始めて、各依存関係のコピーを1つ保存し、すべてのバージョンを揃えたままにします。 ブラウザネットワークパネルで失敗したリクエストをチェックしてください。 |
| エディターが空白 | コンテナに明示的な高さを与えます。ネットワークまたはCSPがCDNをブロックする場合、依存関係を自分でホストするか、パッケージマネージャーの統合に切り替えます。 |
React または Vue アプリケーションをお持ちの場合は、代わりに パッケージマネージャーの統合を使用してください。パッケージマネージャは、長いスクリプトリストを維持しない依存関係を処理します。
このガイドはいかがですか?