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(sheet-numfmt-plugin):numfmt plugin optimization

eiji-th079d7b9c669c
diff --git a/packages/base-numfmt-engine/test/numfmt.test.ts b/packages/base-numfmt-engine/test/numfmt.test.ts
index 4a813a887b..8ea14d8e41 100644
--- a/packages/base-numfmt-engine/test/numfmt.test.ts
+++ b/packages/base-numfmt-engine/test/numfmt.test.ts
@@ -38,4 +38,8 @@ test('numfmt currency', () => {
test('numfmt percentage', () => {
const formatter = numfmt("0.00%");
expect(formatter(0.1)).toEqual('10.00%');
-});;
+});
+
+test('numfmt percentage', () => {
+ console.log(numfmt.parseValue('10%'));
+});
diff --git a/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts b/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
index 9275a4ff1b..a6c742785d 100644
--- a/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
+++ b/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
@@ -1,4 +1,3 @@
export enum ACTION_NAMES {
- SET_NUMFMT_COORDS_ACTION = 'SetNumfmtCoordsAction',
- SET_NUMFMT_RANGE_ACTION = 'SetNumfmtRangeAction',
+ SET_NUMFMT_RANGE_DATA_ACTION = 'SetNumfmtRangeDataAction',
}
diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
index 1eaae2b91d..205a7db467 100644
--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
@@ -1,5 +1,4 @@
-import { Command, Range, IRangeData, ObjectMatrix, Plugin } from '@univer/core';
-import { ACTION_NAMES } from '../Const';
+import { Command, Range, IRangeData, ObjectMatrix, Plugin, ACTION_NAMES, ObjectMatrixPrimitiveType } from '@univer/core';
import { NumfmtModel } from '../Model/NumfmtModel';
export class NumfmtController {
@@ -12,34 +11,38 @@ export class NumfmtController {
this._plugin = plugin;
}
+ getNumfmtBySheetIdConfig(sheetId: string): ObjectMatrixPrimitiveType<string> {
+ return this._model.getNumfmtBySheetIdConfig(sheetId);
+ }
+
setNumfmtByRange(sheetId: string, numfmtRange: IRangeData, numfmtValue: string): void {
const numfmtMatrix = new ObjectMatrix<string>();
Range.foreach(numfmtRange, (row, column) => {
numfmtMatrix.setValue(row, column, numfmtValue);
});
- this._model.setNumfmtMatrix(sheetId, numfmtMatrix);
const pluginContext = this._plugin.getContext();
const commandManager = pluginContext.getCommandManager();
const config = {
- actionName: ACTION_NAMES.SET_NUMFMT_RANGE_ACTION,
+ actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
sheetId,
- numfmtMatrix,
- numfmtValue,
+ rangeData: numfmtRange,
+ cellValue: numfmtMatrix.toJSON(),
};
const command = new Command(pluginContext.getWorkBook(), config);
commandManager.invoke(command);
}
setNumfmtByCoords(sheetId: string, row: number, column: number, numfmt: string): void {
- this._model.setNumfmtCoords(sheetId, row, column, numfmt);
+ const numfmtMatrix = new ObjectMatrix<string>();
+ numfmtMatrix.setValue(row, column, numfmt);
+ const numfmtRange: IRangeData = { startRow: row, startColumn: column, endRow: row, endColumn: column };
const pluginContext = this._plugin.getContext();
const commandManager = pluginContext.getCommandManager();
const config = {
- actionName: ACTION_NAMES.SET_NUMFMT_COORDS_ACTION,
+ actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
sheetId,
- row,
- column,
- numfmt,
+ rangeData: numfmtRange,
+ cellValue: numfmtMatrix.toJSON(),
};
const command = new Command(pluginContext.getWorkBook(), config);
commandManager.invoke(command);
diff --git a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts
deleted file mode 100644
index d3019252ea..0000000000
--- a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import { ActionBase, ActionObservers, IActionData, Workbook } from '@univer/core';
-import { SetNumfmtCoords } from '../Apply/SetNumfmtCoords';
-
-export interface ISetNumfmtCoordsActionData extends IActionData {
- row: number;
- column: number;
- numfmt: string;
-}
-
-export class SetNumfmtCoordsAction extends ActionBase<ISetNumfmtCoordsActionData, ISetNumfmtCoordsActionData> {
- constructor(actionData: ISetNumfmtCoordsActionData, workbook: Workbook, observers: ActionObservers) {
- super(actionData, workbook, observers);
- this._doActionData = {
- ...actionData,
- convertor: [],
- };
- this._oldActionData = {
- ...actionData,
- numfmt: this.do(),
- convertor: [],
- };
- this.do();
- }
-
- redo(): string {
- return SetNumfmtCoords(this.getWorkBook(), this._doActionData.sheetId, this._doActionData.row, this._doActionData.column, this._doActionData.numfmt);
- }
-
- do(): string {
- return this.redo();
- }
-
- undo(): void {
- SetNumfmtCoords(this.getWorkBook(), this._oldActionData.sheetId, this._oldActionData.row, this._oldActionData.column, this._oldActionData.numfmt);
- }
-
- validate(): boolean {
- return false;
- }
-}
diff --git a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeDataAction.ts
similarity index 67%
rename from packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts
rename to packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeDataAction.ts
index 56415ece34..2df54a9162 100644
--- a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts
+++ b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeDataAction.ts
@@ -1,11 +1,11 @@
import { ActionBase, ActionObservers, IActionData, ObjectMatrixPrimitiveType, Workbook } from '@univer/core/src';
-import { SetNumfmtRange } from '../Apply/SetNumfmtRange';
+import { SetNumfmtRangeData } from '../Apply/SetNumfmtRangeData';
export interface ISetNumfmtRangeActionData extends IActionData {
numfmtMatrix: ObjectMatrixPrimitiveType<string>;
}
-export class SetNumfmtRangeAction extends ActionBase<ISetNumfmtRangeActionData, ISetNumfmtRangeActionData> {
+export class SetNumfmtRangeDataAction extends ActionBase<ISetNumfmtRangeActionData, ISetNumfmtRangeActionData> {
constructor(actionData: ISetNumfmtRangeActionData, workbook: Workbook, observers: ActionObservers) {
super(actionData, workbook, observers);
this._doActionData = {
@@ -24,11 +24,11 @@ export class SetNumfmtRangeAction extends ActionBase<ISetNumfmtRangeActionData,
}
redo(): ObjectMatrixPrimitiveType<string> {
- return SetNumfmtRange(this.getWorkBook(), this._doActionData.sheetId, this._doActionData.numfmtMatrix);
+ return SetNumfmtRangeData(this.getWorkBook(), this._doActionData.sheetId, this._doActionData.numfmtMatrix);
}
undo(): void {
- SetNumfmtRange(this.getWorkBook(), this._oldActionData.sheetId, this._oldActionData.numfmtMatrix);
+ SetNumfmtRangeData(this.getWorkBook(), this._oldActionData.sheetId, this._oldActionData.numfmtMatrix);
}
validate(): boolean {
diff --git a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts
deleted file mode 100644
index b1a62a0b5c..0000000000
--- a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { Workbook } from '@univer/core';
-
-export function SetNumfmtCoords(workbook: Workbook, sheetId: string, row: number, column: number, numfmt: string): string {
- let worksheet = workbook.getSheetBySheetId(sheetId);
- let orderNumfmt: string = '';
- if (worksheet) {
- const worksheetCellMatrix = worksheet.getCellMatrix();
- const cell = worksheetCellMatrix.getValue(row, column);
- if (cell) {
- orderNumfmt = cell.m ?? '';
- cell.m = numfmt;
- } else {
- orderNumfmt = '';
- worksheetCellMatrix.setValue(row, column, { m: numfmt });
- }
- }
- return orderNumfmt;
-}
diff --git a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRange.ts b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRange.ts
deleted file mode 100644
index 5b84570ebd..0000000000
--- a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRange.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { ObjectMatrix, ObjectMatrixPrimitiveType, Workbook } from '@univer/core/src';
-
-export function SetNumfmtRange(workbook: Workbook, sheetId: string, numfmtMatrix: ObjectMatrixPrimitiveType<string>): ObjectMatrixPrimitiveType<string> {
- const orderNumfmtMatrix = new ObjectMatrix<string>();
- const currNumfmtMatrix = new ObjectMatrix(numfmtMatrix);
- const worksheet = workbook.getSheetBySheetId(sheetId);
- if (worksheet) {
- const worksheetCellMatrix = worksheet.getCellMatrix();
- currNumfmtMatrix.forValue((r, c, m) => {
- const cell = worksheetCellMatrix.getValue(r, c);
- if (cell) {
- orderNumfmtMatrix.setValue(r, c, cell.m);
- cell.m = m;
- } else {
- orderNumfmtMatrix.setValue(r, c, '');
- worksheetCellMatrix.setValue(r, c, { m });
- }
- });
- }
- return orderNumfmtMatrix.toJSON();
-}
diff --git a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRangeData.ts b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRangeData.ts
new file mode 100644
index 0000000000..ac60f40591
--- /dev/null
+++ b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRangeData.ts
@@ -0,0 +1,18 @@
+import { ObjectMatrix, ObjectMatrixPrimitiveType, Workbook } from '@univer/core/src';
+import { NUMFMT_PLUGIN_NAME } from '../../Const';
+import { NumfmtPlugin } from '../../NumfmtPlugin';
+
+export function SetNumfmtRangeData(workbook: Workbook, sheetId: string, numfmtPrimitiveMatrix: ObjectMatrixPrimitiveType<string>): ObjectMatrixPrimitiveType<string> {
+ const context = workbook.getContext();
+ const manager = context.getPluginManager();
+ const numfmtPlugin = manager.getPluginByName(NUMFMT_PLUGIN_NAME) as NumfmtPlugin;
+ const numfmtConfig = numfmtPlugin.getNumfmtBySheetIdConfig(sheetId);
+ const currSheetNumfmtMatrix = new ObjectMatrix(numfmtConfig);
+ const olderSheetNumfmtMatrix = new ObjectMatrix<string>();
+ new ObjectMatrix(numfmtPrimitiveMatrix).forValue((r, c, value) => {
+ const olderValue = currSheetNumfmtMatrix.getValue(r, c);
+ currSheetNumfmtMatrix.setValue(r, c, value);
+ olderSheetNumfmtMatrix.setValue(r, c, olderValue);
+ });
+ return olderSheetNumfmtMatrix.toJSON();
+}
diff --git a/packages/sheets-plugin-numfmt/src/Model/NumfmtModel.ts b/packages/sheets-plugin-numfmt/src/Model/NumfmtModel.ts
index e67c056a12..3893871582 100644
--- a/packages/sheets-plugin-numfmt/src/Model/NumfmtModel.ts
+++ b/packages/sheets-plugin-numfmt/src/Model/NumfmtModel.ts
@@ -1,31 +1,19 @@
-import { ObjectMatrix } from '@univer/core';
+import { ObjectMatrixPrimitiveType } from '@univer/core';
export class NumfmtModel {
- protected _sheetNumfmtMatrix: Map<string, ObjectMatrix<string>>;
+ protected _sheetsNumfmtConfig: Map<string, ObjectMatrixPrimitiveType<string>>;
- protected _fromMatrix(sheetId: string): ObjectMatrix<string> {
- let sheetNumfmtMatrix = this._sheetNumfmtMatrix.get(sheetId);
+ constructor() {
+ this._sheetsNumfmtConfig = new Map();
+ }
+
+ getNumfmtBySheetIdConfig(sheetId: string): ObjectMatrixPrimitiveType<string> {
+ let sheetNumfmtMatrix = this._sheetsNumfmtConfig.get(sheetId);
if (sheetNumfmtMatrix) {
return sheetNumfmtMatrix;
}
- sheetNumfmtMatrix = new ObjectMatrix<string>();
- this._sheetNumfmtMatrix.set(sheetId, sheetNumfmtMatrix);
+ sheetNumfmtMatrix = {};
+ this._sheetsNumfmtConfig.set(sheetId, sheetNumfmtMatrix);
return sheetNumfmtMatrix;
}
-
- constructor() {
- this._sheetNumfmtMatrix = new Map();
- }
-
- setNumfmtMatrix(sheetId: string, matrix: ObjectMatrix<string>): void {
- const numfmtMatrix = this._fromMatrix(sheetId);
- matrix.forValue((row: number, col: number, numfmt) => {
- numfmtMatrix.setValue(row, col, numfmt);
- });
- }
-
- setNumfmtCoords(sheetId: string, row: number, col: number, numfmt: string): void {
- const numfmtMatrix = this._fromMatrix(sheetId);
- numfmtMatrix.setValue(row, col, numfmt);
- }
}
diff --git a/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts b/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts
index 96550859c4..e844ec8927 100644
--- a/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts
+++ b/packages/sheets-plugin-numfmt/src/NumfmtActionExtensionFactory.ts
@@ -1,5 +1,6 @@
import { ACTION_NAMES, Plugin, BaseActionExtension, BaseActionExtensionFactory, IActionData, ISetRangeDataActionData, ObjectMatrix } from '@univer/core';
import { numfmt } from '@univer/base-numfmt-engine';
+import { ACTION_NAMES as PLUGIN_ACTION_NAMES } from './Const';
export class NumfmtActionExtension extends BaseActionExtension<ISetRangeDataActionData> {
constructor(protected actionData: ISetRangeDataActionData) {
@@ -7,18 +8,20 @@ export class NumfmtActionExtension extends BaseActionExtension<ISetRangeDataActi
}
execute() {
- const matrix = new ObjectMatrix(this.actionData.cellValue);
- matrix.forEach((rowNumber, row) => {
- row.forEach((colNumber, cell) => {
- if (cell.v) {
- const result = numfmt.parseValue(cell.v) as { z: string };
- if (result) {
- const format = numfmt(result.z);
- cell.m = format(String(cell.v));
- }
- }
- });
+ const rangeMatrix = new ObjectMatrix(this.actionData.cellValue);
+ const numfmtMatrix = new ObjectMatrix<string>();
+ rangeMatrix.forValue((r, c, cell) => {
+ const result = numfmt.parseValue(cell.v) as { z: string };
+ const patten = result.z || 'General';
+ cell.m = numfmt(patten)(String(cell.v));
+ numfmtMatrix.setValue(r, c, patten);
});
+ const setNumfmtRangeDataAction = {
+ actionName: PLUGIN_ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,
+ sheetId: this.actionData.sheetId,
+ rangeData: this.actionData.rangeData,
+ cellValue: numfmtMatrix.toJSON(),
+ };
}
}
diff --git a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
index 5d855830fe..1ed397722f 100644
--- a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
+++ b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
@@ -1,4 +1,4 @@
-import { ActionExtensionManager, Context, IRangeData, Plugin } from '@univer/core';
+import { ActionExtensionManager, Context, IRangeData, ObjectMatrixPrimitiveType, Plugin } from '@univer/core';
import { NUMFMT_PLUGIN_NAME } from './Const/PLUGIN_NAME';
import { NumfmtController } from './Controller/NumfmtController';
import { NumfmtActionExtensionFactory } from './NumfmtActionExtensionFactory';
@@ -6,17 +6,25 @@ import { NumfmtActionExtensionFactory } from './NumfmtActionExtensionFactory';
export class NumfmtPlugin extends Plugin {
protected _controller: NumfmtController;
+ protected _numfmtActionExtensionFactory: NumfmtActionExtensionFactory;
+
constructor() {
super(NUMFMT_PLUGIN_NAME);
}
onMounted(context: Context): void {
+ this._numfmtActionExtensionFactory = new NumfmtActionExtensionFactory(this);
this._controller = new NumfmtController(this);
+ ActionExtensionManager.create().add(this._numfmtActionExtensionFactory);
+ }
+
+ onDestroy() {
+ super.onDestroy();
+ ActionExtensionManager.create().delete(this._numfmtActionExtensionFactory);
}
- intercepted(): void {
- const register = ActionExtensionManager.create();
- register.add(new NumfmtActionExtensionFactory(this));
+ getNumfmtBySheetIdConfig(sheetId: string): ObjectMatrixPrimitiveType<string> {
+ return this._controller.getNumfmtBySheetIdConfig(sheetId);
}
setNumfmtByRange(sheetId: string, range: IRangeData, format: string): void {
diff --git a/packages/sheets-plugin-numfmt/src/RegisterActionExtension.ts b/packages/sheets-plugin-numfmt/test/NumfmtPlugin.test.ts
similarity index 100%
rename from packages/sheets-plugin-numfmt/src/RegisterActionExtension.ts
rename to packages/sheets-plugin-numfmt/test/NumfmtPlugin.test.ts