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-thdc19506277c5
diff --git a/packages/core/src/Shared/ObjectMatrix.ts b/packages/core/src/Shared/ObjectMatrix.ts
index 6ab945e364..6934ec0af3 100644
--- a/packages/core/src/Shared/ObjectMatrix.ts
+++ b/packages/core/src/Shared/ObjectMatrix.ts
@@ -59,6 +59,29 @@ export class ObjectMatrix<T> {
return this;
}
+ forValue(
+ callback: (row: number, col: number, value: T) => Nullable<boolean>
+ ): ObjectMatrix<T> {
+ const rowArray = this._matrix;
+ const rowKeys = rowArray.getKeys();
+ const rowLength = rowKeys.length;
+ for (let i = 0; i < rowLength; i++) {
+ const rowNumber = +rowKeys[i];
+ const colArray = this.getRow(rowNumber) as ObjectArray<T>;
+ const colKeys = colArray.getKeys();
+ const colLength = colKeys.length;
+ for (let j = 0; j < colLength; j++) {
+ const colNumber = +colKeys[j];
+ const value = colArray.get(colNumber) as T;
+ const result = callback(rowNumber, colNumber, value);
+ if (result === false) {
+ return this;
+ }
+ }
+ }
+ return this;
+ }
+
swapRow(src: number, target: number): void {
const srcRow = this._matrix.get(src);
const targetRow = this._matrix.get(target);
diff --git a/packages/core/src/Sheets/Domain/Range.ts b/packages/core/src/Sheets/Domain/Range.ts
index 95118e27d4..fa2eff43fb 100644
--- a/packages/core/src/Sheets/Domain/Range.ts
+++ b/packages/core/src/Sheets/Domain/Range.ts
@@ -75,6 +75,18 @@ type IValueOptionsType = {
* @beta
*/
export class Range {
+ static foreach(
+ rangeData: IRangeData,
+ action: (row: number, column: number) => void
+ ): void {
+ const { startRow, startColumn, endRow, endColumn } = rangeData;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ action(i, j);
+ }
+ }
+ }
+
private _commandManager: CommandManager;
private _context: Context;
@@ -3638,12 +3650,7 @@ export class Range {
}
forEach(action: (row: number, column: number) => void): void {
- const { startRow, startColumn, endRow, endColumn } = this._rangeData;
- for (let i = startRow; i <= endRow; i++) {
- for (let j = startColumn; j <= endColumn; j++) {
- action(i, j);
- }
- }
+ Range.foreach(this._rangeData, action);
}
/**
diff --git a/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts b/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
index 127b30afeb..9275a4ff1b 100644
--- a/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
+++ b/packages/sheets-plugin-numfmt/src/Const/ACTION_NAMES.ts
@@ -1,3 +1,4 @@
export enum ACTION_NAMES {
SET_NUMFMT_COORDS_ACTION = 'SetNumfmtCoordsAction',
+ SET_NUMFMT_RANGE_ACTION = 'SetNumfmtRangeAction',
}
diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
index 9250e1b807..e04b307b9b 100644
--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
@@ -1,4 +1,4 @@
-import { Command, ObjectMatrixPrimitiveType, Plugin } from '@univer/core';
+import { Command, Range, IRangeData, ObjectMatrix, ObjectMatrixPrimitiveType, Plugin } from '@univer/core';
import { ACTION_NAMES } from '../Const';
import { NumfmtModel, NumfmtValue } from '../Model/NumfmtModel';
@@ -24,11 +24,28 @@ export class NumfmtController {
return this._model.getNumfmtValue(sheetId, row, column);
}
+ setNumfmtByRange(sheetId: string, numfmtRange: IRangeData, numfmtValue: string): void {
+ const numfmtMatrix = new ObjectMatrix<string>();
+ Range.foreach(numfmtRange, (row, column) => {
+ numfmtMatrix.setValue(row, column, numfmtValue);
+ });
+ const pluginContext = this._plugin.getContext();
+ const commandManager = pluginContext.getCommandManager();
+ const config = {
+ actionName: ACTION_NAMES.SET_NUMFMT_RANGE_ACTION,
+ sheetId,
+ numfmtMatrix,
+ numfmtValue,
+ };
+ const command = new Command(pluginContext.getWorkBook(), config);
+ commandManager.invoke(command);
+ }
+
setNumfmtByCoords(sheetId: string, row: number, column: number, numfmt: string): void {
const pluginContext = this._plugin.getContext();
const commandManager = pluginContext.getCommandManager();
const config = {
- actionName: ACTION_NAMES.SET_NUMFMT_ACTION,
+ actionName: ACTION_NAMES.SET_NUMFMT_COORDS_ACTION,
sheetId,
row,
column,
diff --git a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts
index ac095ea9a1..d3019252ea 100644
--- a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts
+++ b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtCoordsAction.ts
@@ -1,4 +1,5 @@
import { ActionBase, ActionObservers, IActionData, Workbook } from '@univer/core';
+import { SetNumfmtCoords } from '../Apply/SetNumfmtCoords';
export interface ISetNumfmtCoordsActionData extends IActionData {
row: number;
@@ -15,21 +16,22 @@ export class SetNumfmtCoordsAction extends ActionBase<ISetNumfmtCoordsActionData
};
this._oldActionData = {
...actionData,
+ numfmt: this.do(),
convertor: [],
};
this.do();
}
- redo(): void {
- // todo ...
+ redo(): string {
+ return SetNumfmtCoords(this.getWorkBook(), this._doActionData.sheetId, this._doActionData.row, this._doActionData.column, this._doActionData.numfmt);
}
- do(): void {
- this.redo();
+ do(): string {
+ return this.redo();
}
undo(): void {
- // todo ...
+ SetNumfmtCoords(this.getWorkBook(), this._oldActionData.sheetId, this._oldActionData.row, this._oldActionData.column, this._oldActionData.numfmt);
}
validate(): boolean {
diff --git a/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts
new file mode 100644
index 0000000000..56415ece34
--- /dev/null
+++ b/packages/sheets-plugin-numfmt/src/Model/Action/SetNumfmtRangeAction.ts
@@ -0,0 +1,37 @@
+import { ActionBase, ActionObservers, IActionData, ObjectMatrixPrimitiveType, Workbook } from '@univer/core/src';
+import { SetNumfmtRange } from '../Apply/SetNumfmtRange';
+
+export interface ISetNumfmtRangeActionData extends IActionData {
+ numfmtMatrix: ObjectMatrixPrimitiveType<string>;
+}
+
+export class SetNumfmtRangeAction extends ActionBase<ISetNumfmtRangeActionData, ISetNumfmtRangeActionData> {
+ constructor(actionData: ISetNumfmtRangeActionData, workbook: Workbook, observers: ActionObservers) {
+ super(actionData, workbook, observers);
+ this._doActionData = {
+ ...actionData,
+ convertor: [],
+ };
+ this._oldActionData = {
+ ...actionData,
+ numfmtMatrix: this.do(),
+ convertor: [],
+ };
+ }
+
+ do(): ObjectMatrixPrimitiveType<string> {
+ return this.redo();
+ }
+
+ redo(): ObjectMatrixPrimitiveType<string> {
+ return SetNumfmtRange(this.getWorkBook(), this._doActionData.sheetId, this._doActionData.numfmtMatrix);
+ }
+
+ undo(): void {
+ SetNumfmtRange(this.getWorkBook(), this._oldActionData.sheetId, this._oldActionData.numfmtMatrix);
+ }
+
+ validate(): boolean {
+ return false;
+ }
+}
diff --git a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmt.ts b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmt.ts
deleted file mode 100644
index 182ef0e322..0000000000
--- a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmt.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { Nullable, ObjectMatrix, Workbook } from '@univer/core';
-import { NumfmtPlugin } from '../../NumfmtPlugin';
-import { NUMFMT_PLUGIN_NAME } from '../../Const/PLUGIN_NAME';
-
-export function SetNumfmt(workbook: Workbook, sheetId: string, row: number, column: number, value: string) {
- const numfmtPlugin: Nullable<NumfmtPlugin> = workbook.getContext().getPluginManager().getPluginByName(NUMFMT_PLUGIN_NAME);
- if (numfmtPlugin) {
- const primitive = numfmtPlugin.getConfig(sheetId);
- const matrix = new ObjectMatrix(primitive);
- matrix.setValue(row, column, {
- numfmt: value,
- change: true,
- value: String(),
- color: String(),
- });
- }
-}
diff --git a/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts
new file mode 100644
index 0000000000..b1a62a0b5c
--- /dev/null
+++ b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtCoords.ts
@@ -0,0 +1,18 @@
+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
new file mode 100644
index 0000000000..5b84570ebd
--- /dev/null
+++ b/packages/sheets-plugin-numfmt/src/Model/Apply/SetNumfmtRange.ts
@@ -0,0 +1,21 @@
+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();
+}