Installation & Basic Usage
Univer adopts a plugin-based design philosophy, aim`ing to provide developers with a flexible and extensible framework for electronic document applications. Through this plugin-based design, Univer can easily integrate various functional modules to meet the needs of different users. However, the plugin-based design increases the complexity of the application, especially for developers who are new to Univer.
For this reason, Univer provides two ways to help you quickly integrate and use Univer: plugin mode and preset mode. The so-called preset is actually a combination of pre-configured plugins, so the capabilities provided by both modes are the same without complex extensions.
Caution
- Consistent Dependency Versions: Whether you use preset mode or plugin mode, you must ensure that all dependencies have consistent version numbers.
- Cautious Mixing of Plugins and Presets: If a plugin is already included in a preset, you do not need to import that plugin separately when using the preset. Otherwise, it may lead to plugin conflicts or functional anomalies.
Differences between preset mode and plugin mode:
Plugin Mode | Preset Mode |
---|---|
Requires manual import of corresponding facade packages | No need to manually import any facade packages |
Requires attention to the registration order of plugins with the same functionality | No need to pay attention to the registration order of functionalities included in presets |
Supports on-demand lazy loading | Only supports preset-level lazy loading, plugins within the preset can only be loaded synchronously |
Setting Up via Preset Mode
Here we will briefly introduce how to quickly build a Univer Sheets application with less than twenty lines of code.
Using Package Manager
If your project already uses modern front-end development tools, integrating Univer will be very simple. We recommend using build tools like Vite, esbuild, or Webpack 5 that have good support for ES Modules to build Univer applications. If you are using other build tools (such as Webpack 4), you may need some additional configuration.
Installation
Choose your package manager to install @unvierjs/presets
:
npm install @univerjs/presets @univerjs/preset-docs-core
Usage
If your build tool does not support the exports
field in package.json (common in Webpack 4), you need to manually change the mapped paths to the actual paths.
With the following code, you can start an Univer Sheets application:
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'
import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'
import { createUniver, LocaleType, merge } from '@univerjs/presets'
import '@univerjs/preset-sheets-core/lib/index.css'
const { univer, univerAPI } = createUniver({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
UniverPresetSheetsCoreEnUS,
),
},
presets: [
UniverSheetsCorePreset({
container: 'app',
}),
],
})
univerAPI.createWorkbook({})
The univerAPI
object returned here is called the Univer Facade API, through which you can call many features provided by Univer.
You can also import UniverSheetsCorePreset
from @univerjs/presets/preset-sheets-core
, but you must ensure that your build tool supports the exports
field in package.json.
createUniver
Method
The createUniver
method accepts a configuration object that contains the configuration information for Univer, such as language, theme, and plugins. This method returns an object containing the Univer instance and the Univer Facade API instance.
Some properties of the configuration object are as follows:
locale
: Language environment, can be anLocaleType
enumeration value.locales
: Locales, an object, the key is the language environment, and the value is the language pack object.theme
: A theme object, which is optional.presets
: An array of presets that contains the presets to be registered, such asUniverSheetsCorePreset
.plugins
: An array of plugins that contains the plugins that need to be registered additionally.
When you use a plugin that is not included in any preset package or you implement your own plugin, you can register these plugins through the plugins
property. You can also choose to register plugins using the univer.registerPlugin
method after obtaining the Univer instance.
You can find more detailed information about the createUniver
method in the API Reference / createUniver.
Import from CDN
If you do not want to use a package manager or just want to quickly try out Univer's features, you can import the relevant resources of Univer via CDN. For details, please refer to Using Univer via CDN.
Setting Up via Plugin Mode
Univer provides a series of features in the form of plugins. In addition to some core plugins that are essential for the product, you can selectively introduce other plugins as needed. Here, we will take the most basic Univer Sheets application as an example to introduce how to manually combine and install plugins.
Different from preset mode, plugin mode does not include Facade API by default. The parts related to Facade API in the following code examples are optional, and you can decide whether to remove them based on your needs.
Using Package Manager
Installation
We use React to develop the view (this does not affect your use of Univer in Vue or Angular), and Rxjs to handle data streams. Since these two libraries are widely used in modern front-end development, we treat them as peerDependencies. However, different package management tools have different behaviors regarding peerDependencies, so you may need to pay attention to some details.
- If you are using npm, make sure you are using npm@7 or higher. This is because npm@3 ~ npm@6 will not correctly install
peerDependencies
1. - If you are using pnpm, make sure you are using pnpm@8 or higher. If you are using pnpm@6 ~ pnpm@7, you can try configuring
auto-install-peers=true
2 to resolve dependency installation issues. - If you are using yarn, you need to manually install the missing
peerDependencies
3, but don't worry, the installation commands below already include these dependencies.
npm install @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/sheets @univerjs/sheets-formula @univerjs/sheets-formula-ui @univerjs/sheets-numfmt @univerjs/sheets-numfmt-ui @univerjs/sheets-ui @univerjs/ui
pnpm add @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/sheets @univerjs/sheets-formula @univerjs/sheets-formula-ui @univerjs/sheets-numfmt @univerjs/sheets-numfmt-ui @univerjs/sheets-ui @univerjs/ui
yarn add @univerjs/core @univerjs/design @univerjs/docs @univerjs/docs-ui @univerjs/engine-formula @univerjs/engine-render @univerjs/sheets @univerjs/sheets-formula @univerjs/sheets-formula-ui @univerjs/sheets-numfmt @univerjs/sheets-numfmt-ui @univerjs/sheets-ui @univerjs/ui react react-dom rxjs
Usage
Caution
- Not all plugins include facade packages, language packs, and style files. We will specify this in the documentation for each feature.
- The order of importing style files is important. Make sure to import the CSS styles of
@univerjs/design
and@univerjs/ui
before importing the styles of other plugins.
You need to import the Univer style files, language packs, and some necessary plugins in your project:
import { LocaleType, merge, Univer, UniverInstanceType } from '@univerjs/core'
import { FUniver } from '@univerjs/core/facade'
import DesignEnUS from '@univerjs/design/locale/en-US'
import { UniverDocsPlugin } from '@univerjs/docs'
import { UniverDocsUIPlugin } from '@univerjs/docs-ui'
import DocsUIEnUS from '@univerjs/docs-ui/locale/en-US'
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'
import { UniverRenderEnginePlugin } from '@univerjs/engine-render'
import { UniverSheetsPlugin } from '@univerjs/sheets'
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula'
import { UniverSheetsFormulaUIPlugin } from '@univerjs/sheets-formula-ui'
import SheetsFormulaUIEnUS from '@univerjs/sheets-formula-ui/locale/en-US'
import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt'
import { UniverSheetsNumfmtUIPlugin } from '@univerjs/sheets-numfmt-ui'
import SheetsNumfmtUIEnUS from '@univerjs/sheets-numfmt-ui/locale/en-US'
import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui'
import SheetsUIEnUS from '@univerjs/sheets-ui/locale/en-US'
import SheetsEnUS from '@univerjs/sheets/locale/en-US'
import { UniverUIPlugin } from '@univerjs/ui'
import UIEnUS from '@univerjs/ui/locale/en-US'
// The Facade API here is optional, you can decide whether to import it according to your needs
import '@univerjs/engine-formula/facade'
import '@univerjs/ui/facade'
import '@univerjs/docs-ui/facade'
import '@univerjs/sheets/facade'
import '@univerjs/sheets-ui/facade'
import '@univerjs/sheets-formula/facade'
import '@univerjs/sheets-numfmt/facade'
import '@univerjs/design/lib/index.css'
import '@univerjs/ui/lib/index.css'
import '@univerjs/docs-ui/lib/index.css'
import '@univerjs/sheets-ui/lib/index.css'
import '@univerjs/sheets-formula-ui/lib/index.css'
import '@univerjs/sheets-numfmt-ui/lib/index.css'
Then create a Univer instance and register these plugins:
const univer = new Univer({
locale: LocaleType.EN_US,
locales: {
[LocaleType.EN_US]: merge(
{},
DesignEnUS,
UIEnUS,
DocsUIEnUS,
SheetsEnUS,
SheetsUIEnUS,
SheetsFormulaUIEnUS,
SheetsNumfmtUIEnUS,
),
},
})
univer.registerPlugin(UniverRenderEnginePlugin)
univer.registerPlugin(UniverFormulaEnginePlugin)
univer.registerPlugin(UniverUIPlugin, {
container: 'app',
})
univer.registerPlugin(UniverDocsPlugin)
univer.registerPlugin(UniverDocsUIPlugin)
univer.registerPlugin(UniverSheetsPlugin)
univer.registerPlugin(UniverSheetsUIPlugin)
univer.registerPlugin(UniverSheetsFormulaPlugin)
univer.registerPlugin(UniverSheetsFormulaUIPlugin)
univer.registerPlugin(UniverSheetsNumfmtPlugin)
univer.registerPlugin(UniverSheetsNumfmtUIPlugin)
univer.createUnit(UniverInstanceType.UNIVER_SHEET, {})
const univerAPI = FUniver.newAPI(univer)
Lazy Loading Plugins
A common advantage of plugin mode is that you can control the loading timing of plugins more flexibly. A common approach is to load only the essential plugins during application initialization, while deferring the loading of some plugins until after the first render is complete.
// ...
univer.createUnit(UniverInstanceType.UNIVER_SHEET, {})
import('@univerjs/watermark').then(({ UniverWatermarkPlugin }) => {
univer.registerPlugin(UniverWatermarkPlugin, {
textWatermarkSettings: {
content: 'Hello, Univer!',
fontSize: 36,
},
})
})
Our official demo uses this optimization technique, which you can refer to.
Import from CDN
If you do not want to use a package manager or just want to quickly try out Univer's features, you can import the relevant resources of Univer via CDN. For details, please refer to Using Univer via CDN.
Other Releases
In addition to stable releases, Univer offers alpha / beta and nightly channels. These versions allow you to get early access to the newest features that have not been officially released yet. However, keep in mind that more features also mean more risks. These versions may contain bugs, incomplete functionality, or unstable features. Please avoid using these versions in production environments whenever possible.
Alpha / Beta Release
When the Univer development team completes a new feature or significant change, they may pre-release it to the alpha or beta channel. You can install the alpha / beta version of a Univer package using the following command:
npm install @univerjs/<package-name>@alpha # Alpha version
npm install @univerjs/<package-name>@beta # Beta version
Nightly Releases
Nightly versions are automatically built versions of Univer, generated daily with the latest changes, bug fixes, and experimental features. These versions are highly unstable and not suitable for use in production environments. However, they are ideal for testing the latest features and ensuring compatibility with the most recent codebase.
npm install @univerjs/<package-name>@nightly
Warning
Nightly versions are extremely unstable and may contain fatal errors, incomplete features, or experimental functionality. Please absolutely DO NOT use these versions in production environments.
If you encounter any issues or have any feedback while using nightly versions, please report them to the Univer development team through appropriate channels, such as Github Issues or the Discord channels.
Happy early access experimentation!
Next Steps
- Basic Concepts - Learn about the basic concepts of Univer.
- Features - Learn about the features of Univer and how to manipulate data.
Footnotes
How is this guide?