Trabajadores web

Los ejemplos a continuación muestran cómo configurar un Worker1 para el cálculo de fórmulas en Univer.

En Hojas, un Trabajador suele manejar tareas de alta intensidad como el cálculo de fórmulas. Ejecutar este trabajo en un proceso de Trabajador reduce la carga en el hilo principal, por lo que la renderización de páginas y la interacción siguen siendo sensibles cuando un libro de trabajo contiene muchas fórmulas.

Modo plugin

Esta sección comienza desde la configuración del modo Plugin en instalación. Los plugins de Rendering y UI permanecen en el hilo principal. Reemplazar las inscripciones de los tres plugins de cálculo con las versiones siguientes, y registrar el plugin RPC principal antes de crear el libro de trabajo.

UniverRPCMainThreadPlugin

  • workerURL
    string | URL | Worker
    Conecta el editor con un Worker que calcula las fórmulas.

UniverFormulaEnginePlugin | UniverSheetsPlugin | UniverSheetsFormulaPlugin

  • notExecuteFormula
    booleanValor predeterminado: false
    Omite el cálculo de fórmulas en el hilo principal cuando se ejecuta en el 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 })

El Worker contiene sólo los datos y los plugins de cálculo que se enumeran a continuación. No registre UI o renderice plugins en el Worker. En el hilo principal, los delegados notExecuteFormula: true calculan fórmula al Trabajador.

UniverSheetsPlugin

  • onlyRegisterFormulaRelatedMutations
    booleanValor predeterminado: false
    Registra en el Worker solo los cambios necesarios para calcular fórmulas.
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.ES_ES })univer.registerPlugin(UniverSheetsPlugin, { onlyRegisterFormulaRelatedMutations: true })univer.registerPlugin(UniverFormulaEnginePlugin)univer.registerPlugin(UniverRPCWorkerThreadPlugin)univer.registerPlugin(UniverRemoteSheetsFormulaPlugin)

Filtrar requiere UniverSheetsFilterPlugin en ambos hilos. Sin este plugin, fórmulas como SUBTOTAL no pueden determinar qué filas se ha ocultado el filtrado. Importar el plugin de @univerjs/sheets-filter sobre el hilo principal y desde @univerjs/sheets-filter/worker del Worker. UniverSheetsFilterUIPlugin permanece en el hilo principal.

Para los plugins de cálculo adicionales en el Worker, utilice los puntos de entrada a continuación. Los principales hilos importan desde la entrada raíz de cada paquete. Todas las versiones de paquete SDK deben permanecer alineadas.

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'

Las tablas Pivot requieren una configuración diferente. Reemplazar UniverFormulaEnginePlugin con UniverProFormulaEnginePlugin en ambos hilos, y registrar UniverLicensePlugin y UniverSheetsPivotTablePlugin también. En el hilo principal, establecer notExecuteFormula: true del motor Pro fórmula y el plugin PivotTable; en el Worker, establecerlo en false.

UniverLicensePlugin

  • license
    string
    Contenido del archivo de licencia del cliente.

UniverProFormulaEnginePlugin

  • notExecuteFormula
    boolean
    Omite el cálculo de fórmulas en este entorno cuando se encarga el Worker.

UniverSheetsPivotTablePlugin

  • notExecuteFormula
    boolean
    Usa true en el hilo principal y false en el Worker. Omite la opción al calcular en un solo hilo.

Modo de preset

UniverSheetsCorePreset

  • workerURL
    string | URL | Worker
    Conecta el editor a un Trabajador para el cálculo de fórmulas.

Ejemplo: hojas-core-con-worker

Este ejemplo incluye el paquete de preset sheets-filter porque las hojas-filtro esconde filas, y las filas ocultas afectan cómo se calcula SUBTOTAL. Cuando el cálculo de la fórmula se ejecuta en un Worker, las hojas-películas también deben ejecutarse en el Worker.

TypeScript
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' })
TypeScript
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(),  ],})

Ejemplo: hoja-colaboración-con-trabajador

UniverSheetsDrawingPreset

  • collaboration
    booleanDefault: false
    Permite el almacenamiento de imágenes colaborativas utilizando el servicio de colaboración para la imagen I/O.

UniverSheetsAdvancedPreset

  • useWorker
    booleanDefault: false
    Habilitar los plugins de cálculo avanzados en el Worker.
  • license
    string
    Contenido del archivo de licencia de cliente.

UniverSheetsAdvancedWorkerPreset

  • license
    string
    Contenido del archivo de licencia de cliente.
TypeScript
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({    }),  ],})
TypeScript
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

  1. Un Web Worker ejecuta JavaScript fuera del hilo principal del navegador y se comunica mediante mensajes. No puede acceder directamente al DOM; mover los cálculos no mueve la interfaz del editor. ↩

¿Cómo es esta guía?

© 2026 DreamNum Co., Ltd.