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(conditional-formatting): ranking error (#3589)

Gpound.liuf0252ca6e914
diff --git a/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts b/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
index 67b753fbb4..91d10a9249 100644
--- a/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
+++ b/packages/sheets-conditional-formatting/src/services/calculate-unit/highlight-cell.ts
@@ -14,16 +14,18 @@
* limitations under the License.
*/
-import type { IStyleBase } from '@univerjs/core';
+/* eslint-disable max-lines-per-function */
+
import { CellValueType, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
-import dayjs from 'dayjs';
import { deserializeRangeWithSheet, ERROR_TYPE_SET, generateStringWithSequence, LexerTreeBuilder, sequenceNodeType, serializeRange } from '@univerjs/engine-formula';
+import dayjs from 'dayjs';
+import type { IStyleBase } from '@univerjs/core';
import { CFNumberOperator, CFRuleType, CFSubRuleType, CFTextOperator, CFTimePeriodOperator } from '../../base/const';
-import type { IAverageHighlightCell, IConditionFormattingRule, IFormulaHighlightCell, IHighlightCell, INumberHighlightCell, IRankHighlightCell, ITextHighlightCell, ITimePeriodHighlightCell } from '../../models/type';
import { ConditionalFormattingFormulaService, FormulaResultStatus } from '../conditional-formatting-formula.service';
+import { EMPTY_STYLE } from './type';
import { compareWithNumber, filterRange, getCellValue, isFloatsEqual, isNullable, serialTimeToTimestamp } from './utils';
+import type { IAverageHighlightCell, IConditionFormattingRule, IFormulaHighlightCell, IHighlightCell, INumberHighlightCell, IRankHighlightCell, ITextHighlightCell, ITimePeriodHighlightCell } from '../../models/type';
import type { ICalculateUnit } from './type';
-import { EMPTY_STYLE } from './type';
export const highlightCellCalculateUnit: ICalculateUnit = {
type: CFRuleType.highlightCell,
@@ -82,10 +84,10 @@ export const highlightCellCalculateUnit: ICalculateUnit = {
allValue.sort((a, b) => b - a);
const configRule = rule.rule as IRankHighlightCell;
const targetIndex = configRule.isPercent
- ? Math.round(Math.max(Math.min(configRule.value, 100), 0) / 100 * allValue.length)
- : Math.round(Math.max(Math.min(configRule.value, allValue.length), 0));
+ ? Math.floor(Math.max(Math.min(configRule.value, 100), 0) / 100 * allValue.length)
+ : Math.floor(Math.max(Math.min(configRule.isBottom ? (configRule.value - 1) : configRule.value, allValue.length), 0));
if (configRule.isBottom) {
- return { rank: allValue[allValue.length - targetIndex] };
+ return { rank: allValue[allValue.length - targetIndex - 1] };
} else {
return { rank: allValue[Math.max(targetIndex - 1, 0)] };
}
@@ -106,6 +108,8 @@ export const highlightCellCalculateUnit: ICalculateUnit = {
}
};
const cache = getCache();
+
+ // eslint-disable-next-line complexity
const check = (row: number, col: number) => {
const cellValue = worksheet?.getCellRaw(row, col);
switch (ruleConfig.subType) {
diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
index 0ef8aec645..b5cba6c2de 100644
--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting.service.ts
@@ -15,23 +15,23 @@
*/
import type { IMutationInfo, IRange, Workbook } from '@univerjs/core';
-import { createInterceptorKey, Disposable, ICommandService, Inject, Injector, InterceptorManager, IResourceManagerService, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
import type { IInsertColMutationParams, IMoveColumnsMutationParams, IMoveRangeMutationParams, IMoveRowsMutationParams, IRemoveRowsMutationParams, IRemoveSheetCommandParams, IReorderRangeMutationParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
+import type { IDeleteConditionalRuleMutationParams } from '../commands/mutations/delete-conditional-rule.mutation';
+import type { IConditionFormattingRule, IHighlightCell, IRuleModelJson } from '../models/type';
+import type { IDataBarCellData, IDataBarRenderParams, IIconSetCellData, IIconSetRenderParams } from '../render/type';
+import type { ICalculateUnit, IContext } from './calculate-unit/type';
+import { createInterceptorKey, Disposable, ICommandService, Inject, Injector, InterceptorManager, IResourceManagerService, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, Rectangle, Tools, UniverInstanceType } from '@univerjs/core';
import { InsertColMutation, InsertRowMutation, MoveColsMutation, MoveRangeMutation, MoveRowsMutation, RemoveColMutation, RemoveRowMutation, RemoveSheetCommand, ReorderRangeMutation, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';
import { Subject } from 'rxjs';
import { bufferTime, filter, map } from 'rxjs/operators';
-import type { IDeleteConditionalRuleMutationParams } from '../commands/mutations/delete-conditional-rule.mutation';
+import { CFRuleType, SHEET_CONDITIONAL_FORMATTING_PLUGIN } from '../base/const';
import { DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory } from '../commands/mutations/delete-conditional-rule.mutation';
import { ConditionalFormattingRuleModel } from '../models/conditional-formatting-rule-model';
import { ConditionalFormattingViewModel } from '../models/conditional-formatting-view-model';
-import { CFRuleType, SHEET_CONDITIONAL_FORMATTING_PLUGIN } from '../base/const';
-import type { IConditionFormattingRule, IHighlightCell, IRuleModelJson } from '../models/type';
-import type { IDataBarCellData, IDataBarRenderParams, IIconSetCellData, IIconSetRenderParams } from '../render/type';
+import { colorScaleCellCalculateUnit } from './calculate-unit/color-scale';
import { dataBarCellCalculateUnit } from './calculate-unit/data-bar';
import { highlightCellCalculateUnit } from './calculate-unit/highlight-cell';
-import { colorScaleCellCalculateUnit } from './calculate-unit/color-scale';
import { iconSetCalculateUnit } from './calculate-unit/icon-set';
-import type { ICalculateUnit, IContext } from './calculate-unit/type';
import { EMPTY_STYLE } from './calculate-unit/type';
type ComputeStatus = 'computing' | 'end' | 'error';
@@ -72,7 +72,7 @@ export class ConditionalFormattingService extends Disposable {
this._registerCalculationUnit(colorScaleCellCalculateUnit);
this._registerCalculationUnit(highlightCellCalculateUnit);
this._registerCalculationUnit(iconSetCalculateUnit);
- this._calculateUnit$.pipe(bufferTime(16), filter((list) => !!list.length), map((list) => {
+ this._calculateUnit$.pipe(bufferTime(100), filter((list) => !!list.length), map((list) => {
const createKey = (config: typeof list[0]) => `${config.unitId}_${config.subUnitId}_${config.rule.cfId}`;
const result = list.reduce((a, b) => {
const key = createKey(b);