指南Univer Sheets开始使用在 Node.js 上运行 Univer

在 Node.js 上运行 Univer

Univer 的同构特性使得它不仅可以在浏览器中运行,并且可以在 Node.js 上运行 🎉。你可以基于 Univer on Node 开发数据处理服务,在服务器端生成电子表格,或执行电子表格的公式、透视表等运算,并通过熟悉的 Facade API 和 Univer 交互,而这一切仅需要调整所加载的插件。

Presets

如果你使用 Presets 加载 Univer on Node,可参考如下代码:

import { createUniver, LocaleType } from '@univerjs/presets';
import { UniverSheetsNodeCorePreset } from '@univerjs/presets/preset-sheets-node-core';
import sheetsNodeCoreZhCN from '@univerjs/presets/preset-sheets-node-core/locales/zh-CN';
 
async function run(): Promise<void> {
  const { univerAPI } = createUniver({
    locale: LocaleType.ZH_CN,
      locales: {
        zhCN: Tools.deepMerge(
          {},
          sheetsNodeCoreZhCN,
        ),
      },
    presets: [
      UniverSheetsNodeCorePreset(),
    ],
  });
}
 
run();

高级安装方式

可参考如下代码启动 Univer on Node:

import { Univer, FUniver } from '@univerjs/core';
import { UniverDataValidationPlugin } from '@univerjs/data-validation';
import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsDrawingPlugin } from '@univerjs/docs-drawing';
import { UniverDrawingPlugin } from '@univerjs/drawing';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRPCNodeMainPlugin } from '@univerjs/rpc-node';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting';
import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation';
import { UniverSheetsDrawingPlugin } from '@univerjs/sheets-drawing';
import { UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
import { UniverSheetsHyperLinkPlugin } from '@univerjs/sheets-hyper-link';
import { UniverSheetsSortPlugin } from '@univerjs/sheets-sort';
import { UniverThreadCommentPlugin } from '@univerjs/thread-comment';
 
import '@univerjs/sheets/facade';
import '@univerjs/sheets-data-validation/facade';
import '@univerjs/engine-formula/facade';
import '@univerjs/sheets-filter/facade';
import '@univerjs/sheets-formula/facade';
 
export function createUniverOnNode(): Univer {
  const univer = new Univer();
 
  registerBasicPlugins(univer);
  registerSharedPlugins(univer);
  registerRPCPlugin(univer);
  registerDocPlugins(univer);
  registerSheetPlugins(univer);
 
  return univer;
}
 
function registerBasicPlugins(univer: Univer): void {
  univer.registerPlugin(UniverFormulaEnginePlugin);
}
 
function registerSharedPlugins(univer: Univer): void {
  univer.registerPlugin(UniverThreadCommentPlugin);
  univer.registerPlugin(UniverDrawingPlugin);
}
 
function registerDocPlugins(univer: Univer): void {
  univer.registerPlugin(UniverDocsPlugin);
  univer.registerPlugin(UniverDocsDrawingPlugin);
}
 
function registerSheetPlugins(univer: Univer): void {
  univer.registerPlugin(UniverSheetsPlugin);
  univer.registerPlugin(UniverSheetsFormulaPlugin);
  univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);
  univer.registerPlugin(UniverDataValidationPlugin);
  univer.registerPlugin(UniverSheetsDataValidationPlugin);
  univer.registerPlugin(UniverSheetsFilterPlugin);
  univer.registerPlugin(UniverSheetsHyperLinkPlugin);
  univer.registerPlugin(UniverSheetsDrawingPlugin);
  univer.registerPlugin(UniverSheetsSortPlugin);
}
 
async function run(): Promise<void> {
  const univerAPI = FUniver.newAPI(createUniverOnNode());
}
 
run();

可以看到,Univer on Node 基本上就是一个移除了和 UI 相关的插件的 Univer!