Skip to Content
🎉 Univer 0.6.7 is released.Read more →
GuidesUniver SheetsFeaturesConditional Formatting

Conditional Formatting

Facade APIHas Paid PlanUniver ServerUniver on Node.jsPreset
--UniverSheetsConditionalFormattingPreset

Conditional formatting is a feature that sets rules in cells to automatically set cell formats based on the value of the data.

This feature includes the following plugin packages:

Presets Installation

import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets'; import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core'; import UniverPresetSheetsCoreEnUS from '@univerjs/presets/preset-sheets-core/locales/en-US'; import { UniverSheetsConditionalFormattingPreset } from '@univerjs/presets/preset-sheets-conditional-formatting'; import UniverPresetSheetsConditionalFormattingEnUS from '@univerjs/presets/preset-sheets-conditional-formatting/locales/en-US'; import '@univerjs/presets/lib/styles/preset-sheets-core.css' import '@univerjs/presets/lib/styles/preset-sheets-conditional-formatting.css' const { univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: merge( {}, UniverPresetSheetsCoreEnUS, UniverPresetSheetsConditionalFormattingEnUS ), }, theme: defaultTheme, presets: [ UniverSheetsCorePreset(), UniverSheetsConditionalFormattingPreset(), ], });

Piecemeal Installation

