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.

feat(facade): add range to-string api & fix data-validation api (#4325)

zhangw47773f8d9973
diff --git a/packages/sheets-data-validation/src/facade/f-data-validation.ts b/packages/sheets-data-validation/src/facade/f-data-validation.ts
index 86456f7e74..4f541d2646 100644
--- a/packages/sheets-data-validation/src/facade/f-data-validation.ts
+++ b/packages/sheets-data-validation/src/facade/f-data-validation.ts
@@ -14,10 +14,9 @@
* limitations under the License.
*/
-import type { DataValidationOperator, DataValidationType, IDataValidationRule, IDataValidationRuleOptions, IRange } from '@univerjs/core';
+import type { DataValidationOperator, DataValidationType, IDataValidationRule, IDataValidationRuleOptions, Injector, IRange, Workbook, Worksheet } from '@univerjs/core';
import type { IRemoveSheetDataValidationCommandParams, IUpdateSheetDataValidationOptionsCommandParams, IUpdateSheetDataValidationRangeCommandParams, IUpdateSheetDataValidationSettingCommandParams } from '@univerjs/sheets-data-validation';
-import type { FWorksheet } from '@univerjs/sheets/facade';
-import { DataValidationErrorStyle, ICommandService } from '@univerjs/core';
+import { DataValidationErrorStyle, ICommandService, IUniverInstanceService } from '@univerjs/core';
import { DataValidationModel, getRuleOptions } from '@univerjs/data-validation';
import { RemoveSheetDataValidationCommand, UpdateSheetDataValidationOptionsCommand, UpdateSheetDataValidationRangeCommand, UpdateSheetDataValidationSettingCommand } from '@univerjs/sheets-data-validation';
import { FRange } from '@univerjs/sheets/facade';
@@ -25,9 +24,11 @@ import { FDataValidationBuilder } from './f-data-validation-builder';
export class FDataValidation {
rule: IDataValidationRule;
- private _worksheet: FWorksheet | undefined;
+ private _worksheet: Worksheet | undefined;
+ private _injector: Injector | undefined;
- constructor(rule: IDataValidationRule, worksheet?: FWorksheet) {
+ constructor(rule: IDataValidationRule, worksheet?: Worksheet, _injector?: Injector) {
+ this._injector = _injector;
this.rule = rule;
this._worksheet = worksheet;
}
@@ -88,8 +89,8 @@ export class FDataValidation {
return false;
}
- const dataValidationModel = this._worksheet.getInject().get(DataValidationModel);
- const currentRule = dataValidationModel.getRuleById(this._worksheet.getWorkbook().getUnitId(), this._worksheet.getSheetId(), this.rule.uid);
+ const dataValidationModel = this._injector!.get(DataValidationModel);
+ const currentRule = dataValidationModel.getRuleById(this._worksheet.getUnitId(), this._worksheet.getSheetId(), this.rule.uid);
if (currentRule && currentRule.ranges.length) {
return true;
@@ -103,21 +104,12 @@ export class FDataValidation {
* @returns An array of IRange objects representing the ranges to which the data validation rule is applied.
*/
getRanges(): FRange[] {
- if (!this.getAllowInvalid()) {
- return [];
- }
-
- const workbook = this._worksheet?.getWorkbook();
- const sheetId = this.getSheetId();
- if (!sheetId) {
- return [];
- }
- const worksheet = workbook?.getSheetBySheetId(sheetId);
- if (!workbook || !worksheet) {
+ if (!this.getApplied()) {
return [];
}
- return this.rule.ranges.map((range: IRange) => this._worksheet?.getInject().createInstance(FRange, workbook, worksheet, range));
+ const workbook = this._injector!.get(IUniverInstanceService).getUnit<Workbook>(this._worksheet!.getUnitId())!;
+ return this.rule.ranges.map((range: IRange) => this._injector!.createInstance(FRange, workbook, this._worksheet!, range));
}
/**
@@ -126,7 +118,7 @@ export class FDataValidation {
* @returns The title of the error message dialog box.
*/
getUnitId(): string | undefined {
- return this._worksheet?.getWorkbook().getUnitId();
+ return this._worksheet?.getUnitId();
}
/**
@@ -146,7 +138,7 @@ export class FDataValidation {
*/
setCriteria(type: DataValidationType, values: [DataValidationOperator, string, string]): boolean {
if (this.getApplied()) {
- const commandService = this._worksheet!.getInject().get(ICommandService);
+ const commandService = this._injector!.get(ICommandService);
const res = commandService.syncExecuteCommand(UpdateSheetDataValidationSettingCommand.id, {
unitId: this.getUnitId(),
subUnitId: this.getSheetId(),
@@ -180,7 +172,7 @@ export class FDataValidation {
*/
setOptions(options: Partial<IDataValidationRuleOptions>): boolean {
if (this.getApplied()) {
- const commandService = this._worksheet!.getInject().get(ICommandService);
+ const commandService = this._injector!.get(ICommandService);
const res = commandService.syncExecuteCommand(UpdateSheetDataValidationOptionsCommand.id, {
unitId: this.getUnitId(),
subUnitId: this.getSheetId(),
@@ -207,7 +199,7 @@ export class FDataValidation {
*/
setRanges(ranges: FRange[]): boolean {
if (this.getApplied()) {
- const commandService = this._worksheet!.getInject().get(ICommandService);
+ const commandService = this._injector!.get(ICommandService);
const res = commandService.syncExecuteCommand(UpdateSheetDataValidationRangeCommand.id, {
unitId: this.getUnitId(),
subUnitId: this.getSheetId(),
@@ -220,7 +212,7 @@ export class FDataValidation {
}
}
- this.rule.ranges = ranges;
+ this.rule.ranges = ranges.map((range) => range.getRange());
return true;
}
@@ -233,7 +225,7 @@ export class FDataValidation {
return false;
}
- const commandService = this._worksheet!.getInject().get(ICommandService);
+ const commandService = this._injector!.get(ICommandService);
return commandService.syncExecuteCommand(RemoveSheetDataValidationCommand.id, {
unitId: this.getUnitId(),
subUnitId: this.getSheetId(),
diff --git a/packages/sheets-data-validation/src/facade/f-range.ts b/packages/sheets-data-validation/src/facade/f-range.ts
index d249a3f4b1..e27202aeb4 100644
--- a/packages/sheets-data-validation/src/facade/f-range.ts
+++ b/packages/sheets-data-validation/src/facade/f-range.ts
@@ -16,8 +16,8 @@
import type { DataValidationStatus, Nullable } from '@univerjs/core';
import type { IAddSheetDataValidationCommandParams, IClearRangeDataValidationCommandParams } from '@univerjs/sheets-data-validation';
-import { FRange } from '@univerjs/sheets/facade';
import { AddSheetDataValidationCommand, ClearRangeDataValidationCommand, SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';
+import { FRange } from '@univerjs/sheets/facade';
import { FDataValidation } from './f-data-validation';
export interface IFRangeDataValidationMixin {
@@ -75,7 +75,7 @@ export class FRangeDataValidationMixin extends FRange implements IFRangeDataVali
);
if (rule) {
- return new FDataValidation(rule);
+ return new FDataValidation(rule, this._worksheet, this._injector);
}
return rule;
@@ -87,7 +87,7 @@ export class FRangeDataValidationMixin extends FRange implements IFRangeDataVali
this._workbook.getUnitId(),
this._worksheet.getSheetId(),
[this._range]
- ).map((rule) => new FDataValidation(rule));
+ ).map((rule) => new FDataValidation(rule, this._worksheet, this._injector));
}
override async getValidatorStatus(): Promise<Promise<DataValidationStatus>[][]> {
diff --git a/packages/sheets-data-validation/src/facade/f-univer.ts b/packages/sheets-data-validation/src/facade/f-univer.ts
index c5351f8a93..a765dc2aab 100644
--- a/packages/sheets-data-validation/src/facade/f-univer.ts
+++ b/packages/sheets-data-validation/src/facade/f-univer.ts
@@ -18,9 +18,16 @@ import { FUniver } from '@univerjs/core';
import { FDataValidationBuilder } from './f-data-validation-builder';
export class FUnvierDataValidationMixin {
+ /**
+ * @deparecated use `univerAPI.newDataValidation()` as instead.
+ */
static newDataValidation(): FDataValidationBuilder {
return new FDataValidationBuilder();
}
+
+ newDataValidation(): FDataValidationBuilder {
+ return new FDataValidationBuilder();
+ }
}
FUniver.extend(FUnvierDataValidationMixin);
diff --git a/packages/sheets-data-validation/src/facade/f-worksheet.ts b/packages/sheets-data-validation/src/facade/f-worksheet.ts
index 907c59a2f3..d418fb4a93 100644
--- a/packages/sheets-data-validation/src/facade/f-worksheet.ts
+++ b/packages/sheets-data-validation/src/facade/f-worksheet.ts
@@ -16,8 +16,8 @@
import type { DataValidationStatus, Nullable, ObjectMatrix } from '@univerjs/core';
import { DataValidationModel } from '@univerjs/data-validation';
-import { FWorksheet } from '@univerjs/sheets/facade';
import { SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation';
+import { FWorksheet } from '@univerjs/sheets/facade';
import { FDataValidation } from './f-data-validation';
export interface IFWorksheetDataValidationMixin {
@@ -36,7 +36,7 @@ export interface IFWorksheetDataValidationMixin {
export class FWorksheetDataValidationMixin extends FWorksheet implements IFWorksheetDataValidationMixin {
override getDataValidations(): FDataValidation[] {
const dataValidationModel = this._injector.get(DataValidationModel);
- return dataValidationModel.getRules(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((rule) => new FDataValidation(rule));
+ return dataValidationModel.getRules(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((rule) => new FDataValidation(rule, this._worksheet, this._injector));
}
override getValidatorStatus(): Promise<ObjectMatrix<Nullable<DataValidationStatus>>> {
diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts
index 210c2f7cb4..642523d37d 100644
--- a/packages/sheets/src/facade/f-range.ts
+++ b/packages/sheets/src/facade/f-range.ts
@@ -17,8 +17,8 @@
import type { CellValue, ICellData, IColorStyle, IObjectMatrixPrimitiveType, IRange, IStyleData, ITextDecoration, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { ISetHorizontalTextAlignCommandParams, ISetStyleCommandParams, ISetTextWrapCommandParams, ISetVerticalTextAlignCommandParams, IStyleTypeValue, SplitDelimiterEnum } from '@univerjs/sheets';
import type { FHorizontalAlignment, FVerticalAlignment } from './utils';
-import { BooleanNumber, Dimension, FBase, ICommandService, Inject, Injector, Rectangle, Tools, WrapStrategy } from '@univerjs/core';
-import { FormulaDataModel } from '@univerjs/engine-formula';
+import { BooleanNumber, Dimension, FBase, ICommandService, Inject, Injector, Rectangle, WrapStrategy } from '@univerjs/core';
+import { FormulaDataModel, serializeRange, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { addMergeCellsUtil, getAddMergeMutationRangeByType, RemoveWorksheetMergeCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SplitTextToColumnsCommand } from '@univerjs/sheets';
import { FWorkbook } from './f-workbook';
import { covertCellValue, covertCellValues, transformCoreHorizontalAlignment, transformCoreVerticalAlignment, transformFacadeHorizontalAlignment, transformFacadeVerticalAlignment } from './utils';
@@ -632,20 +632,8 @@ export class FRange extends FBase {
* console.log(fRange.getA1Notation()); // A1:B2
* ```
*/
- getA1Notation(): string {
- const { startRow, endRow, startColumn, endColumn } = this._range;
- let start;
- let end;
- if (startColumn < endColumn) {
- start = Tools.numToWord(startColumn + 1) + (startRow + 1);
- end = Tools.numToWord(endColumn + 1) + (endRow + 1);
- } else {
- start = Tools.numToWord(endColumn + 1) + (endRow + 1);
- end = Tools.numToWord(startColumn + 1) + (startRow + 1);
- }
-
- if (start === end) return `${start}`;
- return `${start}:${end}`;
+ getA1Notation(withSheet?: boolean): string {
+ return withSheet ? serializeRangeWithSheet(this._worksheet.getName(), this._range) : serializeRange(this._range);
}
/**