Web Workers

以下範例說明如何在 Univer 中為公式計算配置 Worker1。

在 Sheets 中,Worker 通常承擔公式計算等計算密集型任務。把這類工作放到 Worker 程序中執行,可以減輕主執行緒的負擔;工作簿包含大量公式時,頁面渲染和互動不會受到影響。

外掛模式

本節以安裝和基本使用中的外掛模式為基礎。渲染和 UI 外掛保留在主執行緒。請將下面三個計算外掛的註冊替換為範例中的寫法,並在建立工作簿之前註冊主執行緒 RPC 外掛。

UniverRPCMainThreadPlugin

  • workerURL
    string | URL | Worker
    連線至負責公式計算的 Worker。

UniverFormulaEnginePlugin | UniverSheetsPlugin | UniverSheetsFormulaPlugin

  • notExecuteFormula
    boolean預設值: false
    公式交給 Worker 計算時,略過主執行緒的公式計算。
TypeScript
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'import { UniverRPCMainThreadPlugin } from '@univerjs/rpc'import { UniverSheetsPlugin } from '@univerjs/sheets'import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula'univer.registerPlugin(UniverRPCMainThreadPlugin, {  workerURL: new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),})univer.registerPlugin(UniverFormulaEnginePlugin, { notExecuteFormula: true })univer.registerPlugin(UniverSheetsPlugin, { notExecuteFormula: true })univer.registerPlugin(UniverSheetsFormulaPlugin, { notExecuteFormula: true })

Worker 中只包含下面列出的資料和計算外掛,不應註冊 UI 或渲染外掛。在主執行緒中,notExecuteFormula: true 表示把公式計算交給 Worker。

UniverSheetsPlugin

  • onlyRegisterFormulaRelatedMutations
    boolean預設值: false
    在 Worker 中只註冊公式計算所需的變更。
TypeScript
import { LocaleType, Univer } from '@univerjs/core'import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula/worker'import { UniverRPCWorkerThreadPlugin } from '@univerjs/rpc/worker'import { UniverSheetsPlugin } from '@univerjs/sheets/worker'import { UniverRemoteSheetsFormulaPlugin } from '@univerjs/sheets-formula/worker'const univer = new Univer({ locale: LocaleType.ZH_TW })univer.registerPlugin(UniverSheetsPlugin, { onlyRegisterFormulaRelatedMutations: true })univer.registerPlugin(UniverFormulaEnginePlugin)univer.registerPlugin(UniverRPCWorkerThreadPlugin)univer.registerPlugin(UniverRemoteSheetsFormulaPlugin)

使用篩選功能時,兩個執行緒都需要註冊 UniverSheetsFilterPlugin。缺少該外掛時,SUBTOTAL 等公式無法判斷哪些行已被篩選隱藏。主執行緒從 @univerjs/sheets-filter 匯入該外掛,Worker 從 @univerjs/sheets-filter/worker 匯入。UniverSheetsFilterUIPlugin 保留在主執行緒。

Worker 中如需新增其他計算外掛,請使用下面列出的入口;主執行緒從對應包的根入口匯入。所有 SDK 包的版本需保持一致。

TypeScript
import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula/worker'import { UniverSheetsPivotChartWorkerPlugin } from '@univerjs-pro/sheets-pivot-chart/worker'import { UniverSheetsPivotTablePlugin } from '@univerjs-pro/sheets-pivot/worker'import { UniverSheetsSlicerPlugin } from '@univerjs-pro/sheets-slicer/worker'import { UniverSheetsTablePlugin } from '@univerjs/sheets-table/worker'

使用透視表需要不同的配置。兩個執行緒都要把 UniverFormulaEnginePlugin 替換為 UniverProFormulaEnginePlugin,並同時註冊 UniverLicensePlugin 和 UniverSheetsPivotTablePlugin。主執行緒的 Pro 公式引擎和透視表外掛設 notExecuteFormula: true,Worker 中設 false。兩個執行環境都需要傳入許可證。透視表 UI 保留在主執行緒,自定義公式的實現也需要在兩個公式引擎中註冊。

UniverLicensePlugin

  • license
    string
    用戶端授權檔的內容。

