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: numfmt bug

jerry7e468e927f01
diff --git a/packages/base-sheets/src/Controller/ToolBarController.ts b/packages/base-sheets/src/Controller/ToolBarController.ts
index 38b12a0bb2..7ee74e458e 100644
--- a/packages/base-sheets/src/Controller/ToolBarController.ts
+++ b/packages/base-sheets/src/Controller/ToolBarController.ts
@@ -484,7 +484,6 @@ export class ToolBarController {
this._toolBarComponent = component;
this.resetToolBarList();
});
-
this._plugin.getObserver('onLineColorDidMountObservable')?.add((component) => {
//初始化视图
this._lineColor = component;
diff --git a/packages/core/src/Sheets/Action/SetRangeDataAction.ts b/packages/core/src/Sheets/Action/SetRangeDataAction.ts
index 475f708b39..63788a9e0c 100644
--- a/packages/core/src/Sheets/Action/SetRangeDataAction.ts
+++ b/packages/core/src/Sheets/Action/SetRangeDataAction.ts
@@ -58,6 +58,7 @@ export class SetRangeDataAction extends SheetActionBase<
this._doActionData.options
);
+ debugger;
this._observers.notifyObservers({
type: ActionType.REDO,
data: this._doActionData,
diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
index c6b3157903..665d1c77fe 100644
--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts
@@ -1,58 +1,71 @@
-import { Command, Range, IRangeData, ObjectMatrix, ObjectMatrixPrimitiveType, PLUGIN_NAMES, ACTION_NAMES } from '@univer/core';
+import { Command, Range, IRangeData, ObjectMatrix, ObjectMatrixPrimitiveType, PLUGIN_NAMES, ACTION_NAMES as CORE_ACTION_NAME } from '@univer/core';
import { BaseComponentRender } from '@univer/base-component';
import { IToolBarItemProps, SheetPlugin } from '@univer/base-sheets';
-import { IActionData } from '@univer/core/src/Command/ActionBase';
+import { numfmt } from '@univer/base-numfmt-engine';
+import { ACTION_NAMES } from '../Basic/Enum';
+import { DEFAULT_DATA, NUMFMT_PLUGIN_NAME, NumftmConfig } from '../Basic/Const';
import { NumfmtModel } from '../Model/NumfmtModel';
-import { DEFAULT_DATA } from '../Basic/Const/DEFAULT_DATA';
-import { NUMFMT_PLUGIN_NAME, NumftmConfig } from '../Basic/Const';
import { NumfmtPlugin } from '../NumfmtPlugin';
+
import styles from '../View/UI/index.module.less';
export class NumfmtController {
- protected _plugin: NumfmtPlugin;
+ protected _sheetPlugin: SheetPlugin;
protected _model: NumfmtModel;
- private _sheetPlugin: SheetPlugin;
+ protected _numfmtList: IToolBarItemProps;
- private _numfmtList: IToolBarItemProps;
+ protected _plugin: NumfmtPlugin;
- private _render: BaseComponentRender;
+ protected _render: BaseComponentRender;
constructor(plugin: NumfmtPlugin) {
this._model = new NumfmtModel();
this._plugin = plugin;
this._sheetPlugin = this._plugin.getContext().getPluginManager().getPluginByName<SheetPlugin>(PLUGIN_NAMES.SPREADSHEET)!;
- const wrappingRangeData = (row: number, col: number): IRangeData => ({
- startRow: row,
- endRow: row,
- startColumn: col,
- endColumn: col,
- });
const executeFormatter = (type: string): void => {
+ const manager = this._sheetPlugin.getSelectionManager();
const workbook = this._plugin.getContext().getWorkBook();
- const selectManager = this._sheetPlugin.getSelectionManager();
- const selectRangeList = selectManager.getActiveRangeList();
- if (selectRangeList == null) {
+ const activeSheet = workbook.getActiveSheet();
+ const activeRange = manager.getActiveRangeList();
+ const cellMatrix = activeSheet.getCellMatrix();
+ if (activeRange == null) {
return;
}
- selectRangeList.getRangeList().forEach((value) => {
- const actions: IActionData[] = [];
- Range.foreach(value, (row, col) => {
- const setRangeAction = {
- actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
- cellValue: { m: type },
- rangeData: wrappingRangeData(row, col),
- };
- actions.push(setRangeAction);
+ // update cell data
+ activeRange.getRangeList().forEach((range) => {
+ let matrix = new ObjectMatrix();
+ Range.foreach(range, (row, col) => {
+ const cell = cellMatrix.getValue(row, col);
+ if (cell) {
+ const formatter = numfmt(type);
+ matrix.setValue(row, col, { v: cell.v, m: formatter(cell.v) });
+ }
+ });
+ const setRangeDataAction = {
+ sheetId: activeSheet.getSheetId(),
+ actionName: CORE_ACTION_NAME.SET_RANGE_DATA_ACTION,
+ rangeData: range,
+ cellValue: matrix.getData(),
+ };
+ const cmd = new Command({ WorkBookUnit: workbook }, setRangeDataAction);
+ workbook.getCommandManager().invoke(cmd);
+ });
+ // update numfmt data
+ activeRange.getRangeList().forEach((range) => {
+ let matrix = new ObjectMatrix();
+ Range.foreach(range, (row, col) => {
+ matrix.setValue(row, col, type);
});
- new Command(
- {
- WorkBookUnit: workbook,
- },
- ...actions
- );
+ const setNumfmtRangeDataAction = {
+ sheetId: activeSheet.getSheetId(),
+ actionName: ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,
+ numfmtMatrix: matrix.getData(),
+ };
+ const cmd = new Command({ WorkBookUnit: workbook }, setNumfmtRangeDataAction);
+ cmd.invoke();
});
};
const CHILDREN_DATA = DEFAULT_DATA.map((item, index) => ({
@@ -168,7 +181,7 @@ export class NumfmtController {
const pluginContext = this._plugin.getContext();
const commandManager = pluginContext.getCommandManager();
const config = {
- actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
+ actionName: CORE_ACTION_NAME.SET_RANGE_DATA_ACTION,
sheetId,
rangeData: numfmtRange,
cellValue: numfmtMatrix.toJSON(),
@@ -189,7 +202,7 @@ export class NumfmtController {
const pluginContext = this._plugin.getContext();
const commandManager = pluginContext.getCommandManager();
const config = {
- actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,
+ actionName: CORE_ACTION_NAME.SET_RANGE_DATA_ACTION,
sheetId,
rangeData: numfmtRange,
cellValue: numfmtMatrix.toJSON(),
diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts
index 11bdb6696e..9e7aef4ed6 100644
--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts
+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts
@@ -1,8 +1,7 @@
import { ComponentChildren } from '@univer/base-component';
import { SheetPlugin } from '@univer/base-sheets';
import { PLUGIN_NAMES } from '@univer/core';
-import { NUMBERFORMAT, NUMFMT_PLUGIN_NAME } from '../Basic/Const';
-import { CURRENCYDETAIL, DATEFMTLISG } from '../Basic/Const/FORMATDETAIL';
+import { NUMBERFORMAT, NUMFMT_PLUGIN_NAME, CURRENCYDETAIL, DATEFMTLISG } from '../Basic/Const';
import { NumfmtPlugin } from '../NumfmtPlugin';
import { FormatContent } from '../View/UI/FormatContent';
import { NumfmtModal } from '../View/UI/NumfmtModal';
@@ -38,10 +37,9 @@ export class NumfmtModalController {
private _modalData: ModalDataProps[];
constructor(plugin: NumfmtPlugin) {
+ const locale = plugin.getContext().getLocale();
this._plugin = plugin;
this._sheetPlugin = this._plugin.getContext().getPluginManager().getPluginByName<SheetPlugin>(PLUGIN_NAMES.SPREADSHEET)!;
- const locale = this._plugin.getContext().getLocale();
-
this._modalData = [
{
name: 'currency',
@@ -120,16 +118,13 @@ export class NumfmtModalController {
},
},
];
-
this._initRegisterComponent();
-
this._initialize();
}
private _initialize() {
this._plugin.getObserver('onNumfmtModalDidMountObservable')!.add((component) => {
this._numfmtModal = component;
-
this.resetModalData();
});
}
@@ -141,20 +136,17 @@ export class NumfmtModalController {
resetContentData(data: any[]) {
const locale = this._plugin.getContext().getLocale();
-
for (let i = 0; i < data.length; i++) {
if (data[i].locale) {
data[i].label = locale.get(data[i].locale);
}
}
-
return data;
}
// 渲染所需数据
resetModalData() {
const locale = this._plugin.getContext().getLocale();
-
this._modalData.forEach((item) => {
item.title = locale.get(item.locale);
if (item.group && item.group.length) {
@@ -163,7 +155,6 @@ export class NumfmtModalController {
});
}
});
-
this._numfmtModal.setModal(this._modalData);
}
diff --git a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
index 89f5c1f16f..20d95c0ba3 100644
--- a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
+++ b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts
@@ -9,7 +9,7 @@ import { NumfmtActionExtensionFactory } from './Basic/Register/NumfmtActionExten
export class NumfmtPlugin extends Plugin<NumfmtPluginObserve, SheetContext> {
protected _controller: NumfmtController;
- private _NumfmtModalController: NumfmtModalController;
+ protected _numfmtModalController: NumfmtModalController;
protected _numfmtActionExtensionFactory: NumfmtActionExtensionFactory;
@@ -26,7 +26,7 @@ export class NumfmtPlugin extends Plugin<NumfmtPluginObserve, SheetContext> {
});
this._numfmtActionExtensionFactory = new NumfmtActionExtensionFactory(this);
- this._NumfmtModalController = new NumfmtModalController(this);
+ this._numfmtModalController = new NumfmtModalController(this);
this._controller = new NumfmtController(this);
const actionRegister = this.context.getCommandManager().getActionExtensionManager().getRegister();
actionRegister.add(this._numfmtActionExtensionFactory);
@@ -51,6 +51,6 @@ export class NumfmtPlugin extends Plugin<NumfmtPluginObserve, SheetContext> {
}
getNumfmtModalController() {
- return this._NumfmtModalController;
+ return this._numfmtModalController;
}
}