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(formula): formula action extension add formulaData actoin
Dushusir2bec218954f6
diff --git a/packages/core/src/Command/CommandManager.ts b/packages/core/src/Command/CommandManager.tsindex a621bcbfae..dd65d3fa81 100644--- a/packages/core/src/Command/CommandManager.ts+++ b/packages/core/src/Command/CommandManager.ts@@ -85,6 +85,7 @@ export class CommandManager {const { _actionDataList, _unit, _actionList } = command;// const server = _workbook.getServer();this._actionExtensionManager.handle(_actionDataList);+_actionDataList.forEach((data) => {const ActionClass = CommandManager.getAction(data.actionName);const observers = CommandManager.getActionObservers();diff --git a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.tsindex 580975b538..32ee27a747 100644--- a/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts+++ b/packages/sheets-plugin-formula/src/Basic/Register/FormulaActionExtension.ts@@ -1,7 +1,9 @@import {+ ActionOperation,ACTION_NAMES,BaseActionExtension,BaseActionExtensionFactory,+ Command,ICellData,ISetRangeDataActionData,isFormulaString,@@ -12,14 +14,20 @@ import {Tools,} from '@univer/core';import { FormulaPlugin } from '../../FormulaPlugin';+import { ACTION_NAMES as PLUGIN_ACTION_NAMES } from '../Enum';export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataActionData, FormulaPlugin> {execute() {const engine = this._plugin.getFormulaEngine();- const formulaData = Tools.deepClone(this._plugin.getFormulaController().getDataModel().getFormulaData());+ const formulaController = this._plugin.getFormulaController();+ const formulaDataModel = formulaController.getDataModel();+ const formulaData = Tools.deepClone(formulaDataModel.getFormulaData());const unitId = this._plugin.getContext().getWorkBook().getUnitId();const sheetId = this.actionData.sheetId;const rangeData = this.actionData.rangeData;+ const commandManager = this._plugin.getContext().getCommandManager();++ const workBook = this._plugin.getContext().getWorkBook();let cellValue = this.actionData.cellValue;const cellData = new ObjectMatrix(formulaData[unitId][sheetId]);@@ -53,32 +61,54 @@ export class FormulaActionExtension extends BaseActionExtension<ISetRangeDataActcolumn: c,sheetId,});- }- const unitRange: IUnitRange[] = [- {- unitId,- sheetId,- rangeData,- },- ];+ const unitRange: IUnitRange[] = [+ {+ unitId,+ sheetId,+ rangeData,+ },+ ];- // cell.v = 55;- // cell.t = 1;- engine.execute(unitId, formulaData, undefined, undefined, unitRange).then((sheetData) => {- const cellCalculate = sheetData[sheetId].getValue(r, c);- cell.v = cellCalculate?.v;- cell.t = cellCalculate?.t;- });- }+ // cell.v = 55;+ // cell.t = 1;- // const setNumfmtRangeDataAction = {- // actionName: ACTION_NAMES,- // sheetId: this.actionData.sheetId,- // rangeData: this.actionData.rangeData,- // cellValue: formulaMatrix.toJSON(),- // };- // this.push(setNumfmtRangeDataAction);+ engine.execute(unitId, formulaData, formulaController.toInterpreterCalculateProps(), true, unitRange).then((sheetData) => {+ const cellCalculate = sheetData[sheetId].getValue(r, c);+ // cell.v = cellCalculate?.v;+ // cell.t = cellCalculate?.t;+ if (cellCalculate && cellCalculate.p) {+ delete cellCalculate.p;+ }++ const action: ISetRangeDataActionData = {+ actionName: ACTION_NAMES.SET_RANGE_DATA_ACTION,+ sheetId,+ rangeData: {+ startColumn: c,+ endColumn: c,+ startRow: r,+ endRow: r,+ },+ cellValue: {+ [r]: {+ [c]: cellCalculate,+ },+ },+ };++ const setFormulaDataAction = {+ actionName: PLUGIN_ACTION_NAMES.SET_FORMULA_RANGE_DATA_ACTION,+ sheetId: this.actionData.sheetId,+ rangeData: this.actionData.rangeData,+ formulaData,+ };+ const actionData = ActionOperation.make<ISetRangeDataActionData>(action).removeCollaboration().removeUndo().getAction();+ const command = new Command({ WorkBookUnit: workBook }, actionData, setFormulaDataAction);+ commandManager.invoke(command);+ });+ }+ }}}diff --git a/packages/sheets-plugin-formula/src/Model/Action/SetFormulaRangeDataAction.ts b/packages/sheets-plugin-formula/src/Model/Action/SetFormulaRangeDataAction.tsindex 597359549a..40f020dc46 100644--- a/packages/sheets-plugin-formula/src/Model/Action/SetFormulaRangeDataAction.ts+++ b/packages/sheets-plugin-formula/src/Model/Action/SetFormulaRangeDataAction.ts@@ -1,12 +1,12 @@-import { IFormulaData } from '@univer/base-formula-engine';-import { SheetActionBase, ActionObservers, ISheetActionData, ObjectMatrixPrimitiveType, Workbook } from '@univer/core';+import { FormulaDataType } from '@univer/base-formula-engine';+import { SheetActionBase, ActionObservers, ISheetActionData, Workbook } from '@univer/core';import { SetFormulaRangeData } from '../Apply/SetFormulaRangeData';export interface ISetFormulaRangeActionData extends ISheetActionData {- formulaData: IFormulaData;+ formulaData: FormulaDataType;}-export class SetFormulaRangeDataAction extends SheetActionBase<ISetFormulaRangeActionData, IFormulaData> {+export class SetFormulaRangeDataAction extends SheetActionBase<ISetFormulaRangeActionData, ISetFormulaRangeActionData, FormulaDataType> {constructor(actionData: ISetFormulaRangeActionData, workbook: Workbook, observers: ActionObservers) {super(actionData, workbook, observers);this._doActionData = {@@ -18,16 +18,20 @@ export class SetFormulaRangeDataAction extends SheetActionBase<ISetFormulaRangeA};}- do(): ObjectMatrixPrimitiveType<string> {+ do(): FormulaDataType {return this.redo();}- redo(): IFormulaData {- return SetFormulaRangeData();+ redo(): FormulaDataType {+ const { _workbook } = this;+ const { formulaData } = this._doActionData;+ return SetFormulaRangeData(_workbook, formulaData);}undo(): void {- SetFormulaRangeData();+ const { _workbook } = this;+ const { formulaData } = this._oldActionData;+ SetFormulaRangeData(_workbook, formulaData);}validate(): boolean {diff --git a/packages/sheets-plugin-formula/src/Model/Apply/SetFormulaRangeData.ts b/packages/sheets-plugin-formula/src/Model/Apply/SetFormulaRangeData.tsindex f8d64686cc..3d4a0e712d 100644--- a/packages/sheets-plugin-formula/src/Model/Apply/SetFormulaRangeData.ts+++ b/packages/sheets-plugin-formula/src/Model/Apply/SetFormulaRangeData.ts@@ -1,5 +1,16 @@-import { IFormulaData } from '@univer/base-formula-engine';+import { Tools, Workbook } from '@univer/core';+import { FormulaDataType } from '@univer/base-formula-engine';+import { FORMULA_PLUGIN_NAME } from '../../Basic';+import { FormulaPlugin } from '../../FormulaPlugin';-export function SetFormulaRangeData(): IFormulaData {- return {};+export function SetFormulaRangeData(workbook: Workbook, formulaData: FormulaDataType): FormulaDataType {+ const formulaDataModel = workbook.getContext().getPluginManager().getRequirePluginByName<FormulaPlugin>(FORMULA_PLUGIN_NAME).getFormulaController().getDataModel();++ // store old result+ const result = Tools.deepClone(formulaDataModel.getFormulaData());++ // update+ formulaDataModel.setFormulaData(formulaData);++ return result;}diff --git a/packages/sheets-plugin-formula/src/Model/FormulaDataModel.ts b/packages/sheets-plugin-formula/src/Model/FormulaDataModel.tsindex efd6ef9c3d..56802bc07d 100644--- a/packages/sheets-plugin-formula/src/Model/FormulaDataModel.ts+++ b/packages/sheets-plugin-formula/src/Model/FormulaDataModel.ts@@ -25,4 +25,8 @@ export class FormulaDataModel {getFormulaData() {return this._formulaData;}++ setFormulaData(value: FormulaDataType) {+ this._formulaData = value;+ }}diff --git a/packages/sheets-plugin-formula/src/index.ts b/packages/sheets-plugin-formula/src/index.tsindex 684586a557..c8d3bd820a 100644--- a/packages/sheets-plugin-formula/src/index.ts+++ b/packages/sheets-plugin-formula/src/index.ts@@ -1,3 +1,6 @@+/* eslint-disable import/first */+import './Model/RegisterAction';+export * from './FormulaPlugin';export * from './Basic/Const/DEFAULT_FORMULA_DATA';export * from './Basic/Const/PLUGIN_NAME';