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(sheets-formula): update formula refs when deleting rows on inactive sheet (#7306)

QJerry22472c3652dd
diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts
index 4e65482375..22a95999ae 100644
--- a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts
+++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts
@@ -1403,8 +1403,20 @@ describe('Test update formula ', () => {
expect(valuesRedo).toStrictEqual([[{ f: '=A1:B1' }]]);
});
- it('Remove row, update reference and position', async () => {
- const params: IRemoveRowColCommandParams = {
+ it('Remove rows on non-active sheet, update reference and position', async () => {
+ const workbook = get(IUniverInstanceService).getUnit<Workbook>('test');
+ if (!workbook) {
+ throw new Error('workbook not found');
+ }
+ const sheet2 = workbook.getSheetBySheetId('sheet2');
+ if (!sheet2) {
+ throw new Error('sheet2 not found');
+ }
+ workbook.setActiveSheet(sheet2);
+
+ const params = {
+ unitId: 'test',
+ subUnitId: 'sheet1',
range: {
startRow: 1,
endRow: 1,
@@ -1413,7 +1425,7 @@ describe('Test update formula ', () => {
},
};
- expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
+ expect(commandService.syncExecuteCommand(RemoveRowByRangeCommand.id, params)).toBeTruthy();
const values = getValues(1, 2, 2, 2);
expect(values).toStrictEqual([[{ f: '=A1:B1' }], [null]]);
const values2 = getValues(4, 2, 5, 2);
@@ -1537,17 +1549,30 @@ describe('Test update formula ', () => {
expect(valuesRedo).toStrictEqual([[{ f: '=A1:A2' }]]);
});
- it('Remove column, update reference and position', async () => {
- const params: IRemoveRowColCommandParams = {
+ it('Remove columns on non-active sheet, update reference and position', async () => {
+ const workbook = get(IUniverInstanceService).getUnit<Workbook>('test');
+ if (!workbook) {
+ throw new Error('workbook not found');
+ }
+ const sheet1 = workbook.getSheetBySheetId('sheet1');
+ const sheet2 = workbook.getSheetBySheetId('sheet2');
+ if (!sheet1 || !sheet2) {
+ throw new Error('sheet not found');
+ }
+ workbook.setActiveSheet(sheet2);
+
+ const params = {
+ unitId: 'test',
+ subUnitId: 'sheet1',
range: {
startColumn: 1,
endColumn: 1,
startRow: 0,
- endRow: 2,
+ endRow: sheet1.getRowCount() - 1,
},
};
- expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
+ expect(commandService.syncExecuteCommand(RemoveColByRangeCommand.id, params)).toBeTruthy();
const values = getValues(2, 1, 2, 2);
expect(values).toStrictEqual([[{ f: '=A1:A2' }, null]]);
const values2 = getValues(5, 1, 5, 2);
diff --git a/packages/sheets-formula/src/controllers/utils/__tests__/ref-range-param.spec.ts b/packages/sheets-formula/src/controllers/utils/__tests__/ref-range-param.spec.ts
index 9c9e17e596..eaac453197 100644
--- a/packages/sheets-formula/src/controllers/utils/__tests__/ref-range-param.spec.ts
+++ b/packages/sheets-formula/src/controllers/utils/__tests__/ref-range-param.spec.ts
@@ -195,7 +195,7 @@ describe('getReferenceMoveParams', () => {
});
expect(getReferenceMoveParams(workbook, {
id: RemoveRowCommand.id,
- params: { range },
+ params: { range, unitId: 'test', subUnitId: 'sheet1' },
})).toEqual({
type: FormulaReferenceMoveType.RemoveRow,
unitId: 'test',
@@ -205,7 +205,7 @@ describe('getReferenceMoveParams', () => {
});
expect(getReferenceMoveParams(workbook, {
id: RemoveColCommand.id,
- params: { range },
+ params: { range, unitId: 'test', subUnitId: 'sheet1' },
})).toEqual({
type: FormulaReferenceMoveType.RemoveColumn,
unitId: 'test',
diff --git a/packages/sheets-formula/src/controllers/utils/ref-range-param.ts b/packages/sheets-formula/src/controllers/utils/ref-range-param.ts
index 1e5832fbc0..8f458d3b35 100644
--- a/packages/sheets-formula/src/controllers/utils/ref-range-param.ts
+++ b/packages/sheets-formula/src/controllers/utils/ref-range-param.ts
@@ -26,7 +26,7 @@ import type {
IMoveColsCommandParams,
IMoveRangeCommandParams,
IMoveRowsCommandParams,
- IRemoveRowColCommandParams,
+ IRemoveRowColCommandInterceptParams,
IRemoveSheetCommandParams,
ISetWorkbookNameCommandParams,
ISetWorksheetNameCommandParams,
@@ -104,10 +104,10 @@ export function getReferenceMoveParams(workbook: Workbook, command: ICommandInfo
result = handleRefInsertRangeMoveDown(command as ICommandInfo<IInsertRangeMoveDownCommandParams>, workbook);
break;
case RemoveRowCommand.id:
- result = handleRefRemoveRow(command as ICommandInfo<IRemoveRowColCommandParams>, workbook);
+ result = handleRefRemoveRow(command as ICommandInfo<IRemoveRowColCommandInterceptParams>, workbook);
break;
case RemoveColCommand.id:
- result = handleRefRemoveCol(command as ICommandInfo<IRemoveRowColCommandParams>, workbook);
+ result = handleRefRemoveCol(command as ICommandInfo<IRemoveRowColCommandInterceptParams>);
break;
case DeleteRangeMoveUpCommand.id:
result = handleRefDeleteRangeMoveUp(command as ICommandInfo<IDeleteRangeMoveUpCommandParams>, workbook);
@@ -324,12 +324,11 @@ function handleRefInsertRangeMoveDown(command: ICommandInfo<IInsertRangeMoveDown
};
}
-function handleRefRemoveRow(command: ICommandInfo<IRemoveRowColCommandParams>, workbook: Workbook) {
+function handleRefRemoveRow(command: ICommandInfo<IRemoveRowColCommandInterceptParams>, workbook: Workbook) {
const { params } = command;
if (!params) return null;
- const { range } = params;
- const { unitId, sheetId } = getCurrentSheetInfo(workbook);
+ const { range, unitId, subUnitId: sheetId } = params;
return {
type: FormulaReferenceMoveType.RemoveRow,
@@ -340,12 +339,11 @@ function handleRefRemoveRow(command: ICommandInfo<IRemoveRowColCommandParams>, w
};
}
-function handleRefRemoveCol(command: ICommandInfo<IRemoveRowColCommandParams>, workbook: Workbook) {
+function handleRefRemoveCol(command: ICommandInfo<IRemoveRowColCommandInterceptParams>) {
const { params } = command;
if (!params) return null;
- const { range } = params;
- const { unitId, sheetId } = getCurrentSheetInfo(workbook);
+ const { range, unitId, subUnitId: sheetId } = params;
return {
type: FormulaReferenceMoveType.RemoveColumn,
diff --git a/packages/sheets/src/commands/commands/remove-row-col.command.ts b/packages/sheets/src/commands/commands/remove-row-col.command.ts
index 9e38aaa1af..d6267aab26 100644
--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts
@@ -50,6 +50,8 @@ export interface IRemoveRowColCommandParams extends Partial<ISheetCommandSharedP
}
export interface IRemoveRowColCommandInterceptParams extends IRemoveRowColCommandParams {
+ unitId: string;
+ subUnitId: string;
ranges?: IRange[];
}
@@ -126,9 +128,10 @@ export const RemoveRowByRangeCommand: ICommand<IRemoveRowByRangeCommandParams> =
undoMutations.unshift(...undos);
});
+ const interceptorParams: IRemoveRowColCommandInterceptParams = params;
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveRowCommandId,
- params: { range } as IRemoveRowColCommandParams,
+ params: interceptorParams,
});
const commandService = accessor.get(ICommandService);
@@ -146,7 +149,7 @@ export const RemoveRowByRangeCommand: ICommand<IRemoveRowByRangeCommandParams> =
const afterInterceptors = sheetInterceptorService.afterCommandExecute({
id: RemoveRowCommandId,
- params: { range } as IRemoveRowColCommandParams,
+ params: interceptorParams,
});
sequenceExecute(afterInterceptors.redos, commandService);
@@ -202,7 +205,7 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {
const canPerform = await sheetInterceptorService.beforeCommandExecute({
id: RemoveRowCommand.id,
- params: { range } as IRemoveRowColCommandParams,
+ params: { range, unitId, subUnitId },
});
if (!canPerform) {
@@ -248,9 +251,10 @@ export const RemoveColByRangeCommand: ICommand<IRemoveColByRangeCommandParams> =
cellValue: removedCols.getMatrix(),
};
+ const interceptorParams: IRemoveRowColCommandInterceptParams = params;
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveColCommandId,
- params: { range } as IRemoveRowColCommandParams,
+ params: interceptorParams,
});
const commandService = accessor.get(ICommandService);
const result = sequenceExecute(
@@ -267,7 +271,7 @@ export const RemoveColByRangeCommand: ICommand<IRemoveColByRangeCommandParams> =
const afterInterceptors = sheetInterceptorService.afterCommandExecute({
id: RemoveColCommandId,
- params: { range } as IRemoveRowColCommandParams,
+ params: interceptorParams,
});
sequenceExecute(afterInterceptors.redos, commandService);
@@ -311,7 +315,7 @@ export const RemoveColCommand: ICommand = {
if (!range) return false;
const univerInstanceService = accessor.get(IUniverInstanceService);
- const target = getSheetCommandTarget(univerInstanceService);
+ const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;
const { worksheet, subUnitId, unitId } = target;
@@ -324,7 +328,7 @@ export const RemoveColCommand: ICommand = {
const canPerform = await sheetInterceptorService.beforeCommandExecute({
id: RemoveColCommand.id,
- params: { range } as IRemoveRowColCommandParams,
+ params: { range, unitId, subUnitId },
});
if (!canPerform) {
diff --git a/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts b/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts
index 7c85c17185..62e27465fa 100644
--- a/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts
+++ b/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts
@@ -2107,16 +2107,16 @@ describe('test ref-range move', () => {
it('should be delete', () => {
const targetRange: IRange = { startRow: 5, endRow: 7, startColumn: 1, endColumn: 1 };
- const resultRange = handleRemoveRowCommon({ range: { ...targetRange } }, targetRange);
+ const resultRange = handleRemoveRowCommon({ range: { ...targetRange }, unitId: '', subUnitId: '' }, targetRange);
expect(resultRange.length).toBe(0);
- const resultRange2 = handleRemoveRowCommon({ range: { ...targetRange } }, { ...targetRange, startRow: 4 });
+ const resultRange2 = handleRemoveRowCommon({ range: { ...targetRange }, unitId: '', subUnitId: '' }, { ...targetRange, startRow: 4 });
expect(resultRange2).toEqual([{ startRow: 4, endRow: 4, startColumn: 1, endColumn: 1 }]);
});
it('should not be delete ', () => {
const targetRange: IRange = { startRow: 5, endRow: 7, startColumn: 1, endColumn: 1 };
- const resultRange = handleRemoveRowCommon({ range: { ...targetRange }, ranges: [{ startRow: 5, endRow: 5, startColumn: 1, endColumn: 1 }, { startRow: 6, endRow: 6, startColumn: 1, endColumn: 1 }] }, targetRange);
+ const resultRange = handleRemoveRowCommon({ range: { ...targetRange }, unitId: '', subUnitId: '', ranges: [{ startRow: 5, endRow: 5, startColumn: 1, endColumn: 1 }, { startRow: 6, endRow: 6, startColumn: 1, endColumn: 1 }] }, targetRange);
expect(resultRange).toEqual([{ startRow: 5, endRow: 5, startColumn: 1, endColumn: 1 }]);
});
});
diff --git a/packages/sheets/src/services/ref-range/ref-range.service.ts b/packages/sheets/src/services/ref-range/ref-range.service.ts
index 16a649f929..82b84bf1e0 100644
--- a/packages/sheets/src/services/ref-range/ref-range.service.ts
+++ b/packages/sheets/src/services/ref-range/ref-range.service.ts
@@ -22,7 +22,7 @@ import type { IInsertRangeMoveRightCommandParams } from '../../commands/commands
import type { IInsertColCommandParams, IInsertRowCommandParams } from '../../commands/commands/insert-row-col.command';
import type { IMoveRangeCommandParams } from '../../commands/commands/move-range.command';
import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../../commands/commands/move-rows-cols.command';
-import type { IRemoveRowColCommandParams } from '../../commands/commands/remove-row-col.command';
+import type { IRemoveRowColCommandInterceptParams } from '../../commands/commands/remove-row-col.command';
import type { IReorderRangeCommandParams } from '../../commands/commands/reorder-range.command';
import type { IMoveRangeMutationParams } from '../../commands/mutations/move-range.mutation';
import type { ISheetCommandSharedParams } from '../../commands/utils/interface';
@@ -286,8 +286,8 @@ export class RefRangeService extends Disposable {
return this._checkRange([effectRange], unitId, subUnitId);
}
case EffectRefRangId.RemoveRowCommandId: {
- const params = command.params as IRemoveRowColCommandParams;
- const target = getSheetCommandTarget(this._univerInstanceService);
+ const params = command.params as IRemoveRowColCommandInterceptParams;
+ const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) return [];
const { worksheet, unitId, subUnitId } = target;
@@ -302,8 +302,8 @@ export class RefRangeService extends Disposable {
return this._checkRange([effectRange], unitId, subUnitId);
}
case EffectRefRangId.RemoveColCommandId: {
- const params = command.params as IRemoveRowColCommandParams;
- const target = getSheetCommandTarget(this._univerInstanceService);
+ const params = command.params as IRemoveRowColCommandInterceptParams;
+ const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) return [];
const { worksheet, unitId, subUnitId } = target;
diff --git a/packages/sheets/src/services/ref-range/util.ts b/packages/sheets/src/services/ref-range/util.ts
index c22dd7fc78..59df7028e7 100644
--- a/packages/sheets/src/services/ref-range/util.ts
+++ b/packages/sheets/src/services/ref-range/util.ts
@@ -23,7 +23,7 @@ import type { IInsertRangeMoveRightCommandParams } from '../../commands/commands
import type { IInsertColCommandParams, IInsertRowCommandParams } from '../../commands/commands/insert-row-col.command';
import type { IMoveRangeCommandParams } from '../../commands/commands/move-range.command';
import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../../commands/commands/move-rows-cols.command';
-import type { IRemoveRowColCommandInterceptParams, IRemoveRowColCommandParams } from '../../commands/commands/remove-row-col.command';
+import type { IRemoveRowColCommandInterceptParams } from '../../commands/commands/remove-row-col.command';
import type { IReorderRangeCommandParams } from '../../commands/commands/reorder-range.command';
import type { IMoveRangeMutationParams } from '../../commands/mutations/move-range.mutation';
import type { IMoveColumnsMutationParams, IMoveRowsMutationParams } from '../../commands/mutations/move-rows-cols.mutation';
@@ -1633,8 +1633,8 @@ export function getSeparateEffectedRangesOnCommand(accessor: IAccessor, command:
};
}
case EffectRefRangId.RemoveRowCommandId: {
- const params = command.params as IRemoveRowColCommandParams;
- const target = getSheetCommandTarget(univerInstanceService);
+ const params = command.params as IRemoveRowColCommandInterceptParams;
+ const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return;
const { worksheet, unitId, subUnitId } = target;
@@ -1654,8 +1654,8 @@ export function getSeparateEffectedRangesOnCommand(accessor: IAccessor, command:
};
}
case EffectRefRangId.RemoveColCommandId: {
- const params = command.params as IRemoveRowColCommandParams;
- const target = getSheetCommandTarget(univerInstanceService);
+ const params = command.params as IRemoveRowColCommandInterceptParams;
+ const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return;
const { worksheet, unitId, subUnitId } = target;