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(formula): offset array formula (#1040)

Dushusiree470f1fe311
diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
index c440858df2..578102422f 100644
--- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
+++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
@@ -27,7 +27,12 @@ import {
SetFormulaCalculationStartMutation,
} from '@univerjs/engine-formula';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
-import { SetBorderCommand, SetRangeValuesMutation, SetStyleCommand } from '@univerjs/sheets';
+import {
+ ClearSelectionFormatCommand,
+ SetBorderCommand,
+ SetRangeValuesMutation,
+ SetStyleCommand,
+} from '@univerjs/sheets';
import { IActiveDirtyManagerService } from '../services/active-dirty-manager.service';
@@ -85,7 +90,8 @@ export class TriggerCalculationController extends Disposable {
if (
(options && options.onlyLocal === true) ||
params.trigger === SetStyleCommand.id ||
- params.trigger === SetBorderCommand.id
+ params.trigger === SetBorderCommand.id ||
+ params.trigger === ClearSelectionFormatCommand.id
) {
return;
}
diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.ts
index dacae1e530..d25554583c 100644
--- a/packages/sheets-formula/src/controllers/update-formula.controller.ts
+++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts
@@ -14,7 +14,15 @@
* limitations under the License.
*/
-import type { ICellData, ICommandInfo, IExecutionOptions, IRange, IUnitRange, Nullable } from '@univerjs/core';
+import type {
+ ICellData,
+ ICommandInfo,
+ IExecutionOptions,
+ IMutationCommonParams,
+ IRange,
+ IUnitRange,
+ Nullable,
+} from '@univerjs/core';
import {
deserializeRangeWithSheet,
Direction,
@@ -27,8 +35,10 @@ import {
OnLifecycle,
RANGE_TYPE,
Rectangle,
+ RedoCommand,
serializeRangeToRefString,
Tools,
+ UndoCommand,
} from '@univerjs/core';
import type { IFormulaData, IFormulaDataItem, ISequenceNode, IUnitSheetNameMap } from '@univerjs/engine-formula';
import {
@@ -48,6 +58,7 @@ import type {
IInsertSheetMutationParams,
IMoveColsCommandParams,
IMoveRangeCommandParams,
+ IMoveRangeMutationParams,
IMoveRowsCommandParams,
InsertRangeMoveDownCommandParams,
InsertRangeMoveRightCommandParams,
@@ -57,6 +68,7 @@ import type {
ISetWorksheetNameCommandParams,
} from '@univerjs/sheets';
import {
+ ClearSelectionFormatCommand,
DeleteRangeMoveLeftCommand,
DeleteRangeMoveUpCommand,
EffectRefRangId,
@@ -76,6 +88,7 @@ import {
InsertSheetMutation,
MoveColsCommand,
MoveRangeCommand,
+ MoveRangeMutation,
MoveRowsCommand,
RemoveColCommand,
RemoveRowCommand,
@@ -91,7 +104,9 @@ import {
} from '@univerjs/sheets';
import { Inject, Injector } from '@wendellhu/redi';
-import { offsetArrayFormula, offsetFormula, removeFormulaData } from './utils';
+import type { IRefRangeWithPosition } from './utils/offset-formula-data';
+import { offsetArrayFormula, offsetFormula, removeFormulaData } from './utils/offset-formula-data';
+import { handleRedoUndoMoveRange } from './utils/redo-undo-formula-data';
interface IUnitRangeWithOffset extends IUnitRange {
refOffsetX: number;
@@ -166,7 +181,8 @@ export class UpdateFormulaController extends Disposable {
if (
(options && options.onlyLocal === true) ||
params.trigger === SetStyleCommand.id ||
- params.trigger === SetBorderCommand.id
+ params.trigger === SetBorderCommand.id ||
+ params.trigger === ClearSelectionFormatCommand.id
) {
return;
}
@@ -175,15 +191,54 @@ export class UpdateFormulaController extends Disposable {
this._handleRemoveSheetMutation(command.params as IRemoveSheetMutationParams);
} else if (command.id === InsertSheetMutation.id) {
this._handleInsertSheetMutation(command.params as IInsertSheetMutationParams);
+ } else if (
+ (command.params as IMutationCommonParams)?.trigger === UndoCommand.id ||
+ (command.params as IMutationCommonParams)?.trigger === RedoCommand.id
+ ) {
+ this._handleRedoUndo(command);
}
})
);
}
+ private _handleRedoUndo(command: ICommandInfo) {
+ const { id } = command;
+ const formulaData = this._formulaDataModel.getFormulaData();
+ const arrayFormulaRange = this._formulaDataModel.getArrayFormulaRange();
+ const arrayFormulaCellData = this._formulaDataModel.getArrayFormulaCellData();
+
+ switch (id) {
+ case MoveRangeMutation.id:
+ handleRedoUndoMoveRange(
+ command as ICommandInfo<IMoveRangeMutationParams>,
+ formulaData,
+ arrayFormulaRange,
+ arrayFormulaCellData
+ );
+ break;
+
+ // TODO:@Dushusir handle other mutations
+ }
+
+ this._commandService.executeCommand(SetFormulaDataMutation.id, {
+ formulaData,
+ });
+ this._commandService.executeCommand(SetArrayFormulaDataMutation.id, {
+ arrayFormulaRange,
+ arrayFormulaCellData,
+ });
+ }
+
private _handleSetRangeValuesMutation(params: ISetRangeValuesMutationParams, options?: IExecutionOptions) {
const { subUnitId: sheetId, unitId, cellValue } = params;
- if ((options && options.onlyLocal === true) || cellValue == null) {
+ if (
+ (options && options.onlyLocal === true) ||
+ params.trigger === SetStyleCommand.id ||
+ params.trigger === SetBorderCommand.id ||
+ params.trigger === ClearSelectionFormatCommand.id ||
+ cellValue == null
+ ) {
return;
}
@@ -314,7 +369,11 @@ export class UpdateFormulaController extends Disposable {
let oldFormulaData = this._formulaDataModel.getFormulaData();
// change formula reference
- const formulaData = this._getFormulaReferenceMoveInfo(oldFormulaData, unitSheetNameMap, result);
+ const { newFormulaData: formulaData, refRanges } = this._getFormulaReferenceMoveInfo(
+ oldFormulaData,
+ unitSheetNameMap,
+ result
+ );
const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
const unitId = workbook.getUnitId();
@@ -325,15 +384,25 @@ export class UpdateFormulaController extends Disposable {
const arrayFormulaRange = this._formulaDataModel.getArrayFormulaRange();
const arrayFormulaCellData = this._formulaDataModel.getArrayFormulaCellData();
- let offsetArrayFormulaRange = offsetFormula(arrayFormulaRange, command, unitId, sheetId, selections);
- offsetArrayFormulaRange = offsetArrayFormula(offsetArrayFormulaRange, unitId, sheetId);
+ // First use arrayFormulaCellData and the original arrayFormulaRange to calculate the offset of arrayFormulaCellData, otherwise the offset of arrayFormulaRange will be inaccurate.
const offsetArrayFormulaCellData = offsetFormula(
arrayFormulaCellData,
command,
unitId,
sheetId,
- selections
+ selections,
+ arrayFormulaRange,
+ refRanges
);
+ let offsetArrayFormulaRange = offsetFormula(
+ arrayFormulaRange,
+ command,
+ unitId,
+ sheetId,
+ selections,
+ arrayFormulaRange
+ );
+ offsetArrayFormulaRange = offsetArrayFormula(offsetArrayFormulaRange, command, unitId, sheetId);
// Synchronous to the worker thread
this._commandService.executeCommand(
@@ -466,7 +535,7 @@ export class UpdateFormulaController extends Disposable {
const { range, unitId, subUnitId } = params;
return {
type: FormulaReferenceMoveType.InsertRow,
- ranges: [range],
+ range,
unitId,
sheetId: subUnitId,
};
@@ -479,7 +548,7 @@ export class UpdateFormulaController extends Disposable {
const { range, unitId, subUnitId } = params;
return {
type: FormulaReferenceMoveType.InsertColumn,
- ranges: [range],
+ range,
unitId,
sheetId: subUnitId,
};
@@ -666,14 +735,17 @@ export class UpdateFormulaController extends Disposable {
unitSheetNameMap: IUnitSheetNameMap,
formulaReferenceMoveParam: IFormulaReferenceMoveParam
) {
- if (formulaData == null) return {};
+ if (!Tools.isDefine(formulaData)) return { newFormulaData: {}, refRanges: [] };
const formulaDataKeys = Object.keys(formulaData);
- if (formulaDataKeys.length === 0) return {};
+ if (formulaDataKeys.length === 0) return { newFormulaData: {}, refRanges: [] };
const newFormulaData: IFormulaData = {};
+ // Return all reference ranges. If the operation affects the reference range, you need to clear arrayFormulaRange and arrayFormulaCellData.
+ const refRanges: IRefRangeWithPosition[] = [];
+
for (const unitId of formulaDataKeys) {
const sheetData = formulaData[unitId];
@@ -683,7 +755,7 @@ export class UpdateFormulaController extends Disposable {
const sheetDataKeys = Object.keys(sheetData);
- if (newFormulaData[unitId] == null) {
+ if (!Tools.isDefine(newFormulaData[unitId])) {
newFormulaData[unitId] = {};
}
@@ -716,6 +788,9 @@ export class UpdateFormulaController extends Disposable {
const { range, sheetName, unitId: sequenceUnitId } = sequenceGrid;
+ // store ref range
+ refRanges.push({ row, column, range });
+
const mapUnitId =
sequenceUnitId == null || sequenceUnitId.length === 0 ? unitId : sequenceUnitId;
@@ -798,7 +873,7 @@ export class UpdateFormulaController extends Disposable {
}
}
- return newFormulaData;
+ return { newFormulaData, refRanges };
}
private _getNewRangeByMoveParam(
diff --git a/packages/sheets-formula/src/controllers/__tests__/utils.spec.ts b/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
similarity index 55%
rename from packages/sheets-formula/src/controllers/__tests__/utils.spec.ts
rename to packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
index 609f887e78..6fd5ab6759 100644
--- a/packages/sheets-formula/src/controllers/__tests__/utils.spec.ts
+++ b/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
@@ -38,13 +38,13 @@ import {
} from '@univerjs/sheets';
import { describe, expect, it } from 'vitest';
-import { offsetArrayFormula, offsetFormula, removeFormulaData } from '../utils';
+import { offsetArrayFormula, offsetFormula, removeFormulaData } from '../offset-formula-data';
const cellToRange = (row: number, col: number) =>
({ startRow: row, endRow: row, startColumn: col, endColumn: col }) as IRange;
-describe('utils test', () => {
+describe('Utils offset formula data test', () => {
describe('function offsetFormula', () => {
- it('move range', () => {
+ it('move range normal formula', () => {
const unitId = 'workbook-01';
const sheetId = 'sheet-0011';
const formulaData = {
@@ -93,6 +93,187 @@ describe('utils test', () => {
},
});
});
+ it('move range array formula with first cell', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': {
+ v: '1',
+ },
+ },
+ '1': {
+ '3': {
+ v: '2',
+ },
+ },
+ '2': {
+ '3': {
+ v: '3',
+ },
+ },
+ },
+ },
+ };
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': {
+ startRow: 0,
+ startColumn: 3,
+ endRow: 2,
+ endColumn: 3,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: MoveRangeCommand.id,
+ params: {
+ fromRange: {
+ startRow: 0,
+ startColumn: 3,
+ endRow: 0,
+ endColumn: 3,
+ },
+ toRange: {
+ startRow: 3,
+ startColumn: 3,
+ endRow: 3,
+ endColumn: 3,
+ },
+ },
+ };
+
+ const newFormulaData = offsetFormula(
+ arrayFormulaCellData,
+ command,
+ unitId,
+ sheetId,
+ null,
+ arrayFormulaRange
+ );
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': null,
+ },
+ '1': {
+ '3': null,
+ },
+ '2': {
+ '3': null,
+ },
+ '3': {
+ '3': {
+ v: '1',
+ },
+ },
+ },
+ },
+ });
+ });
+ it('move range array formula with not first cell', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': {
+ v: '1',
+ },
+ },
+ '1': {
+ '3': {
+ v: '2',
+ },
+ },
+ '2': {
+ '3': {
+ v: '3',
+ },
+ },
+ '3': {
+ '3': {
+ v: '4',
+ },
+ },
+ },
+ },
+ };
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': {
+ startRow: 0,
+ startColumn: 3,
+ endRow: 2,
+ endColumn: 3,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: MoveRangeCommand.id,
+ params: {
+ fromRange: {
+ startRow: 1,
+ startColumn: 3,
+ endRow: 1,
+ endColumn: 3,
+ },
+ toRange: {
+ startRow: 3,
+ startColumn: 3,
+ endRow: 3,
+ endColumn: 3,
+ },
+ },
+ };
+
+ const newFormulaData = offsetFormula(
+ arrayFormulaCellData,
+ command,
+ unitId,
+ sheetId,
+ null,
+ arrayFormulaRange
+ );
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '3': {
+ v: '1',
+ },
+ },
+ '1': {
+ '3': {
+ v: '2',
+ },
+ },
+ '2': {
+ '3': {
+ v: '3',
+ },
+ },
+ '3': {
+ '3': null,
+ },
+ },
+ },
+ });
+ });
it('move rows', () => {
const unitId = 'workbook-01';
const sheetId = 'sheet-0011';
@@ -295,7 +476,6 @@ describe('utils test', () => {
f: '=SUM(A1)',
},
},
- // '1': {},
'2': {
'3': {
f: '=SUM(A3)',
@@ -305,6 +485,155 @@ describe('utils test', () => {
},
});
});
+ it('insert row, handle arrayFormulaCellData, with arrayFormulaRange and refRanges', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ v: 1,
+ t: 2,
+ },
+ },
+ '5': {
+ '0': {
+ v: 2,
+ t: 2,
+ },
+ },
+ '6': {
+ '0': {
+ v: 3,
+ t: 2,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: InsertRowCommand.id,
+ params: {
+ unitId,
+ subUnitId: sheetId,
+ direction: 0,
+ range: {
+ startRow: 3,
+ endRow: 3,
+ startColumn: 0,
+ endColumn: 19,
+ },
+ cellValue: {},
+ },
+ };
+
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ startRow: 4,
+ startColumn: 0,
+ endRow: 6,
+ endColumn: 0,
+ },
+ },
+ },
+ },
+ };
+
+ const refRanges = [
+ {
+ row: 4,
+ column: 0,
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 2,
+ endColumn: 0,
+ startAbsoluteRefType: 0,
+ endAbsoluteRefType: 0,
+ },
+ },
+ ];
+
+ const newFormulaData = offsetFormula(
+ arrayFormulaCellData,
+ command,
+ unitId,
+ sheetId,
+ null,
+ arrayFormulaRange,
+ refRanges
+ );
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': null,
+ },
+ '5': {
+ '0': null,
+ },
+ '6': {
+ '0': null,
+ },
+ },
+ },
+ });
+ });
+ it('insert row, handle arrayFormulaRange', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ startRow: 4,
+ startColumn: 0,
+ endRow: 6,
+ endColumn: 0,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: InsertRowCommand.id,
+ params: {
+ unitId,
+ subUnitId: sheetId,
+ direction: 0,
+ range: {
+ startRow: 3,
+ endRow: 3,
+ startColumn: 0,
+ endColumn: 19,
+ },
+ cellValue: {},
+ },
+ };
+ const newFormulaData = offsetFormula(arrayFormulaRange, command, unitId, sheetId, null, arrayFormulaRange);
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': null,
+ },
+ '5': {
+ '0': null,
+ },
+ '6': {
+ '0': null,
+ },
+ },
+ },
+ });
+ });
it('insert column', () => {
const unitId = 'workbook-01';
const sheetId = 'sheet-0011';
@@ -499,7 +828,149 @@ describe('utils test', () => {
f: '=SUM(A2)',
},
},
- // '2': {},
+ },
+ },
+ });
+ });
+ it('remove row, handle arrayFormulaCellData, with arrayFormulaRange and refRanges', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ v: 1,
+ t: 2,
+ },
+ },
+ '5': {
+ '0': {
+ v: 2,
+ t: 2,
+ },
+ },
+ '6': {
+ '0': {
+ v: 3,
+ t: 2,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: RemoveRowCommand.id,
+ params: {
+ range: {
+ startRow: 1,
+ startColumn: 0,
+ endRow: 1,
+ endColumn: 19,
+ rangeType: 1,
+ },
+ },
+ };
+
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ startRow: 4,
+ startColumn: 0,
+ endRow: 6,
+ endColumn: 0,
+ },
+ },
+ },
+ },
+ };
+
+ const refRanges = [
+ {
+ row: 4,
+ column: 0,
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 2,
+ endColumn: 0,
+ startAbsoluteRefType: 0,
+ endAbsoluteRefType: 0,
+ },
+ },
+ ];
+
+ const newFormulaData = offsetFormula(
+ arrayFormulaCellData,
+ command,
+ unitId,
+ sheetId,
+ null,
+ arrayFormulaRange,
+ refRanges
+ );
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': null,
+ },
+ '5': {
+ '0': null,
+ },
+ '6': {
+ '0': null,
+ },
+ },
+ },
+ });
+ });
+ it('remove row, handle arrayFormulaRange', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': {
+ startRow: 4,
+ startColumn: 0,
+ endRow: 6,
+ endColumn: 0,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: RemoveRowCommand.id,
+ params: {
+ range: {
+ startRow: 1,
+ startColumn: 0,
+ endRow: 1,
+ endColumn: 19,
+ rangeType: 1,
+ },
+ },
+ };
+ const newFormulaData = offsetFormula(arrayFormulaRange, command, unitId, sheetId, null, arrayFormulaRange);
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '4': {
+ '0': null,
+ },
+ '5': {
+ '0': null,
+ },
+ '6': {
+ '0': null,
+ },
},
},
});
@@ -550,6 +1021,137 @@ describe('utils test', () => {
},
});
});
+ it('remove column, handle arrayFormulaCellData, with arrayFormulaRange and refRanges', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '4': {
+ v: 1,
+ t: 2,
+ },
+ '5': {
+ v: 2,
+ t: 2,
+ },
+ '6': {
+ v: 3,
+ t: 2,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: RemoveColCommand.id,
+ params: {
+ range: {
+ startRow: 0,
+ startColumn: 1,
+ endRow: 999,
+ endColumn: 1,
+ rangeType: 2,
+ },
+ },
+ };
+
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '4': {
+ startRow: 0,
+ startColumn: 4,
+ endRow: 0,
+ endColumn: 6,
+ },
+ },
+ },
+ },
+ };
+
+ const refRanges = [
+ {
+ row: 0,
+ column: 4,
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 0,
+ endColumn: 2,
+ startAbsoluteRefType: 0,
+ endAbsoluteRefType: 0,
+ },
+ },
+ ];
+
+ const newFormulaData = offsetFormula(
+ arrayFormulaCellData,
+ command,
+ unitId,
+ sheetId,
+ null,
+ arrayFormulaRange,
+ refRanges
+ );
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '4': null,
+ '5': null,
+ '6': null,
+ },
+ },
+ },
+ });
+ });
+ it('remove column, handle arrayFormulaRange', () => {
+ const unitId = 'workbook-01';
+ const sheetId = 'sheet-0011';
+ const arrayFormulaRange = {
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '4': {
+ startRow: 0,
+ startColumn: 4,
+ endRow: 0,
+ endColumn: 6,
+ },
+ },
+ },
+ },
+ };
+
+ const command = {
+ id: RemoveColCommand.id,
+ params: {
+ range: {
+ startRow: 0,
+ startColumn: 1,
+ endRow: 999,
+ endColumn: 1,
+ rangeType: 2,
+ },
+ },
+ };
+ const newFormulaData = offsetFormula(arrayFormulaRange, command, unitId, sheetId, null, arrayFormulaRange);
+ expect(newFormulaData).toStrictEqual({
+ [unitId]: {
+ [sheetId]: {
+ '0': {
+ '4': null,
+ '5': null,
+ '6': null,
+ },
+ },
+ },
+ });
+ });
it('delete range move up', () => {
const unitId = 'workbook-01';
const sheetId = 'sheet-0011';
@@ -675,7 +1277,20 @@ describe('utils test', () => {
},
};
- const newArrayFormulaData = offsetArrayFormula(arrayFormulaData, unitId, sheetId);
+ const command = {
+ id: DeleteRangeMoveLeftCommand.id,
+ params: {
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 0,
+ endColumn: 0,
+ rangeType: 0,
+ },
+ },
+ };
+
+ const newArrayFormulaData = offsetArrayFormula(arrayFormulaData, command, unitId, sheetId);
expect(newArrayFormulaData).toStrictEqual({
[unitId]: {
[sheetId]: {
diff --git a/packages/sheets-formula/src/controllers/utils/__tests__/redo-undo-formula-data.spec.ts b/packages/sheets-formula/src/controllers/utils/__tests__/redo-undo-formula-data.spec.ts
new file mode 100644
index 0000000000..d5c2ab23d8
--- /dev/null
+++ b/packages/sheets-formula/src/controllers/utils/__tests__/redo-undo-formula-data.spec.ts
@@ -0,0 +1,165 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { UndoCommand } from '@univerjs/core';
+import { MoveRangeMutation } from '@univerjs/sheets';
+import { describe, expect, it } from 'vitest';
+
+import { handleRedoUndoMoveRange } from '../redo-undo-formula-data';
+
+describe('Utils redo undo formula data test', () => {
+ describe('function handleRedoUndoMoveRange', () => {
+ it('move range array formula with first cell, undo', () => {
+ const unitId = 'workbook-01';
+ const subUnitId = 'sheet1';
+
+ const command = {
+ id: MoveRangeMutation.id,
+ type: 2,
+ params: {
+ from: {
+ value: {
+ '4': {
+ '0': {
+ f: '=A1:A3',
+ v: 1,
+ t: 2,
+ },
+ },
+ },
+ subUnitId,
+ },
+ to: {
+ value: {
+ '4': {
+ '2': null,
+ },
+ },
+ subUnitId,
+ },
+ unitId,
+ trigger: UndoCommand.id,
+ },
+ };
+
+ const formulaData = {
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '2': {
+ f: '=A1:A3',
+ },
+ },
+ },
+ },
+ };
+
+ const arrayFormulaRange = {
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '2': {
+ startRow: 4,
+ startColumn: 2,
+ endRow: 6,
+ endColumn: 2,
+ },
+ },
+ },
+ },
+ };
+
+ const arrayFormulaCellData = {
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '0': null,
+ '2': {
+ v: 1,
+ t: 2,
+ },
+ },
+ '5': {
+ '0': null,
+ '2': {
+ v: 2,
+ t: 2,
+ },
+ },
+ '6': {
+ '0': null,
+ '2': {
+ v: 3,
+ t: 2,
+ },
+ },
+ },
+ },
+ };
+
+ handleRedoUndoMoveRange(command, formulaData, arrayFormulaRange, arrayFormulaCellData);
+
+ expect(formulaData).toStrictEqual({
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '0': {
+ f: '=A1:A3',
+ },
+ },
+ },
+ },
+ });
+ expect(arrayFormulaRange).toStrictEqual({
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '0': {
+ startRow: 4,
+ startColumn: 2,
+ endRow: 6,
+ endColumn: 2,
+ },
+ '2': null,
+ },
+ },
+ },
+ });
+
+ expect(arrayFormulaCellData).toStrictEqual({
+ [unitId]: {
+ [subUnitId]: {
+ '4': {
+ '0': {
+ v: 1,
+ t: 2,
+ },
+ '2': null,
+ },
+ '5': {
+ '0': null,
+ '2': null,
+ },
+ '6': {
+ '0': null,
+ '2': null,
+ },
+ },
+ },
+ });
+ });
+ });
+});
diff --git a/packages/sheets-formula/src/controllers/utils.ts b/packages/sheets-formula/src/controllers/utils/offset-formula-data.ts
similarity index 55%
rename from packages/sheets-formula/src/controllers/utils.ts
rename to packages/sheets-formula/src/controllers/utils/offset-formula-data.ts
index aa89d3030f..0c68f40358 100644
--- a/packages/sheets-formula/src/controllers/utils.ts
+++ b/packages/sheets-formula/src/controllers/utils/offset-formula-data.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICommandInfo, IObjectMatrixPrimitiveType, Nullable } from '@univerjs/core';
+import type { ICommandInfo, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
import { Dimension, ObjectMatrix, RANGE_TYPE } from '@univerjs/core';
import type { IArrayFormulaRangeType } from '@univerjs/engine-formula';
import type {
@@ -50,22 +50,31 @@ interface IFormulaDataGenerics<T> {
[unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<T> }>;
}
+export interface IRefRangeWithPosition {
+ row: number;
+ column: number;
+ range: IRange;
+}
+
export function offsetFormula<T>(
formulaData: IFormulaDataGenerics<T>,
command: ICommandInfo,
unitId: string,
sheetId: string,
- selections?: Readonly<Nullable<ISelectionWithStyle[]>>
+ selections?: Readonly<Nullable<ISelectionWithStyle[]>>,
+ formulaRangeData?: IFormulaDataGenerics<IRange>,
+ refRanges?: IRefRangeWithPosition[]
): IFormulaDataGenerics<T> {
const { id } = command;
if (checkFormulaDataNull(formulaData, unitId, sheetId)) return formulaData;
- const formulaMatrix = new ObjectMatrix(formulaData[unitId]?.[sheetId]);
+ const formulaMatrix = new ObjectMatrix(formulaData[unitId]![sheetId]);
+ const formulaRangeMatrix = formulaRangeData && new ObjectMatrix(formulaRangeData[unitId]![sheetId]);
switch (id) {
case MoveRangeCommand.id:
- handleMoveRange<T>(formulaMatrix, command as ICommandInfo<IMoveRangeCommandParams>);
+ handleMoveRange<T>(formulaMatrix, command as ICommandInfo<IMoveRangeCommandParams>, formulaRangeMatrix);
break;
case MoveRowsCommand.id:
handleMoveRows<T>(formulaMatrix, command as ICommandInfo<IMoveRowsCommandParams>, selections);
@@ -74,10 +83,20 @@ export function offsetFormula<T>(
handleMoveCols<T>(formulaMatrix, command as ICommandInfo<IMoveColsCommandParams>, selections);
break;
case InsertRowCommand.id:
- handleInsertRow<T>(formulaMatrix, command as ICommandInfo<IInsertRowCommandParams>);
+ handleInsertRow<T>(
+ formulaMatrix,
+ command as ICommandInfo<IInsertRowCommandParams>,
+ formulaRangeMatrix,
+ refRanges
+ );
break;
case InsertColCommand.id:
- handleInsertCol<T>(formulaMatrix, command as ICommandInfo<IInsertColCommandParams>);
+ handleInsertCol<T>(
+ formulaMatrix,
+ command as ICommandInfo<IInsertColCommandParams>,
+ formulaRangeMatrix,
+ refRanges
+ );
break;
case InsertRangeMoveRightCommand.id:
handleInsertRangeMoveRight<T>(formulaMatrix, command as ICommandInfo<InsertRangeMoveRightCommandParams>);
@@ -86,10 +105,20 @@ export function offsetFormula<T>(
handleInsertRangeMoveDown<T>(formulaMatrix, command as ICommandInfo<InsertRangeMoveDownCommandParams>);
break;
case RemoveRowCommand.id:
- handleRemoveRow<T>(formulaMatrix, command as ICommandInfo<IRemoveRowColCommandParams>);
+ handleRemoveRow<T>(
+ formulaMatrix,
+ command as ICommandInfo<IRemoveRowColCommandParams>,
+ formulaRangeMatrix,
+ refRanges
+ );
break;
case RemoveColCommand.id:
- handleRemoveCol<T>(formulaMatrix, command as ICommandInfo<IRemoveRowColCommandParams>);
+ handleRemoveCol<T>(
+ formulaMatrix,
+ command as ICommandInfo<IRemoveRowColCommandParams>,
+ formulaRangeMatrix,
+ refRanges
+ );
break;
case DeleteRangeMoveUpCommand.id:
handleDeleteRangeMoveUp<T>(formulaMatrix, command as ICommandInfo<IDeleteRangeMoveUpCommandParams>);
@@ -102,36 +131,17 @@ export function offsetFormula<T>(
return formulaData;
}
-function handleMoveRange<T>(formulaMatrix: ObjectMatrix<T>, command: ICommandInfo<IMoveRangeCommandParams>) {
+function handleMoveRange<T>(
+ formulaMatrix: ObjectMatrix<T>,
+ command: ICommandInfo<IMoveRangeCommandParams>,
+ formulaRangeMatrix?: ObjectMatrix<IRange>
+) {
const { params } = command;
if (!params) return;
const { fromRange, toRange } = params;
- const {
- startRow: fromStartRow,
- endRow: fromEndRow,
- startColumn: fromStartColumn,
- endColumn: fromEndColumn,
- } = fromRange;
- const { startRow: toStartRow, endRow: toEndRow, startColumn: toStartColumn, endColumn: toEndColumn } = toRange;
- const cacheMatrix = new ObjectMatrix<T>();
-
- for (let r = fromStartRow; r <= fromEndRow; r++) {
- for (let c = fromStartColumn; c <= fromEndColumn; c++) {
- const cacheValue = formulaMatrix.getValue(r, c);
- cacheMatrix.setValue(r - fromStartRow, c - fromStartColumn, cacheValue);
-
- formulaMatrix.setValue(r, c, null as T);
- }
- }
-
- for (let r = toStartRow; r <= toEndRow; r++) {
- for (let c = toStartColumn; c <= toEndColumn; c++) {
- const cacheValue = cacheMatrix.getValue(r - toStartRow, c - toStartColumn);
- formulaMatrix.setValue(r, c, cacheValue);
- }
- }
+ moveRangeUpdateFormulaData(fromRange, toRange, formulaMatrix, formulaMatrix, formulaRangeMatrix);
}
function handleMoveRows<T>(
@@ -192,25 +202,49 @@ function handleMoveCols<T>(
formulaMatrix.moveColumns(fromColNumber, count, toCol);
}
-function handleInsertRow<T>(formulaMatrix: ObjectMatrix<T>, command: ICommandInfo<IInsertRowCommandParams>) {
+function handleInsertRow<T>(
+ formulaMatrix: ObjectMatrix<T>,
+ command: ICommandInfo<IInsertRowCommandParams>,
+ formulaRangeMatrix?: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
const { params } = command;
if (!params) return;
const { range } = params;
+
+ if (formulaRangeMatrix) {
+ removeFormulaArrayRow(range, formulaMatrix, formulaRangeMatrix, refRanges);
+ return;
+ }
+
+ // Handle formula data
const lastEndRow = formulaMatrix.getLength() - 1;
const lastEndColumn = formulaMatrix.getRange().endColumn;
-
+ // Offset formula range
handleInsertRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.ROWS);
}
-function handleInsertCol<T>(formulaMatrix: ObjectMatrix<T>, command: ICommandInfo<IInsertColCommandParams>) {
+function handleInsertCol<T>(
+ formulaMatrix: ObjectMatrix<T>,
+ command: ICommandInfo<IInsertColCommandParams>,
+ formulaRangeMatrix?: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
const { params } = command;
if (!params) return;
const { range } = params;
+
+ if (formulaRangeMatrix) {
+ removeFormulaArrayColumn(range, formulaMatrix, formulaRangeMatrix, refRanges);
+ return;
+ }
+
+ // Handle formula data
const lastEndRow = formulaMatrix.getLength() - 1;
const lastEndColumn = formulaMatrix.getRange().endColumn;
-
+ // Offset formula range
handleInsertRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.COLUMNS);
}
@@ -242,25 +276,49 @@ function handleInsertRangeMoveDown<T>(
handleInsertRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.ROWS);
}
-function handleRemoveRow<T>(formulaMatrix: ObjectMatrix<T>, command: ICommandInfo<IRemoveRowColCommandParams>) {
+function handleRemoveRow<T>(
+ formulaMatrix: ObjectMatrix<T>,
+ command: ICommandInfo<IRemoveRowColCommandParams>,
+ formulaRangeMatrix?: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
const { params } = command;
if (!params) return;
const { range } = params;
+
+ if (formulaRangeMatrix) {
+ removeFormulaArrayRow(range, formulaMatrix, formulaRangeMatrix, refRanges);
+ return;
+ }
+
+ // Handle formula data
const lastEndRow = formulaMatrix.getLength() - 1;
const lastEndColumn = formulaMatrix.getRange().endColumn;
-
+ // Offset formula range
handleDeleteRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.ROWS);
}
-function handleRemoveCol<T>(formulaMatrix: ObjectMatrix<T>, command: ICommandInfo<IRemoveRowColCommandParams>) {
+function handleRemoveCol<T>(
+ formulaMatrix: ObjectMatrix<T>,
+ command: ICommandInfo<IRemoveRowColCommandParams>,
+ formulaRangeMatrix?: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
const { params } = command;
if (!params) return;
const { range } = params;
+
+ if (formulaRangeMatrix) {
+ removeFormulaArrayColumn(range, formulaMatrix, formulaRangeMatrix, refRanges);
+ return;
+ }
+
+ // Handle formula data
const lastEndRow = formulaMatrix.getLength() - 1;
const lastEndColumn = formulaMatrix.getRange().endColumn;
-
+ // Offset formula range
handleDeleteRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.COLUMNS);
}
@@ -292,7 +350,12 @@ function handleDeleteRangeMoveLeft<T>(
handleDeleteRangeMutation(formulaMatrix, range, lastEndRow, lastEndColumn, Dimension.COLUMNS);
}
-export function offsetArrayFormula(arrayFormulaRange: IArrayFormulaRangeType, unitId: string, sheetId: string) {
+export function offsetArrayFormula(
+ arrayFormulaRange: IArrayFormulaRangeType,
+ command: ICommandInfo,
+ unitId: string,
+ sheetId: string
+) {
if (checkFormulaDataNull(arrayFormulaRange, unitId, sheetId)) return arrayFormulaRange;
if (arrayFormulaRange[unitId]?.[sheetId] == null) {
return arrayFormulaRange;
@@ -323,7 +386,7 @@ export function offsetArrayFormula(arrayFormulaRange: IArrayFormulaRangeType, un
return arrayFormulaRange;
}
-function checkFormulaDataNull<T>(formulaData: IFormulaDataGenerics<T>, unitId: string, sheetId: string) {
+export function checkFormulaDataNull<T>(formulaData: IFormulaDataGenerics<T>, unitId: string, sheetId: string) {
if (formulaData == null || formulaData[unitId] == null || formulaData[unitId]?.[sheetId] == null) {
return true;
}
@@ -336,3 +399,150 @@ export function removeFormulaData<T>(formulaData: IFormulaDataGenerics<T>, unitI
delete formulaData[unitId]![sheetId];
}
}
+export function removeValueFormulaArray<T>(formulaRange: IRange, formulaMatrix: ObjectMatrix<T>) {
+ const { startRow, endRow, startColumn, endColumn } = formulaRange;
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ formulaMatrix.setValue(r, c, null as T);
+ }
+ }
+}
+
+export function inFormulaRange(row: number, column: number, formulaMatrix?: ObjectMatrix<IRange>) {
+ if (!formulaMatrix) return false;
+
+ let result = false;
+ formulaMatrix.forValue((r, c, range) => {
+ if (!range) return;
+
+ const { startRow, endRow, startColumn, endColumn } = range;
+
+ if (startRow <= row && row <= endRow && startColumn <= column && column <= endColumn) {
+ result = true;
+ return false;
+ }
+ });
+
+ return result;
+}
+
+export function moveRangeUpdateFormulaData<T>(
+ fromRange: IRange,
+ toRange: IRange,
+ fromArrayFormulaCellDataMatrix: ObjectMatrix<T>,
+ toArrayFormulaCellDataMatrix: ObjectMatrix<T>,
+ arrayFormulaRangeMatrix?: ObjectMatrix<IRange>
+) {
+ const {
+ startRow: fromStartRow,
+ endRow: fromEndRow,
+ startColumn: fromStartColumn,
+ endColumn: fromEndColumn,
+ } = fromRange;
+
+ const { startRow: toStartRow, endRow: toEndRow, startColumn: toStartColumn, endColumn: toEndColumn } = toRange;
+
+ const cacheMatrix = new ObjectMatrix<T>();
+
+ for (let r = fromStartRow; r <= fromEndRow; r++) {
+ for (let c = fromStartColumn; c <= fromEndColumn; c++) {
+ const cacheValue = fromArrayFormulaCellDataMatrix.getValue(r, c);
+
+ // If the moved area has an array formula,
+ // move the first cell, you need to clear the contents of the array formula.
+ // move the no-first cell, do not change the original data, set the new area to null
+ const formulaRange = arrayFormulaRangeMatrix && arrayFormulaRangeMatrix.getValue(r, c);
+ if (formulaRange) {
+ cacheMatrix.setValue(r - fromStartRow, c - fromStartColumn, cacheValue);
+ removeValueFormulaArray(formulaRange, fromArrayFormulaCellDataMatrix);
+ continue;
+ } else if (inFormulaRange(r, c, arrayFormulaRangeMatrix)) {
+ cacheMatrix.setValue(r - fromStartRow, c - fromStartColumn, null as T);
+ continue;
+ }
+
+ cacheMatrix.setValue(r - fromStartRow, c - fromStartColumn, cacheValue);
+ fromArrayFormulaCellDataMatrix.setValue(r, c, null as T);
+ }
+ }
+
+ for (let r = toStartRow; r <= toEndRow; r++) {
+ for (let c = toStartColumn; c <= toEndColumn; c++) {
+ const cacheValue = cacheMatrix.getValue(r - toStartRow, c - toStartColumn);
+ toArrayFormulaCellDataMatrix.setValue(r, c, cacheValue);
+ }
+ }
+}
+
+/**
+ * Handle arrayFormulaRange and arrayFormulaCellData. If removing rows affects the array formula, the array formula cell data needs to be cleared and will be regenerated during recalculation.
+ * @param range
+ * @param formulaMatrix
+ * @param formulaRangeMatrix
+ * @param refRanges
+ */
+export function removeFormulaArrayRow<T>(
+ range: IRange,
+ formulaMatrix: ObjectMatrix<T>,
+ formulaRangeMatrix: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
+ const { endRow } = range;
+ // 1. Above the first line of ArrayFormula
+ formulaRangeMatrix.forValue((row, column, range) => {
+ if (!range) return;
+
+ const { startRow: formulaRangeStartRow } = range;
+ if (endRow <= formulaRangeStartRow) {
+ removeValueFormulaArray(range, formulaMatrix);
+ }
+ });
+
+ // 2. Above the last line of refRanges
+ refRanges?.forEach(({ row, column, range }) => {
+ if (!range) return;
+
+ const { endRow: refRangeEndRow } = range;
+ if (endRow <= refRangeEndRow) {
+ // The formula cell position corresponding to refRange
+ const removeRange = formulaRangeMatrix?.getValue(row, column);
+ removeRange && removeValueFormulaArray(removeRange, formulaMatrix);
+ }
+ });
+}
+
+/**
+ * Handle arrayFormulaRange and arrayFormulaCellData. If removing columns affects the array formula, the array formula cell data needs to be cleared and will be regenerated during recalculation.
+ * @param range
+ * @param formulaRangeMatrix
+ * @param refRanges
+ */
+export function removeFormulaArrayColumn<T>(
+ range: IRange,
+ formulaMatrix: ObjectMatrix<T>,
+ formulaRangeMatrix: ObjectMatrix<IRange>,
+ refRanges?: IRefRangeWithPosition[]
+) {
+ const { endColumn } = range;
+ // 1. Left of the first column of ArrayFormula
+ formulaRangeMatrix.forValue((row, column, range) => {
+ if (!range) return;
+
+ const { startColumn: formulaRangeStartColumn } = range;
+ if (endColumn <= formulaRangeStartColumn) {
+ removeValueFormulaArray(range, formulaMatrix);
+ }
+ });
+
+ // 2. Left of the last column of refRanges
+ refRanges?.forEach(({ row, column, range }) => {
+ if (!range) return;
+
+ const { endColumn: refRangeEndColumn } = range;
+ if (endColumn <= refRangeEndColumn) {
+ // The formula cell position corresponding to refRange
+ const removeRange = formulaRangeMatrix?.getValue(row, column);
+ removeRange && removeValueFormulaArray(removeRange, formulaMatrix);
+ }
+ });
+}
diff --git a/packages/sheets-formula/src/controllers/utils/redo-undo-formula-data.ts b/packages/sheets-formula/src/controllers/utils/redo-undo-formula-data.ts
new file mode 100644
index 0000000000..00017e5417
--- /dev/null
+++ b/packages/sheets-formula/src/controllers/utils/redo-undo-formula-data.ts
@@ -0,0 +1,145 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { ICellData, ICommandInfo, IMutationCommonParams, Nullable } from '@univerjs/core';
+import { isFormulaId, isFormulaString, ObjectMatrix, UndoCommand } from '@univerjs/core';
+import type { IArrayFormulaRangeType, IArrayFormulaUnitCellType, IFormulaData } from '@univerjs/engine-formula';
+import type { IMoveRangeMutationParams } from '@univerjs/sheets';
+
+import { checkFormulaDataNull, moveRangeUpdateFormulaData } from './offset-formula-data';
+
+export function handleRedoUndoMoveRange(
+ command: ICommandInfo<IMoveRangeMutationParams>,
+ formulaData: IFormulaData,
+ arrayFormulaRange: IArrayFormulaRangeType,
+ arrayFormulaCellData: IArrayFormulaUnitCellType
+) {
+ const { params } = command;
+ if (!params) return;
+
+ const { unitId } = params;
+ let { from, to } = params;
+
+ if ((command.params as IMutationCommonParams).trigger === UndoCommand.id) {
+ from = params.to;
+ to = params.from;
+ }
+
+ const { subUnitId: fromSubUnitId, value: fromValue } = from;
+ const { subUnitId: toSubUnitId, value: toValue } = to;
+
+ if (
+ checkFormulaDataNull(formulaData, unitId, fromSubUnitId) ||
+ checkFormulaDataNull(formulaData, unitId, toSubUnitId) ||
+ checkFormulaDataNull(arrayFormulaRange, unitId, fromSubUnitId) ||
+ checkFormulaDataNull(arrayFormulaRange, unitId, toSubUnitId) ||
+ checkFormulaDataNull(arrayFormulaCellData, unitId, fromSubUnitId) ||
+ checkFormulaDataNull(arrayFormulaCellData, unitId, toSubUnitId)
+ )
+ return;
+
+ const fromValueMatrix = new ObjectMatrix(fromValue);
+ const toValueMatrix = new ObjectMatrix(toValue);
+
+ setFormulaData(fromValueMatrix, formulaData, unitId, fromSubUnitId);
+ setFormulaData(toValueMatrix, formulaData, unitId, toSubUnitId);
+ setArrayFormulaData(
+ fromValueMatrix,
+ toValueMatrix,
+ arrayFormulaRange,
+ arrayFormulaCellData,
+ unitId,
+ fromSubUnitId,
+ toSubUnitId
+ );
+}
+
+function setFormulaData(
+ valueMatrix: ObjectMatrix<Nullable<ICellData>>,
+ formulaData: IFormulaData,
+ unitId: string,
+ subUnitId: string
+) {
+ if (!formulaData[unitId]) {
+ formulaData[unitId] = {};
+ }
+
+ valueMatrix.forValue((r, c, newVal) => {
+ if (!formulaData[unitId]![subUnitId][r]) {
+ formulaData[unitId]![subUnitId][r] = {};
+ }
+
+ if (!newVal) {
+ delete formulaData[unitId]![subUnitId][r][c];
+ return;
+ }
+
+ const formulaString = newVal?.f || '';
+ const formulaId = newVal?.si || '';
+
+ const checkFormulaString = isFormulaString(formulaString);
+ const checkFormulaId = isFormulaId(formulaId);
+
+ if (checkFormulaString && checkFormulaId) {
+ formulaData[unitId]![subUnitId][r][c] = {
+ f: formulaString,
+ si: formulaId,
+ };
+ } else if (checkFormulaString && !checkFormulaId) {
+ formulaData[unitId]![subUnitId][r][c] = {
+ f: formulaString,
+ };
+ } else if (!checkFormulaString && checkFormulaId) {
+ formulaData[unitId]![subUnitId][r][c] = {
+ f: '',
+ si: formulaId,
+ };
+ } else if (!checkFormulaString && !checkFormulaId) {
+ delete formulaData[unitId]![subUnitId][r][c];
+ }
+ });
+}
+
+function setArrayFormulaData(
+ fromValueMatrix: ObjectMatrix<Nullable<ICellData>>,
+ toValueMatrix: ObjectMatrix<Nullable<ICellData>>,
+ arrayFormulaRange: IArrayFormulaRangeType,
+ arrayFormulaCellData: IArrayFormulaUnitCellType,
+ unitId: string,
+ fromSubUnitId: string,
+ toSubUnitId: string
+) {
+ const fromArrayFormulaCellDataMatrix = new ObjectMatrix(arrayFormulaCellData?.[unitId]?.[fromSubUnitId] ?? {});
+ const toArrayFormulaCellDataMatrix = new ObjectMatrix(arrayFormulaCellData?.[unitId]?.[toSubUnitId] ?? {});
+ const fromArrayFormulaRangeMatrix = new ObjectMatrix(arrayFormulaRange?.[unitId]?.[fromSubUnitId] ?? {});
+ const toArrayFormulaRangeMatrix = new ObjectMatrix(arrayFormulaRange?.[unitId]?.[toSubUnitId] ?? {});
+
+ // We need set toValueMatrix value to fromValueMatrix value
+ const fromRange = fromValueMatrix.getDataRange();
+ const toRange = toValueMatrix.getDataRange();
+
+ // update arrayFormulaCellData
+ moveRangeUpdateFormulaData(
+ fromRange,
+ toRange,
+ fromArrayFormulaCellDataMatrix,
+ toArrayFormulaCellDataMatrix,
+ fromArrayFormulaRangeMatrix
+ );
+
+ // update arrayFormulaRange
+ moveRangeUpdateFormulaData(fromRange, toRange, fromArrayFormulaRangeMatrix, toArrayFormulaRangeMatrix);
+}
diff --git a/packages/sheets-formula/src/views/prompt/help-function/index.module.less b/packages/sheets-formula/src/views/prompt/help-function/index.module.less
index 889dd43d35..4d8fa0aeed 100644
--- a/packages/sheets-formula/src/views/prompt/help-function/index.module.less
+++ b/packages/sheets-formula/src/views/prompt/help-function/index.module.less
@@ -29,7 +29,8 @@
font-size: var(--font-size-xs);
font-weight: 500;
color: rgb(var(--text-color));
-
+ overflow-wrap: anywhere;
+
border-bottom: 1px solid rgb(var(--border-color));
&-icons {