Worker Example
The following examples demonstrate the basic usage of Worker in Univer.
Presets Example
The workerURL
option in the configuration parameters of UniverSheetsCorePreset
can specify the URL address of the Worker and enable the Worker mode.
sheets-core-with-worker Example
The example here introduces the preset package of sheets-filter because the calculation of the formula SUBTOTAL is affected by the filtering of hidden rows in sheets-filter. When the formula calculation is performed in the Worker, sheets-filter also needs to be executed in the Worker.
// main.ts
import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsFilterPreset } from '@univerjs/presets/preset-sheets-filter';
import UniverPresetSheetsFilterEnUS from '@univerjs/presets/preset-sheets-filter/locales/en-US';
import '@univerjs/presets/lib/styles/preset-sheets-core.css'
import '@univerjs/presets/lib/styles/preset-sheets-filter.css'
const { univerAPI } = createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsFilterEnUS
),
},
theme: defaultTheme,
presets: [
UniverSheetsCorePreset({
workerURL: new Worker(new URL('./worker.js', import.meta.url), { type: 'module' }),
}),
UniverSheetsFilterPreset(),
],
});
univerAPI.createWorkbook({ name: 'Test Sheet' });
// worker.ts
import { createUniver, LocaleType } from '@univerjs/presets';
import { UniverSheetsCoreWorkerPreset } from '@univerjs/presets/preset-sheets-core/worker';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsFilterWorkerPreset } from '@univerjs/presets/preset-sheets-filter/worker';
import UniverPresetSheetsFilterEnUS from '@univerjs/presets/preset-sheets-filter/locales/en-US';
createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsFilterEnUS
),
},
presets: [
UniverSheetsCoreWorkerPreset(),
UniverSheetsFilterWorkerPreset(),
],
});
sheets-collaboration-with-worker Example
import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsAdvancedPreset } from '@univerjs/presets/preset-sheets-advanced';
import UniverPresetSheetsAdvancedEnUS from '@univerjs/presets/preset-sheets-advanced/locales/en-US';
import { UniverSheetsCollaborationPreset } from '@univerjs/presets/preset-sheets-collaboration';
import UniverPresetSheetsCollaborationEnUS from '@univerjs/presets/preset-sheets-collaboration/locales/en-US';
import { UniverSheetsDrawingPreset } from '@univerjs/presets/preset-sheets-drawing'
import UniverPresetSheetsDrawingEnUS from '@univerjs/presets/preset-sheets-drawing/locales/en-US'
import '@univerjs/presets/lib/styles/preset-sheets-core.css'
import '@univerjs/presets/lib/styles/preset-sheets-drawing.css'
import '@univerjs/presets/lib/styles/preset-sheets-advanced.css'
import '@univerjs/presets/lib/styles/preset-sheets-collaboration.css'
const { univerAPI } = createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsDrawingEnUS,
UniverPresetSheetsAdvancedEnUS,
UniverPresetSheetsCollaborationEnUS,
),
},
theme: defaultTheme,
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 no-limit business feature, you can get 30-day trial license from https://univer.ai/en-US/license
license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',
}),
UniverSheetsCollaborationPreset({
universerEndpoint: 'http://localhost:3010',
}),
],
});
import { createUniver, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCoreWorkerPreset } from '@univerjs/presets/preset-sheets-core/worker';
import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US';
import { UniverSheetsAdvancedWorkerPreset } from '@univerjs/presets/preset-sheets-advanced/worker';
import UniverPresetSheetsAdvancedEnUS from '@univerjs/presets/preset-sheets-advanced/locales/en-US';
createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
UniverPresetSheetsAdvancedEnUS
),
},
presets: [
UniverSheetsCoreWorkerPreset(),
UniverSheetsAdvancedWorkerPreset({
// if you want to use the no-limit business feature, you can get 30-day trial license from https://univer.ai/en-US/license
license: process.env.UNIVER_CLIENT_LICENSE || 'your license.txt',
}),
],
});
Piecemeal Combination of PLUGINS Example
You can refer to the example code in the Univer OSS version:
Last updated on