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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheets-data-validation): optimize data-validation error msg (#4044)
zhangwb4d4cfa063c9
diff --git a/packages/data-validation/src/validators/base-data-validator.ts b/packages/data-validation/src/validators/base-data-validator.tsindex 216516dc67..39b47b757d 100644--- a/packages/data-validation/src/validators/base-data-validator.ts+++ b/packages/data-validation/src/validators/base-data-validator.ts@@ -16,6 +16,7 @@import type { CellValue, IDataValidationRule, IDataValidationRuleBase, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';import type { CellValueType } from '@univerjs/protocol';+import type { ISheetLocationBase } from '@univerjs/sheets';import type { IBaseDataValidationWidget } from './base-widget';import { DataValidationOperator, Inject, Injector, LocaleService, Tools } from '@univerjs/core';import { OperatorErrorTitleMap, OperatorTitleMap } from '../types/const/operator-text-map';@@ -99,7 +100,7 @@ export abstract class BaseDataValidator<DataType = CellValue> {return `${this.titleStr} ${ruleName}`;}- generateRuleErrorMessage(rule: IDataValidationRuleBase) {+ generateRuleErrorMessage(rule: IDataValidationRuleBase, position: ISheetLocationBase) {if (!rule.operator) {return this.titleStr;}@@ -110,12 +111,12 @@ export abstract class BaseDataValidator<DataType = CellValue> {getExtraStyle(rule: IDataValidationRuleBase, value: Nullable<CellValue>, ctx: { style: IStyleData }, row: number, column: number): Nullable<IStyleData> {}- getRuleFinalError(rule: IDataValidationRule) {+ getRuleFinalError(rule: IDataValidationRule, position: ISheetLocationBase) {if (rule.showErrorMessage && rule.error) {return rule.error;}- return this.generateRuleErrorMessage(rule);+ return this.generateRuleErrorMessage(rule, position);}isEmptyCellValue(cellValue: Nullable<CellValue>): cellValue is null | undefined | void {diff --git a/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts b/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.tsindex bf07a91249..630b60816b 100644--- a/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts+++ b/packages/sheets-data-validation-ui/src/controllers/dv-alert.controller.ts@@ -69,7 +69,7 @@ export class DataValidationAlertController extends Disposable {this._cellAlertManagerService.showAlert({type: CellAlertType.ERROR,title: this._localeService.t('dataValidation.error.title'),- message: validator?.getRuleFinalError(rule),+ message: validator?.getRuleFinalError(rule, cellPos.location),location: cellPos.location,width: 200,height: 74,diff --git a/packages/sheets-data-validation-ui/src/controllers/dv-reject-input.controller.ts b/packages/sheets-data-validation-ui/src/controllers/dv-reject-input.controller.tsindex 05b4fd7a96..506706a5bb 100644--- a/packages/sheets-data-validation-ui/src/controllers/dv-reject-input.controller.ts+++ b/packages/sheets-data-validation-ui/src/controllers/dv-reject-input.controller.ts@@ -78,7 +78,7 @@ export class DataValidationRejectInputController extends Disposable {},id: 'reject-input-dialog',children: {- title: validator.getRuleFinalError(rule),+ title: validator.getRuleFinalError(rule, { row, col, unitId, subUnitId }),},footer: {title: React.createElement(diff --git a/packages/sheets-data-validation-ui/src/views/components/date-dropdown/index.tsx b/packages/sheets-data-validation-ui/src/views/components/date-dropdown/index.tsxindex 589a21413d..f443f0cf8e 100644--- a/packages/sheets-data-validation-ui/src/views/components/date-dropdown/index.tsx+++ b/packages/sheets-data-validation-ui/src/views/components/date-dropdown/index.tsx@@ -126,7 +126,7 @@ export function DateDropdown(props: IDropdownComponentProps) {},});} else {- rejectInputController.showReject(validator.getRuleFinalError(rule));+ rejectInputController.showReject(validator.getRuleFinalError(rule, { row, col, unitId, subUnitId }));}};diff --git a/packages/sheets-data-validation/src/controllers/dv-formula-ref-range.controller.ts b/packages/sheets-data-validation/src/controllers/dv-formula-ref-range.controller.tsindex dee5c5bb45..f8edf5552d 100644--- a/packages/sheets-data-validation/src/controllers/dv-formula-ref-range.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-formula-ref-range.controller.ts@@ -18,7 +18,6 @@ import type { IDisposable, IMutationInfo, ISheetDataValidationRule } from '@univimport type { IUpdateDataValidationMutationParams } from '@univerjs/data-validation';import { Disposable, generateRandomId, Inject, toDisposable } from '@univerjs/core';import { AddDataValidationMutation, RemoveDataValidationMutation, UpdateDataValidationMutation, UpdateRuleType } from '@univerjs/data-validation';-import { LexerTreeBuilder } from '@univerjs/engine-formula';import { FormulaRefRangeService } from '@univerjs/sheets-formula';import { SheetDataValidationModel } from '../models/sheet-data-validation-model';import { isCustomFormulaType } from '../utils/formula';@@ -28,8 +27,7 @@ export class DataValidationFormulaRefRangeController extends Disposable {constructor(@Inject(SheetDataValidationModel) private _dataValidationModel: SheetDataValidationModel,- @Inject(FormulaRefRangeService) private _formulaRefRangeService: FormulaRefRangeService,- @Inject(LexerTreeBuilder) private _lexerTreeBuilder: LexerTreeBuilder+ @Inject(FormulaRefRangeService) private _formulaRefRangeService: FormulaRefRangeService) {super();this._initRefRange();diff --git a/packages/sheets-data-validation/src/models/sheet-data-validation-model.ts b/packages/sheets-data-validation/src/models/sheet-data-validation-model.tsindex 82ecdb514b..b8b4a2b303 100644--- a/packages/sheets-data-validation/src/models/sheet-data-validation-model.ts+++ b/packages/sheets-data-validation/src/models/sheet-data-validation-model.ts@@ -277,23 +277,6 @@ export class SheetDataValidationModel extends Disposable {}}- getRuleErrorMsg(unitId: string, subUnitId: string, ruleId: string) {- const rule = this._dataValidationModel.getRuleById(unitId, subUnitId, ruleId);- if (!rule) {- return '';- }- const validator = this._dataValidatorRegistryService.getValidatorItem(rule.type);- if (rule.error) {- return rule.error;- }-- if (validator) {- return validator.getRuleFinalError(rule);- }-- return '';- }-getRuleObjectMatrix(unitId: string, subUnitId: string) {return this._ensureRuleMatrix(unitId, subUnitId);}diff --git a/packages/sheets-data-validation/src/validators/const.ts b/packages/sheets-data-validation/src/validators/const.tsnew file mode 100644index 0000000000..c08d8fc781--- /dev/null+++ b/packages/sheets-data-validation/src/validators/const.ts@@ -0,0 +1,18 @@+/**+ * 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.+ */++export const FORMULA1 = '{FORMULA1}';+export const FORMULA2 = '{FORMULA2}';diff --git a/packages/sheets-data-validation/src/validators/custom-validator.ts b/packages/sheets-data-validation/src/validators/custom-validator.tsindex 6e671cd278..a76378cedf 100644--- a/packages/sheets-data-validation/src/validators/custom-validator.ts+++ b/packages/sheets-data-validation/src/validators/custom-validator.ts@@ -27,7 +27,7 @@ export class CustomFormulaValidator extends BaseDataValidator {override operators: DataValidationOperator[] = [];override scopes: string | string[] = ['sheet'];- private _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _customFormulaService = this.injector.get(DataValidationCustomFormulaService);override validatorFormula(rule: IDataValidationRule, unitId: string, subUnitId: string): IFormulaValidResult {const success = isFormulaString(rule.formula1);diff --git a/packages/sheets-data-validation/src/validators/date-validator.ts b/packages/sheets-data-validation/src/validators/date-validator.tsindex 1e9f64245c..b6cf8fedb0 100644--- a/packages/sheets-data-validation/src/validators/date-validator.ts+++ b/packages/sheets-data-validation/src/validators/date-validator.ts@@ -14,18 +14,19 @@* limitations under the License.*/-import type { CellValue, IDataValidationRule, IDataValidationRuleBase, Nullable } from '@univerjs/core';+import type { CellValue, IDataValidationRule, IDataValidationRuleBase, ISheetDataValidationRule, Nullable } from '@univerjs/core';import type { IFormulaResult, IFormulaValidResult, IValidatorCellInfo } from '@univerjs/data-validation';+import type { ISheetLocationBase } from '@univerjs/sheets';import { DataValidationOperator, DataValidationType, isFormulaString, numfmt, Tools } from '@univerjs/core';import { BaseDataValidator } from '@univerjs/data-validation';+import { LexerTreeBuilder } from '@univerjs/engine-formula';import dayjs from 'dayjs';import { DateOperatorErrorTitleMap, DateOperatorNameMap, DateOperatorTitleMap } from '../common/date-text-map';import { DataValidationCustomFormulaService } from '../services/dv-custom-formula.service';import { TWO_FORMULA_OPERATOR_COUNT } from '../types/const/two-formula-operators';import { isLegalFormulaResult } from '../utils/formula';--const FORMULA1 = '{FORMULA1}';-const FORMULA2 = '{FORMULA2}';+import { FORMULA1, FORMULA2 } from './const';+import { getTransformedFormula } from './util';const transformDate2SerialNumber = (value: Nullable<CellValue>) => {if (value === undefined || value === null || typeof value === 'boolean') {@@ -61,14 +62,15 @@ export class DateValidator extends BaseDataValidator<number> {];scopes: string | string[] = ['sheet'];- private _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _lexerTreeBuilder = this.injector.get(LexerTreeBuilder);override async parseFormula(rule: IDataValidationRule, unitId: string, subUnitId: string, row: number, column: number): Promise<IFormulaResult<number | undefined>> {const formulaResult1 = await this._customFormulaService.getCellFormulaValue(unitId, subUnitId, rule.uid, row, column);const formulaResult2 = await this._customFormulaService.getCellFormula2Value(unitId, subUnitId, rule.uid, row, column);const { formula1, formula2 } = rule;- const isFormulaValid = isLegalFormulaResult(String(formulaResult1)) && isLegalFormulaResult(String(formulaResult2));+ const isFormulaValid = isLegalFormulaResult(String(formulaResult1?.v)) && isLegalFormulaResult(String(formulaResult2?.v));return {formula1: transformDate2SerialNumber(isFormulaString(formula1) ? formulaResult1?.v : formula1),@@ -240,12 +242,13 @@ export class DateValidator extends BaseDataValidator<number> {return `${this.titleStr} ${ruleName}`;}- override generateRuleErrorMessage(rule: IDataValidationRuleBase) {+ override generateRuleErrorMessage(rule: IDataValidationRuleBase, pos: ISheetLocationBase) {if (!rule.operator) {return this.titleStr;}+ const { transformedFormula1, transformedFormula2 } = getTransformedFormula(this._lexerTreeBuilder, rule as ISheetDataValidationRule, pos);- const errorMsg = this.localeService.t(DateOperatorErrorTitleMap[rule.operator]).replace(FORMULA1, rule.formula1 ?? '').replace(FORMULA2, rule.formula2 ?? '');+ const errorMsg = this.localeService.t(DateOperatorErrorTitleMap[rule.operator]).replace(FORMULA1, transformedFormula1 ?? '').replace(FORMULA2, transformedFormula2 ?? '');return `${errorMsg}`;}}diff --git a/packages/sheets-data-validation/src/validators/decimal-validator.ts b/packages/sheets-data-validation/src/validators/decimal-validator.tsindex 314c761774..8dfce5bec0 100644--- a/packages/sheets-data-validation/src/validators/decimal-validator.ts+++ b/packages/sheets-data-validation/src/validators/decimal-validator.ts@@ -14,13 +14,18 @@* limitations under the License.*/-import type { CellValue, IDataValidationRule, Nullable } from '@univerjs/core';+import type { CellValue, IDataValidationRule, IDataValidationRuleBase, ISheetDataValidationRule, Nullable } from '@univerjs/core';import type { IFormulaResult, IFormulaValidResult, IValidatorCellInfo } from '@univerjs/data-validation';+import type { ISheetLocationBase } from '@univerjs/sheets';import { DataValidationOperator, DataValidationType, isFormulaString, Tools } from '@univerjs/core';import { BaseDataValidator } from '@univerjs/data-validation';+import { LexerTreeBuilder } from '@univerjs/engine-formula';import { DataValidationCustomFormulaService } from '../services/dv-custom-formula.service';+import { OperatorErrorTitleMap } from '../types';import { TWO_FORMULA_OPERATOR_COUNT } from '../types/const/two-formula-operators';import { isLegalFormulaResult } from '../utils/formula';+import { FORMULA1, FORMULA2 } from './const';+import { getTransformedFormula } from './util';export function getCellValueNumber(cellValue: CellValue) {let str = cellValue;@@ -36,8 +41,9 @@ export function getCellValueNumber(cellValue: CellValue) {}export class DecimalValidator extends BaseDataValidator<number> {- private _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _customFormulaService = this.injector.get(DataValidationCustomFormulaService);id: string = DataValidationType.DECIMAL;+ private readonly _lexerTreeBuilder = this.injector.get(LexerTreeBuilder);title: string = 'dataValidation.decimal.title';operators: DataValidationOperator[] = [@@ -85,8 +91,7 @@ export class DecimalValidator extends BaseDataValidator<number> {const formulaResult2 = await this._customFormulaService.getCellFormula2Value(unitId, subUnitId, rule.uid, row, column);const { formula1, formula2 } = rule;- const isFormulaValid = isLegalFormulaResult(String(formulaResult1)) && isLegalFormulaResult(String(formulaResult2));-+ const isFormulaValid = isLegalFormulaResult(String(formulaResult1?.v)) && isLegalFormulaResult(String(formulaResult2?.v));const info = {formula1: this._parseNumber(isFormulaString(formula1) ? formulaResult1?.v : formula1),formula2: this._parseNumber(isFormulaString(formula2) ? formulaResult2?.v : formula2),@@ -193,4 +198,14 @@ export class DecimalValidator extends BaseDataValidator<number> {return cellInfo.value <= formula1;}++ override generateRuleErrorMessage(rule: IDataValidationRuleBase, position: ISheetLocationBase) {+ if (!rule.operator) {+ return this.titleStr;+ }++ const { transformedFormula1, transformedFormula2 } = getTransformedFormula(this._lexerTreeBuilder, rule as ISheetDataValidationRule, position);+ const errorMsg = this.localeService.t(OperatorErrorTitleMap[rule.operator]).replace(FORMULA1, transformedFormula1 ?? '').replace(FORMULA2, transformedFormula2 ?? '');+ return `${errorMsg}`;+ }}diff --git a/packages/sheets-data-validation/src/validators/text-length-validator.ts b/packages/sheets-data-validation/src/validators/text-length-validator.tsindex fb669a9884..b30e93352d 100644--- a/packages/sheets-data-validation/src/validators/text-length-validator.ts+++ b/packages/sheets-data-validation/src/validators/text-length-validator.ts@@ -14,20 +14,22 @@* limitations under the License.*/-import type { CellValue, IDataValidationRule, IDataValidationRuleBase, Nullable } from '@univerjs/core';+import type { CellValue, IDataValidationRule, IDataValidationRuleBase, ISheetDataValidationRule, Nullable } from '@univerjs/core';import type { IFormulaResult, IFormulaValidResult, IValidatorCellInfo } from '@univerjs/data-validation';+import type { ISheetLocationBase } from '@univerjs/sheets';import { DataValidationOperator, DataValidationType, isFormulaString, Tools } from '@univerjs/core';import { BaseDataValidator, TextLengthErrorTitleMap } from '@univerjs/data-validation';+import { LexerTreeBuilder } from '@univerjs/engine-formula';import { DataValidationCustomFormulaService } from '../services/dv-custom-formula.service';import { TWO_FORMULA_OPERATOR_COUNT } from '../types/const/two-formula-operators';import { isLegalFormulaResult } from '../utils/formula';--const FORMULA1 = '{FORMULA1}';-const FORMULA2 = '{FORMULA2}';+import { FORMULA1, FORMULA2 } from './const';+import { getTransformedFormula } from './util';export class TextLengthValidator extends BaseDataValidator<number> {id: string = DataValidationType.TEXT_LENGTH;title: string = 'dataValidation.textLength.title';+ private readonly _lexerTreeBuilder = this.injector.get(LexerTreeBuilder);operators: DataValidationOperator[] = [DataValidationOperator.BETWEEN,@@ -42,7 +44,7 @@ export class TextLengthValidator extends BaseDataValidator<number> {scopes: string | string[] = ['sheet'];- private _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _customFormulaService = this.injector.get(DataValidationCustomFormulaService);private _isFormulaOrInt(formula: string) {return !Tools.isBlank(formula) && (isFormulaString(formula) || (!Number.isNaN(+formula) && Number.isInteger(+formula)));@@ -90,7 +92,7 @@ export class TextLengthValidator extends BaseDataValidator<number> {const formulaResult2 = await this._customFormulaService.getCellFormula2Value(unitId, subUnitId, rule.uid, row, column);const { formula1, formula2 } = rule;- const isFormulaValid = isLegalFormulaResult(String(formulaResult1)) && isLegalFormulaResult(String(formulaResult2));+ const isFormulaValid = isLegalFormulaResult(String(formulaResult1?.v)) && isLegalFormulaResult(String(formulaResult2?.v));return {formula1: this._parseNumber(isFormulaString(formula1) ? formulaResult1?.v : formula1),@@ -195,12 +197,13 @@ export class TextLengthValidator extends BaseDataValidator<number> {return cellValue <= formula1;}- override generateRuleErrorMessage(rule: IDataValidationRuleBase) {+ override generateRuleErrorMessage(rule: IDataValidationRuleBase, pos: ISheetLocationBase) {if (!rule.operator) {return this.titleStr;}- const errorMsg = this.localeService.t(TextLengthErrorTitleMap[rule.operator]).replace(FORMULA1, rule.formula1 ?? '').replace(FORMULA2, rule.formula2 ?? '');+ const { transformedFormula1, transformedFormula2 } = getTransformedFormula(this._lexerTreeBuilder, rule as ISheetDataValidationRule, pos);+ const errorMsg = this.localeService.t(TextLengthErrorTitleMap[rule.operator]).replace(FORMULA1, transformedFormula1 ?? '').replace(FORMULA2, transformedFormula2 ?? '');return `${errorMsg}`;}}diff --git a/packages/sheets-data-validation/src/validators/util.ts b/packages/sheets-data-validation/src/validators/util.tsindex 93e1b151a5..9855d8b405 100644--- a/packages/sheets-data-validation/src/validators/util.ts+++ b/packages/sheets-data-validation/src/validators/util.ts@@ -14,8 +14,10 @@* limitations under the License.*/-import type { ICellData, IUnitRangeName, IUniverInstanceService, Nullable, Workbook } from '@univerjs/core';-import { Range, UniverInstanceType } from '@univerjs/core';+import type { ICellData, ISheetDataValidationRule, IUnitRangeName, IUniverInstanceService, Nullable, Workbook } from '@univerjs/core';+import type { LexerTreeBuilder } from '@univerjs/engine-formula';+import type { ISheetLocationBase } from '@univerjs/sheets';+import { isFormulaString, Range, UniverInstanceType } from '@univerjs/core';import { getCellValueOrigin } from '../utils/get-cell-data-origin';export function getSheetRangeValueSet(grid: IUnitRangeName, univerInstanceService: IUniverInstanceService, currUnitId: string, currSubUnitId: string) {@@ -63,3 +65,19 @@ export function getDataValidationCellValue(cellData: Nullable<ICellData>) {return cellValue.toString();}++export function getTransformedFormula(lexerTreeBuilder: LexerTreeBuilder, rule: ISheetDataValidationRule, position: ISheetLocationBase) {+ const { formula1, formula2 } = rule;+ const originStartRow = rule.ranges[0].startRow;+ const originStartColumn = rule.ranges[0].startColumn;+ const offsetRow = position.row - originStartRow;+ const offsetColumn = position.col - originStartColumn;++ const transformedFormula1 = isFormulaString(formula1) ? lexerTreeBuilder.moveFormulaRefOffset(formula1!, offsetColumn, offsetRow, true) : formula1;+ const transformedFormula2 = isFormulaString(formula2) ? lexerTreeBuilder.moveFormulaRefOffset(formula2!, offsetColumn, offsetRow, true) : formula2;++ return {+ transformedFormula1,+ transformedFormula2,+ };+}diff --git a/packages/sheets-data-validation/src/validators/whole-validator.ts b/packages/sheets-data-validation/src/validators/whole-validator.tsindex 5ec15841cd..a921f349b6 100644--- a/packages/sheets-data-validation/src/validators/whole-validator.ts+++ b/packages/sheets-data-validation/src/validators/whole-validator.ts@@ -14,17 +14,23 @@* limitations under the License.*/-import type { CellValue, IDataValidationRule, IDataValidationRuleBase, Nullable } from '@univerjs/core';+import type { CellValue, IDataValidationRule, IDataValidationRuleBase, ISheetDataValidationRule, Nullable } from '@univerjs/core';import type { IFormulaResult, IFormulaValidResult, IValidatorCellInfo } from '@univerjs/data-validation';+import type { ISheetLocationBase } from '@univerjs/sheets';import { DataValidationOperator, DataValidationType, isFormulaString, Tools } from '@univerjs/core';import { BaseDataValidator } from '@univerjs/data-validation';+import { LexerTreeBuilder } from '@univerjs/engine-formula';import { DataValidationCustomFormulaService } from '../services/dv-custom-formula.service';+import { OperatorErrorTitleMap } from '../types';import { TWO_FORMULA_OPERATOR_COUNT } from '../types/const/two-formula-operators';import { isLegalFormulaResult } from '../utils/formula';+import { FORMULA1, FORMULA2 } from './const';import { getCellValueNumber } from './decimal-validator';+import { getTransformedFormula } from './util';export class WholeValidator extends BaseDataValidator<number> {- private _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _customFormulaService = this.injector.get(DataValidationCustomFormulaService);+ private readonly _lexerTreeBuilder = this.injector.get(LexerTreeBuilder);id: string = DataValidationType.WHOLE;title: string = 'dataValidation.whole.title';@@ -184,4 +190,13 @@ export class WholeValidator extends BaseDataValidator<number> {return cellInfo.value <= formula1;}++ override generateRuleErrorMessage(rule: IDataValidationRuleBase, position: ISheetLocationBase) {+ if (!rule.operator) {+ return this.titleStr;+ }+ const { transformedFormula1, transformedFormula2 } = getTransformedFormula(this._lexerTreeBuilder, rule as ISheetDataValidationRule, position);+ const errorMsg = this.localeService.t(OperatorErrorTitleMap[rule.operator]).replace(FORMULA1, transformedFormula1 ?? '').replace(FORMULA2, transformedFormula2 ?? '');+ return `${errorMsg}`;+ }}