Web Workers
The following examples demonstrate the basic usage of Worker in Univer.
In Sheets, Workers are commonly used for compute-intensive tasks such as formula calculation. Running formula calculation in a Worker process can reduce pressure on the main thread and keep page rendering and interactions responsive when a workbook contains many formulas.
Preset Mode
workerURL option in UniverSheetsCorePreset configuration allows you to specify the URL of the Worker and enable Worker mode.
Example: sheets-core-with-worker
This example imports the sheets-filter preset package because the SUBTOTAL formula calculation is affected by hidden rows in sheets-filter. When formula calculations are executed in a Worker, sheets-filter also needs to be executed 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.js', 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
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.js', import.meta.url), { type: 'module' }),
}),
UniverSheetsDrawingPreset({
collaboration: true,
}),
UniverSheetsAdvancedPreset({
useWorker: true,
// If you want to use the unlimited commercial features, you can get a 30-day trial license from https://pro.univer.ai/license
license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',
}),
UniverSheetsCollaborationPreset({
universerEndpoint: 'http://localhost:3010',
}),
],
})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({
// If you want to use the unlimited commercial features, you can get a 30-day trial license from https://pro.univer.ai/license
license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',
}),
],
})For more Preset Mode examples, see dream-num/univer-presets examples. These examples show Worker configuration with different preset combinations and are useful references when integrating Univer through presets.
Plugin Mode
If you do not use presets and instead install and register @univerjs/* plugins manually, register the corresponding plugins in both the main process and the Worker process, and make sure the formula calculation plugins are registered on the Worker side. For complete Plugin Mode examples, see dream-num/univer examples.
How is this guide?
