Tema de rango
Un tema de rango controla cómo se muestra una tabla. Univer proporciona un conjunto de temas preestablecidos, y también puede definir un tema propio.
Temas integrados
El tema predeterminado se llama default y sigue el Excel Table default style. Se aplica a través del atributo theme . Univer también proporciona un conjunto de temas de color. El Facade API a continuación enumera los temas que se soportan actualmente.
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']Configuración de temas
Un tema de gama se puede configurar de dos maneras: en la instantánea, o a través de la Facade API.
Configuración de temas por instantáneas
Univer almacena temas de gama en el recurso de instantáneas bajo la clave SHEET_RANGE_THEME_MODEL_PLUGIN. La estructura de datos es la siguiente:
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>}Configuración de temas a través de la API de Facade
El enfoque recomendado es establecer temas a través del Facade API y luego almacenar la información temática con los métodos de serialización y desserialización que proporciona Univer.
Las siguientes API cubren el flujo de trabajo temático:
// 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)Temas personalizados
Más allá de los temas incorporados, Univer también admite temas personalizados:
// 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')La siguiente llamada elimina un tema registrado:
const fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.unregisterRangeTheme('MyTheme')RangeThemeStyle Clase
Los temas de rango están construidos en la clase RangeThemeStyle, que expone las siguientes propiedades para la personalización:
name: el nombre del temawholeStyle: el estilo para toda la gamafirstRowStyle: el estilo para la primera filasecondRowStyle: el estilo para la segunda filaheaderRowStyle: el estilo para la fila de encabezadolastRowStyle: el estilo para la última filafirstColumnStyle: el estilo para la primera columnasecondColumnStyle: el estilo para la segunda columnaheaderColumnStyle: el estilo para la columna de encabezadolastColumnStyle: el estilo para la última columna
Cada estilo tiene un par de métodos getXXX/setXXX en la clase RangeThemeStyle, por ejemplo:
const rangeThemeStyle = new RangeThemeStyle('MyTheme')rangeThemeStyle.setSecondRowStyle({ bg: { rgb: 'rgb(214,231,241)', },})También puede pasar las opciones al 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)', }, },})Cuando se definen varios estilos, se aplican en el siguiente orden de prioridad:
lastRowStyle > headerRowStyle > lastColumnStyle > headerColumnStyle > secondRowStyle > firstRowStyle > secondColumnStyle > firstColumnStyle > wholeStyle
Cuando el estilo de una célula se encuentra en conflicto con el range theme, el estilo celular tiene precedencia. Un conflicto ocurre cuando ambos definen la propiedad del mismo estilo. Por ejemplo, si la célula establece su fondo a red y el range theme lo establece a blue, la célula permanece red.
¿Cómo es esta guía?