Web Workers
The examples below demonstrate how to configure a Worker1 for formula calculation in Univer.
In Sheets, a Worker commonly handles compute-intensive tasks such as formula calculation. Running this work in a Worker process reduces the load on the main thread, so page rendering and interaction remain responsive when a workbook contains many formulas.
Plugin mode
This section starts from the Plugin Mode setup in installation. Rendering and UI plugins remain on the main thread. Replace the registrations of the three calculation plugins with the versions below, and register the main-thread RPC plugin before creating the workbook.
UniverRPCMainThreadPlugin
workerURLstring | URL | WorkerConnects the editor to a Worker for formula calculation.
UniverFormulaEnginePlugin | UniverSheetsPlugin | UniverSheetsFormulaPlugin
notExecuteFormulabooleanDefault:falseSkips formula calculation on the main thread when it runs in the Worker.
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 })The Worker contains only the data and calculation plugins listed below. Do not register UI or rendering plugins in the Worker. On the main thread, notExecuteFormula: true delegates formula calculation to the Worker.
UniverSheetsPlugin
onlyRegisterFormulaRelatedMutationsbooleanDefault:falseRegisters only the changes needed for formula calculation in the Worker.
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.EN_US })univer.registerPlugin(UniverSheetsPlugin, { onlyRegisterFormulaRelatedMutations: true })univer.registerPlugin(UniverFormulaEnginePlugin)univer.registerPlugin(UniverRPCWorkerThreadPlugin)univer.registerPlugin(UniverRemoteSheetsFormulaPlugin)Filtering requires UniverSheetsFilterPlugin in both threads. Without this plugin, formulas such as SUBTOTAL cannot determine which rows filtering has hidden. Import the plugin from @univerjs/sheets-filter on the main thread and from @univerjs/sheets-filter/worker in the Worker. UniverSheetsFilterUIPlugin remains on the main thread.
For additional calculation plugins in the Worker, use the entry points below. The main thread imports from the root entry of each package. All SDK package versions must remain aligned.
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'Pivot tables require a different configuration. Replace UniverFormulaEnginePlugin with UniverProFormulaEnginePlugin in both threads, and register UniverLicensePlugin and UniverSheetsPivotTablePlugin as well. In the main thread, set notExecuteFormula: true on the Pro formula engine and the PivotTable plugin; in the Worker, set it to false. Both runtimes require the license. The pivot UI remains on the main thread, and custom formula implementations must be registered in both formula engines.
UniverLicensePlugin
licensestringClient license file contents.
UniverProFormulaEnginePlugin
notExecuteFormulabooleanSkip formula calculation in the current runtime when a Worker handles it.
UniverSheetsPivotTablePlugin
notExecuteFormulabooleanUsetrueon the main thread andfalsein the Worker. Omit it for single-threaded calculation.
Preset mode
UniverSheetsCorePreset
workerURLstring | URL | WorkerConnects the editor to a Worker for formula calculation.
Example: sheets-core-with-worker
This example includes the sheets-filter preset package because sheets-filter hides rows, and hidden rows affect how SUBTOTAL is calculated. When formula calculation runs in a Worker, sheets-filter must also run in the Worker.
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsFilterPreset } from '@univerjs/preset-sheets-filter'import UniverPresetSheetsFilterEnUS from '@univerjs/preset-sheets-filter/locales/en-US'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.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsFilterEnUS, ), }, presets: [ UniverSheetsCorePreset({ workerURL: new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' }), }), UniverSheetsFilterPreset(), ],})univerAPI.createWorkbook({ name: 'Test Sheet' })import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsCoreWorkerPreset } from '@univerjs/preset-sheets-core/worker'import UniverPresetSheetsFilterEnUS from '@univerjs/preset-sheets-filter/locales/en-US'import { UniverSheetsFilterWorkerPreset } from '@univerjs/preset-sheets-filter/worker'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsFilterEnUS, ), }, presets: [ UniverSheetsCoreWorkerPreset(), UniverSheetsFilterWorkerPreset(), ],})Example: sheets-collaboration-with-worker
UniverSheetsDrawingPreset
collaborationbooleanDefault:falseEnable collaborative image storage by using the collaboration service for image I/O.
UniverSheetsAdvancedPreset
useWorkerbooleanDefault:falseEnable the advanced calculation plugins in the Worker.licensestringClient license file contents.
UniverSheetsAdvancedWorkerPreset
licensestringClient license file contents.
import { UniverSheetsAdvancedPreset } from '@univerjs/preset-sheets-advanced'import UniverPresetSheetsAdvancedEnUS from '@univerjs/preset-sheets-advanced/locales/en-US'import { UniverSheetsCollaborationPreset } from '@univerjs/preset-sheets-collaboration'import UniverPresetSheetsCollaborationEnUS from '@univerjs/preset-sheets-collaboration/locales/en-US'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing'import UniverPresetSheetsDrawingEnUS from '@univerjs/preset-sheets-drawing/locales/en-US'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.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsDrawingEnUS, UniverPresetSheetsAdvancedEnUS, UniverPresetSheetsCollaborationEnUS, ), }, 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({ }), ],})import UniverPresetSheetsAdvancedEnUS from '@univerjs/preset-sheets-advanced/locales/en-US'import { UniverSheetsAdvancedWorkerPreset } from '@univerjs/preset-sheets-advanced/worker'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsCoreWorkerPreset } from '@univerjs/preset-sheets-core/worker'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsAdvancedEnUS, ), }, presets: [ UniverSheetsCoreWorkerPreset(), UniverSheetsAdvancedWorkerPreset({ license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt', }), ],})Footnotes
-
A Web Worker runs JavaScript outside the browser’s main thread and exchanges messages with it. It cannot access the page DOM directly; moving calculations into a Worker does not move the editor UI there. ↩
How is this guide?