Range Theme
A range theme controls how a table is displayed. Univer provides a set of preset themes, and you can also define a theme of your own.
Built-in themes
The default theme is named default and follows the Excel Table default style. You apply it through the theme attribute. Univer also provides a set of color themes. The Facade API below lists the themes that are currently supported.
const fWorkbook = univerAPI.getActiveWorkbook()const themes = fWorkbook.getRegisteredRangeThemes()console.log(themes) // ['default', 'light-blue', 'light-grey', 'light-red', 'light-orange', 'light-yellow', 'light-green', 'light-azure', 'light-indigo', 'light-purple', 'light-magenta', 'middle-blue', 'middle-grey', 'middle-red', 'middle-orange', 'middle-yellow', 'middle-green', 'middle-azure', 'middle-indigo', 'middle-purple', 'middle-magenta', 'dark-blue', 'dark-grey', 'dark-red', 'dark-orange', 'dark-yellow', 'dark-green', 'dark-azure', 'dark-indigo', 'dark-purple', 'dark-magenta']Setting themes
A range theme can be set in two ways: in the snapshot, or through the Facade API.
Setting themes by snapshots
Univer stores range themes in the snapshot resource under the key SHEET_RANGE_THEME_MODEL_PLUGIN. The data structure is as follows:
interface IRangeThemeRangeInfo { range: IRange unitId: string subUnitId: string}interface IRangeThemeStyleRule { rangeInfo: IRangeThemeRangeInfo themeName: string}interface ISheetRangeThemeModelJSON { // JSON storing the range and theme information for setting themes, the key is the id, please ensure it is not repeated, the value is the corresponding range information and template name. rangeThemeStyleRuleMap: Record<string, IRangeThemeStyleRule> // JSON storing the themes, the key is the theme name, and the value is the serialized JSON of the theme. rangeThemeStyleMapJson: Record<string, IRangeThemeStyleJSON>}Setting themes through Facade API
The recommended approach is to set themes through the Facade API and then store the theme information with the serialization and deserialization methods that Univer provides.
The following APIs cover the theme workflow:
// Get the currently supported themesconst fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const themes = fWorkbook.getRegisteredRangeThemes()// Set the default theme for A1:E20const fRange = fWorksheet.getRange('A1:E20')fRange.useThemeStyle('default') // 使用默认主题// Get the currently used themeconst currentTheme = fRange.getUsedThemeStyle()// Clear the theme stylefRange.useThemeStyle(undefined)fRange.removeThemeStyle(currentTheme)Custom themes
Beyond the built-in themes, Univer also supports custom themes:
// Create a custom themeconst fWorkbook = univerAPI.getActiveWorkbook()const rangeThemeStyle = fWorkbook.createRangeThemeStyle('MyTheme', { secondRowStyle: { bg: { rgb: 'rgb(214,231,241)', }, },})fWorkbook.registerRangeTheme(rangeThemeStyle)// Use the custom theme for A1:E20const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:E20')// Make sure the theme is registered, otherwise it will throw an error.fRange.useThemeStyle('MyTheme')The following call removes a registered theme:
const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.unregisterRangeTheme('MyTheme')RangeThemeStyle class
Range themes are built on the RangeThemeStyle class, which exposes the following properties for customization:
name: the theme namewholeStyle: the style for the entire rangefirstRowStyle: the style for the first rowsecondRowStyle: the style for the second rowheaderRowStyle: the style for the header rowlastRowStyle: the style for the last rowfirstColumnStyle: the style for the first columnsecondColumnStyle: the style for the second columnheaderColumnStyle: the style for the header columnlastColumnStyle: the style for the last column
Each style has a matching pair of getXXX/setXXX methods on the RangeThemeStyle class, for example:
const rangeThemeStyle = new RangeThemeStyle('MyTheme')rangeThemeStyle.setSecondRowStyle({ bg: { rgb: 'rgb(214,231,241)', },})You can also pass the options to the constructor:
/** * @constructor * @param {string} name The name of the range theme style, it used to identify the range theme style. * @param {IRangeThemeStyleJSON} [options] The options to initialize the range theme style. */class RangeThemeStyle {};// For example:const rangeThemeStyle = new RangeThemeStyle('MyTheme', { secondRowStyle: { bg: { rgb: 'rgb(214,231,241)', }, },})When several styles are defined, they apply in the following order of priority:
lastRowStyle > headerRowStyle > lastColumnStyle > headerColumnStyle > secondRowStyle > firstRowStyle > secondColumnStyle > firstColumnStyle > wholeStyle
When a cell's own style conflicts with the range theme, the cell style takes precedence. A conflict occurs when both define the same style property. For example, if the cell sets its background to red and the range theme sets it to blue, the cell remains red.
How is this guide?