UniverProFormulaEnginePlugin

  • notExecuteFormula
    boolean
    公式交由 Worker 計算時,略過目前執行環境的公式計算。

UniverSheetsPivotTablePlugin

  • notExecuteFormula
    boolean
    主執行緒傳 true,Worker 傳 false。採用單執行緒計算時省略。

預設模式

UniverSheetsCorePreset

  • workerURL
    string | URL | Worker
    連線至負責公式計算的 Worker。

示例:sheets-core-with-worker

本範例引入 sheets-filter 預設套件,原因是 sheets-filter 會隱藏行,而隱藏行會影響 SUBTOTAL 的計算。公式計算在 Worker 中執行時,sheets-filter 也需要在 Worker 中執行。

TypeScript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsFilterPreset } from '@univerjs/preset-sheets-filter'import UniverPresetSheetsFilterZhTW from '@univerjs/preset-sheets-filter/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-filter/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsFilterZhTW,    ),  },  presets: [    UniverSheetsCorePreset({      workerURL: new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),    }),    UniverSheetsFilterPreset(),  ],})univerAPI.createWorkbook({ name: 'Test Sheet' })
TypeScript
import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsCoreWorkerPreset } from '@univerjs/preset-sheets-core/worker'import UniverPresetSheetsFilterZhTW from '@univerjs/preset-sheets-filter/locales/zh-TW'import { UniverSheetsFilterWorkerPreset } from '@univerjs/preset-sheets-filter/worker'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsFilterZhTW,    ),  },  presets: [    UniverSheetsCoreWorkerPreset(),    UniverSheetsFilterWorkerPreset(),  ],})

示例:sheets-collaboration-with-worker

UniverSheetsDrawingPreset

  • collaboration
    boolean預設值: false
    啟用協作圖片儲存,由協作服務處理圖片讀寫。

UniverSheetsAdvancedPreset

  • useWorker
    boolean預設值: false
    在 Worker 中啟用進階計算外掛。
  • license
    string
    用戶端授權檔的內容。

UniverSheetsAdvancedWorkerPreset

  • license
    string
    用戶端授權檔的內容。
TypeScript
import { UniverSheetsAdvancedPreset } from '@univerjs/preset-sheets-advanced'import UniverPresetSheetsAdvancedZhTW from '@univerjs/preset-sheets-advanced/locales/zh-TW'import { UniverSheetsCollaborationPreset } from '@univerjs/preset-sheets-collaboration'import UniverPresetSheetsCollaborationZhTW from '@univerjs/preset-sheets-collaboration/locales/zh-TW'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing'import UniverPresetSheetsDrawingZhTW from '@univerjs/preset-sheets-drawing/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-drawing/lib/index.css'import '@univerjs/preset-sheets-advanced/lib/index.css'import '@univerjs/preset-sheets-collaboration/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsDrawingZhTW,      UniverPresetSheetsAdvancedZhTW,      UniverPresetSheetsCollaborationZhTW,    ),  },  collaboration: true,  presets: [    UniverSheetsCorePreset({      workerURL: new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }),    }),    UniverSheetsDrawingPreset({      collaboration: true,    }),    UniverSheetsAdvancedPreset({      useWorker: true,      license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',    }),    UniverSheetsCollaborationPreset({    }),  ],})
TypeScript
import UniverPresetSheetsAdvancedZhTW from '@univerjs/preset-sheets-advanced/locales/zh-TW'import { UniverSheetsAdvancedWorkerPreset } from '@univerjs/preset-sheets-advanced/worker'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { UniverSheetsCoreWorkerPreset } from '@univerjs/preset-sheets-core/worker'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'createUniver({  locale: LocaleType.ZH_TW,  locales: {    [LocaleType.ZH_TW]: mergeLocales(      UniverPresetSheetsCoreZhTW,      UniverPresetSheetsAdvancedZhTW,    ),  },  presets: [    UniverSheetsCoreWorkerPreset(),    UniverSheetsAdvancedWorkerPreset({      license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',    }),  ],})

Footnotes

  1. Web Worker 在瀏覽器主執行緒之外執行 JavaScript,透過訊息與主執行緒通訊,無法直接存取頁面 DOM。將計算移入 Worker 不代表將編輯器介面也移入其中。 ↩

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.