Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(cf): fix the issue of duplicate values in conditional formatting (#6547)

wpxp1234568308b35fc642
diff --git a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/color-scale-calculate-unit.ts b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/color-scale-calculate-unit.ts
index 307f060f76..0e28882f97 100644
--- a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/color-scale-calculate-unit.ts
+++ b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/color-scale-calculate-unit.ts
@@ -82,9 +82,9 @@ export class ColorScaleCalculateUnit extends BaseCalculateUnit<IConfigItem[], st
if (!preComputingResult) {
return null;
}
- const value = context.getCellValue(row, col);
- if (value.t === CellValueType.NUMBER) {
- const v = Number(value.v);
+ const cell = context.getCellValue(row, col);
+ if (cell.t === CellValueType.NUMBER) {
+ const v = Number(cell.v);
if (!Number.isNaN(v)) {
const color = getColorScaleFromValue(preComputingResult, v);
return color;
diff --git a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts
index 3275dc01dd..8c432e6c6a 100644
--- a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts
+++ b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts
@@ -24,8 +24,6 @@ import { ConditionalFormattingFormulaService } from '../../services/conditional-
import { BaseCalculateUnit, CalculateEmitStatus } from './base-calculate-unit';
import { compareWithNumber, filterRange, getCellValue, isFloatsEqual, isNullable, serialTimeToTimestamp } from './utils';
-;
-
interface IConfig {
value: any;
type: CFSubRuleType;
@@ -44,8 +42,8 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
const cell = context.getCellValue(row, col);
- const v = getCellValue(cell || undefined);
- if (cell && cell.t === CellValueType.NUMBER && v !== undefined) {
+ const v = getCellValue(cell);
+ if (cell && cell.t === CellValueType.NUMBER && !isNullable(v)) {
sum += Number(v) || 0;
count++;
}
@@ -59,8 +57,8 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
const cell = context.getCellValue(row, col);
- const v = getCellValue(cell || undefined);
- if (v !== undefined) {
+ const v = getCellValue(cell);
+ if (!isNullable(v)) {
const cache = cacheMap.get(v);
if (cache) {
cacheMap.set(v, cache + 1);
@@ -77,8 +75,8 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
const cell = context.getCellValue(row, col);
- const v = getCellValue(cell || undefined);
- if (cell && cell.t === CellValueType.NUMBER && v !== undefined) {
+ const v = getCellValue(cell);
+ if (cell && cell.t === CellValueType.NUMBER && !isNullable(v)) {
allValue.push(Number(v) || 0);
}
});
@@ -248,8 +246,8 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
}
case CFSubRuleType.text: {
const subRuleConfig = ruleConfig as ITextHighlightCell;
- const value = getCellValue(cellValue!);
- const v = value === null ? '' : String(value);
+ const value = getCellValue(cellValue);
+ const v = isNullable(value) ? '' : String(value);
const condition = subRuleConfig.value || '';
switch (subRuleConfig.operator) {
case CFTextOperator.beginsWith: {
@@ -288,7 +286,7 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
}
}
case CFSubRuleType.timePeriod: {
- const value = getCellValue(cellValue!);
+ const value = getCellValue(cellValue);
if (isNullable(value) || Number.isNaN(Number(value)) || cellValue?.t !== CellValueType.NUMBER || !preComputingResult) {
return;
}
@@ -340,7 +338,7 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
}
}
case CFSubRuleType.rank: {
- const value = getCellValue(cellValue!);
+ const value = getCellValue(cellValue);
const v = Number(value);
@@ -357,7 +355,7 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
}
}
case CFSubRuleType.uniqueValues: {
- const value = getCellValue(cellValue!);
+ const value = getCellValue(cellValue);
if (isNullable(value) || !preComputingResult) {
return false;
@@ -366,13 +364,14 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf
return uniqueCache.get(value) === 1;
}
case CFSubRuleType.duplicateValues: {
- const value = getCellValue(cellValue!);
+ const value = getCellValue(cellValue);
if (isNullable(value) || !preComputingResult) {
return false;
}
- const uniqueCache = preComputingResult.value;
- return uniqueCache.get(value) !== 1;
+ const uniqueCacheValue = preComputingResult.value.get(value);
+
+ return uniqueCacheValue && uniqueCacheValue !== 1;
}
case CFSubRuleType.formula: {
// const _ruleConfig = ruleConfig as IFormulaHighlightCell;
diff --git a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/utils.ts b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/utils.ts
index a6a04915ba..18e0a3454f 100644
--- a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/utils.ts
+++ b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/utils.ts
@@ -28,7 +28,7 @@ import { ConditionalFormattingViewModel } from '../conditional-formatting-view-m
export function isFloatsEqual(a: number, b: number) {
return Math.abs(a - b) < Number.EPSILON;
}
-export const isNullable = (v: any) => [undefined, null].includes(v);
+export const isNullable = (v: Nullable<CellValue>) => v === null || v === undefined || (typeof v === 'string' && v.trim() === '');
export const getCellValue = (cell?: ICellData) => {
if (!cell) {
return null;