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): the maximum and minimum values are corre… (#3270)

Gpound.liu5d41a9bba15c
diff --git a/packages/sheets-conditional-formatting/src/services/__test__/calculate.databar.spec.ts b/packages/sheets-conditional-formatting/src/services/__test__/calculate.databar.spec.ts
new file mode 100644
index 0000000000..40d7217b9a
--- /dev/null
+++ b/packages/sheets-conditional-formatting/src/services/__test__/calculate.databar.spec.ts
@@ -0,0 +1,91 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { beforeEach, describe, expect, it } from 'vitest';
+import { Range } from '@univerjs/core';
+import type { IConditionFormattingRule, IDataBar } from '../../models/type';
+import { CFRuleType, CFValueType } from '../../base/const';
+import { createTestBed } from './test.util';
+
+describe('Test conditional formatting data bar', () => {
+ let testBed: ReturnType<typeof createTestBed>;
+
+ beforeEach(() => {
+ testBed && testBed.univer.dispose();
+ testBed = createTestBed();
+ (testBed.getConditionalFormattingService() as any)._calculateUnit$.subscribe((config: any) => {
+ (testBed.getConditionalFormattingService() as any)._handleCalculateUnit(config.unitId, config.subUnitId, config.rule);
+ });
+ (testBed.getConditionalFormattingRuleModel() as any)._ruleChange$.subscribe((config: any) => {
+ config.rule.ranges.forEach((range: any) => {
+ const viewModel = testBed.getConditionalFormattingViewModel();
+ Range.foreach(range, (row, col) => {
+ viewModel.pushCellCf(config.unitId, config.subUnitId, row, col, config.rule.cfId);
+ });
+ });
+ });
+ });
+
+ it('The maximum and minimum values of the data bar are corrected automatically when minimum is greater than maximum.', () => {
+ const params: IConditionFormattingRule<IDataBar> = {
+ ranges: [{ startRow: 0, startColumn: 0, endRow: 0, endColumn: 6 }],
+ cfId: testBed.getConditionalFormattingRuleModel().createCfId(testBed.unitId, testBed.subUnitId),
+ stopIfTrue: false,
+ rule: {
+ type: CFRuleType.dataBar,
+ isShowValue: false,
+ config: {
+ // The minimum is greater than the maximum
+ min: { type: CFValueType.num, value: 7 },
+ max: { type: CFValueType.num, value: 1 },
+ isGradient: false,
+ positiveColor: '#abd91a',
+ nativeColor: '#ffbe38',
+ },
+ },
+ };
+ testBed.getConditionalFormattingRuleModel().addRule(testBed.unitId, testBed.subUnitId, params);
+
+ const dispose = testBed.getConditionalFormattingService().ruleComputeStatus$.subscribe(() => {
+ dispose.unsubscribe();
+ const start = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 0);
+ const end = testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 6);
+
+ expect(start).toEqual({
+ dataBar: {
+ color: '#abd91a',
+ startPoint: 0,
+ value: 0,
+ isGradient: false,
+ isShowValue: false,
+ },
+ isShowValue: false,
+ });
+
+ expect(end).toEqual({
+ dataBar: {
+ color: '#abd91a',
+ startPoint: 0,
+ value: 100,
+ isGradient: false,
+ isShowValue: false,
+ },
+ isShowValue: false,
+ });
+ });
+ testBed.getConditionalFormattingService().composeStyle(testBed.unitId, testBed.subUnitId, 0, 1);
+ });
+});
diff --git a/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts b/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
index 30522d1f2f..14050d364b 100644
--- a/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
+++ b/packages/sheets-conditional-formatting/src/services/__test__/cf.service.spec.ts
@@ -15,6 +15,7 @@
*/
import { beforeEach, describe, expect, it } from 'vitest';
+import { Range } from '@univerjs/core';
import type { IAverageHighlightCell, IColorScale, IConditionFormattingRule, IDataBar, INumberHighlightCell, IRankHighlightCell, ITextHighlightCell } from '../../models/type';
import { CFNumberOperator, CFRuleType, CFSubRuleType, CFTextOperator, CFValueType } from '../../base/const';
import { EMPTY_STYLE } from '../calculate-unit/type';
@@ -29,6 +30,14 @@ describe('Test conditional formatting service', () => {
(testBed.getConditionalFormattingService() as any)._calculateUnit$.subscribe((config: any) => {
(testBed.getConditionalFormattingService() as any)._handleCalculateUnit(config.unitId, config.subUnitId, config.rule);
});
+ (testBed.getConditionalFormattingRuleModel() as any)._ruleChange$.subscribe((config: any) => {
+ config.rule.ranges.forEach((range: any) => {
+ const viewModel = testBed.getConditionalFormattingViewModel();
+ Range.foreach(range, (row, col) => {
+ viewModel.pushCellCf(config.unitId, config.subUnitId, row, col, config.rule.cfId);
+ });
+ });
+ });
});
describe('Test highlight', () => {
diff --git a/packages/sheets-conditional-formatting/src/services/calculate-unit/data-bar.ts b/packages/sheets-conditional-formatting/src/services/calculate-unit/data-bar.ts
index 14ff70b1ad..be71690e62 100644
--- a/packages/sheets-conditional-formatting/src/services/calculate-unit/data-bar.ts
+++ b/packages/sheets-conditional-formatting/src/services/calculate-unit/data-bar.ts
@@ -26,6 +26,7 @@ import { EMPTY_STYLE } from './type';
export const dataBarCellCalculateUnit: ICalculateUnit = {
type: CFRuleType.dataBar,
+ // eslint-disable-next-line max-lines-per-function
handle: async (rule: IConditionFormattingRule, context) => {
const ruleConfig = rule.rule as IDataBar;
const conditionalFormattingFormulaService = context.accessor.get(ConditionalFormattingFormulaService);
@@ -69,8 +70,9 @@ export const dataBarCellCalculateUnit: ICalculateUnit = {
if (_max.status === FormulaResultStatus.WAIT) {
return conditionalFormattingFormulaService.getCache(context.unitId, context.subUnitId, rule.cfId) || computeResult;
} else if (_max.status === FormulaResultStatus.SUCCESS) {
- const v = Number(_max.result);
- max = Number.isNaN(v) ? 0 : v;
+ const v = Number.isNaN(Number(_max.result)) ? 0 : Number(_max.result);
+ max = Math.max(v, min);
+ min = Math.min(v, min);
} else {
return computeResult;
}