npm install @univerjs/sheets-conditional-formatting @univerjs/sheets-conditional-formatting-ui
import { LocaleType, merge, Univer } from '@univerjs/core'; import { defaultTheme } from "@univerjs/design"; import { UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting'; import { UniverSheetsConditionalFormattingUIPlugin } from '@univerjs/sheets-conditional-formatting-ui'; import SheetsConditionalFormattingUIEnUS from '@univerjs/sheets-conditional-formatting-ui/locale/en-US'; import '@univerjs/sheets-conditional-formatting-ui/lib/index.css'; import '@univerjs/sheets-conditional-formatting/facade'; const univer = new Univer({ theme: defaultTheme, locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: merge( SheetsConditionalFormattingUIEnUS ), }, }); univer.registerPlugin(UniverSheetsConditionalFormattingPlugin); univer.registerPlugin(UniverSheetsConditionalFormattingUIPlugin);

Univer on Node.js Piecemeal Installation

npm install @univerjs/sheets-conditional-formatting
import { LocaleType, Univer } from '@univerjs/core'; import { defaultTheme } from "@univerjs/design"; import { UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting'; import '@univerjs/sheets-conditional-formatting/facade'; const univer = new Univer({ theme: defaultTheme, locale: LocaleType.EN_US, }); univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);

Facade API

To get full defination of facade api, please refer to FacadeAPI

Create a Conditional Formatting Rule

FWorksheet.newConditionalFormattingRule() creates a conditional formatting builder that returns an instance of FConditionalFormattingBuilder that can be used to create conditional formatting rules through chain calls.

Here are some member methods on FConditionalFormattingBuilder:

MethodDescription
buildBuilds the conditional formatting rule
setRangesSets the range of the conditional formatting rule
setAverageSets the average value conditional formatting rule
setUniqueValuesSets the unique value conditional formatting rule
setDuplicateValuesSets the duplicate value conditional formatting rule
setRankSets the ranking conditional formatting rule
setIconSetSets the icon set conditional formatting rule
setColorScaleSets the color scale conditional formatting rule
setDataBarSets the data bar conditional formatting rule
setFontColorSets the font color of the conditional format
setItalicSets whether the font of the conditional format is italic
setBackgroundSets the background color of the conditional format
setBoldSets whether the font of the conditional format is bold
setStrikethroughSets whether the font of the conditional format has a strikethrough
setUnderlineSets whether the font of the conditional format has an underline
whenCellEmptySets the conditional format rule to trigger when the cell is empty
whenCellNotEmptySets the conditional format rule to trigger when the cell is not empty
whenDateSets the conditional format rule to trigger when the date is within a specific time period
whenFormulaSatisfiedSets the conditional format rule to trigger when the formula calculation result is true
whenNumberBetweenSets the conditional format rule to trigger when the number is between two specified values or equal to one of them
whenNumberEqualToSets the conditional format rule to trigger when the number is equal to the given value
whenNumberGreaterThanSets the conditional format rule to trigger when the number is greater than the given value
whenNumberGreaterThanOrEqualToSets the conditional format rule to trigger when the number is greater than or equal to the given value
whenNumberLessThanSets the conditional format rule to trigger when the number is less than the given value
whenNumberLessThanOrEqualToSets the conditional format rule to trigger when the number is less than or equal to the given value
whenNumberNotBetweenSets the conditional format rule to trigger when the number is not between two specified values and not equal to them
whenNumberNotEqualToSets the conditional format rule to trigger when the number is not equal to the given value
whenTextContainsSets the conditional format rule to trigger when the text contains the specified value
whenTextDoesNotContainSets the conditional format rule to trigger when the text does not contain the specified value
whenTextEndsWithSets the conditional format rule to trigger when the text ends with the specified value
whenTextEqualToSets the conditional format rule to trigger when the text equals the given value
whenTextStartsWithSets the conditional format rule to trigger when the text starts with the specified value
const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); // Create a conditional formatting rule that sets the cell format to italic, red background, and green font color when the cell is not empty. const fRange = fWorksheet.getRange('A1:T100'); const rule = fWorksheet.newConditionalFormattingRule() .whenCellNotEmpty() .setRanges([fRange.getRange()]) .setItalic(true) .setBackground('red') .setFontColor('green') .build(); fWorksheet.addConditionalFormattingRule(rule);

Get Conditional Formatting Rules

  • FWorksheet.getConditionalFormattingRules() gets all conditional formatting rules of the worksheet
  • FRange.getConditionalFormattingRules() gets all conditional formatting rules of the range
const fWorkbook = univerAPI.getActiveWorkbook(); // Get all conditional formatting rules of the active sheet const fWorksheet = fWorkbook.getActiveSheet(); const rulesOfSheet = fWorksheet.getConditionalFormattingRules(); console.log(rulesOfSheet); // Get all conditional formatting rules of the range A1:T100 const fRange = fWorksheet.getRange('A1:T100'); const rulesOfRange = fRange.getConditionalFormattingRules(); console.log(rulesOfRange);

Delete Conditional Formatting Rule

FWorksheet.deleteConditionalFormattingRule(cfId) method deletes the conditional formatting rule of the worksheet, where cfId is the id of the conditional formatting rule.

const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const rules = fWorksheet.getConditionalFormattingRules(); // Delete the first rule fWorksheet.deleteConditionalFormattingRule(rules[0]?.cfId);

Update Conditional Formatting Rule

FWorksheet.setConditionalFormattingRule(cfId, rule) 方法可以更新工作表的条件格式规则,cfId 为条件格式规则的 id,rule 为新的条件格式规则。

const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); // Create a conditional formatting rule that sets the cell format to italic, red background, and green font color when the cell is not empty. const fRange = fWorksheet.getRange('A1:T100'); const rule = fWorksheet.newConditionalFormattingRule() .whenCellNotEmpty() .setRanges([fRange.getRange()]) .setItalic(true) .setBackground('red') .setFontColor('green') .build(); fWorksheet.addConditionalFormattingRule(rule); // Modify the first rule to apply to a new range const rules = fWorksheet.getConditionalFormattingRules(); const newRuleRange = fWorksheet.getRange('A1:D10'); fWorksheet.setConditionalFormattingRule(rules[0]?.cfId, { ...rules[0], ranges: [newRuleRange.getRange()] });

Modify the Priority of Conditional Formatting Rule

FWorksheet.moveConditionalFormattingRule(cfId, toCfId, type) method can modify the priority of the conditional formatting rule of the worksheet, where cfId is the id of the conditional formatting rule, toCfId is the id of the target conditional formatting rule, and type is the move type.

const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const rules = fWorksheet.getConditionalFormattingRules(); // Move the third rule before the first rule const rule = rules[2]; const targetRule = rules[0]; fWorksheet.moveConditionalFormattingRule(rule?.cfId, targetRule?.cfId, 'before');

Clear All Conditional Formatting Rules

  • FWorksheet.clearConditionalFormatRules() clear all conditional formatting rules of the worksheet
  • FRange.clearConditionalFormatRules() clear all conditional formatting rules of the range
const fWorkbook = univerAPI.getActiveWorkbook(); // Clear all conditional formatting rules of the active sheet const fWorksheet = fWorkbook.getActiveSheet(); fWorksheet.clearConditionalFormatRules(); // Clear all conditional formatting rules of the range A1:T100 const fRange = fWorksheet.getRange('A1:T100'); fRange.clearConditionalFormatRules();

Was this page helpful?
Last updated on