Skip to content

Commit

Permalink
fix(sheets): merge cell update, test params
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Dec 27, 2024
1 parent d75d3ce commit f6b5447
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,12 @@ describe('Test update defined name', () => {

it('Remove row, update reference', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 1,
endRow: 1,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand All @@ -630,12 +630,12 @@ describe('Test update defined name', () => {

it('Remove column, update reference', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startColumn: 1,
endColumn: 1,
startRow: 0,
endRow: 2,
},
}],
};

expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,12 @@ describe('Test update formula ', () => {

it('Remove row, update reference', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 1,
endRow: 1,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand All @@ -917,12 +917,12 @@ describe('Test update formula ', () => {

it('Remove row, update reference and position', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 1,
endRow: 1,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand All @@ -946,12 +946,12 @@ describe('Test update formula ', () => {

it('Remove row, update reference and position, adjacent formula', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 8,
endRow: 8,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand All @@ -969,12 +969,12 @@ describe('Test update formula ', () => {

it('Remove row, removed row contains formula', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 10,
endRow: 10,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand All @@ -992,12 +992,12 @@ describe('Test update formula ', () => {

it('Remove row, changes to other sheet reference ranges', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startRow: 1,
endRow: 1,
startColumn: 0,
endColumn: 19,
},
}],
};

expect(await commandService.executeCommand(RemoveRowCommand.id, params)).toBeTruthy();
Expand Down Expand Up @@ -1028,12 +1028,12 @@ describe('Test update formula ', () => {

it('Remove column, update reference', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startColumn: 1,
endColumn: 1,
startRow: 0,
endRow: 2,
},
}],
};

expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
Expand All @@ -1051,12 +1051,12 @@ describe('Test update formula ', () => {

it('Remove column, update reference and position', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startColumn: 1,
endColumn: 1,
startRow: 0,
endRow: 2,
},
}],
};

expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
Expand Down Expand Up @@ -1086,12 +1086,12 @@ describe('Test update formula ', () => {

it('Remove column, update reference and position, contains #REF', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startColumn: 0,
endColumn: 1,
startRow: 0,
endRow: 2,
},
}],
};

expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
Expand Down Expand Up @@ -1148,12 +1148,12 @@ describe('Test update formula ', () => {

it('Remove column, removed column contains formula', async () => {
const params: IRemoveRowColCommandParams = {
range: {
ranges: [{
startColumn: 3,
endColumn: 3,
startRow: 0,
endRow: 2,
},
}],
};

expect(await commandService.executeCommand(RemoveColCommand.id, params)).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,60 @@ describe('Test insert and remove rows cols commands', () => {
it('reduce merge cell length', async () => {
await commandService.executeCommand(RemoveRowCommand.id, {
ranges: [
{ startRow: 12, endRow: 12, startColumn: 1, endColumn: 1 },
{ startRow: 13, endRow: 13, startColumn: 1, endColumn: 1 },
{ startRow: 11, endRow: 12, startColumn: 1, endColumn: 1 },
{ startRow: 12, endRow: 13, startColumn: 1, endColumn: 1 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(12, 2)).toEqual({ startRow: 10, endRow: 13, startColumn: 2, endColumn: 2 });
expect(getMergedInfo(10, 2)).toEqual({ startRow: 10, endRow: 12, startColumn: 2, endColumn: 2 });
});
it('remove merge cell', async () => {
await commandService.executeCommand(RemoveRowCommand.id, {
ranges: [
{ startRow: 10, endRow: 12, startColumn: 1, endColumn: 1 },
{ startRow: 13, endRow: 15, startColumn: 1, endColumn: 1 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(10, 2)).toBeNull();
});
it('move merge cell position', async () => {
await commandService.executeCommand(RemoveRowCommand.id, {
ranges: [
{ startRow: 9, endRow: 11, startColumn: 1, endColumn: 1 },
{ startRow: 13, endRow: 14, startColumn: 1, endColumn: 1 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(9, 2)).toEqual({ startRow: 9, endRow: 10, startColumn: 2, endColumn: 2 });
});
});
describe('Remove col where contain mergeCell', () => {
it('reduce merge cell length', async () => {
await commandService.executeCommand(RemoveColCommand.id, {
ranges: [
{ startRow: 1, endRow: 1, startColumn: 12, endColumn: 12 },
{ startRow: 1, endRow: 1, startColumn: 13, endColumn: 13 },
{ startRow: 1, endRow: 1, startColumn: 11, endColumn: 12 },
{ startRow: 1, endRow: 1, startColumn: 12, endColumn: 13 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(10, 12)).toEqual({ startRow: 10, endRow: 10, startColumn: 10, endColumn: 12 });
});

it('remove merge cell', async () => {
await commandService.executeCommand(RemoveColCommand.id, {
ranges: [
{ startRow: 1, endRow: 1, startColumn: 10, endColumn: 12 },
{ startRow: 1, endRow: 1, startColumn: 13, endColumn: 15 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(10, 12)).toBeNull();
});

it('move merge cell position', async () => {
await commandService.executeCommand(RemoveColCommand.id, {
ranges: [
{ startRow: 1, endRow: 1, startColumn: 9, endColumn: 11 },
{ startRow: 1, endRow: 1, startColumn: 13, endColumn: 14 },
],
} as IRemoveRowColCommandParams);
expect(getMergedInfo(10, 12)).toEqual({ startRow: 10, endRow: 10, startColumn: 10, endColumn: 13 });
expect(getMergedInfo(10, 9)).toEqual({ startRow: 10, endRow: 10, startColumn: 9, endColumn: 10 });
});
});
});
Expand Down
30 changes: 19 additions & 11 deletions packages/sheets/src/commands/commands/remove-row-col.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {
const { workbook, worksheet, subUnitId, unitId } = target;

const filteredRanges: IRange[] = [];
const startColumn = 0;
const endColumn = Math.max(worksheet.getMaxColumns() - 1, 0);

ranges.forEach((range) => {
const filterOutRowsInRemove: number[] = [];
Expand All @@ -89,13 +91,13 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {
filteredRanges.push({
startRow: starts[i],
endRow: ends[i],
startColumn: range.startColumn,
endColumn: range.endColumn,
startColumn,
endColumn,
});
}
}
} else {
filteredRanges.push(range);
filteredRanges.push({ ...range, startColumn, endColumn });
}
});

Expand Down Expand Up @@ -143,7 +145,7 @@ export const RemoveRowCommand: ICommand<IRemoveRowColCommandParams> = {
...(intercepted.preRedos ?? []),
...redos,
...intercepted.redos,
followSelectionOperation(filteredRanges[0], workbook, worksheet),
followSelectionOperation(ranges[0], workbook, worksheet),
],
commandService
);
Expand Down Expand Up @@ -194,21 +196,27 @@ export const RemoveColCommand: ICommand<IRemoveRowColCommandParams> = {
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];

const rowRanges: IRange[] = [];
const startRow = 0;
const endRow = Math.max(worksheet.getMaxRows() - 1, 0);

ranges.forEach((range) => {
range = {
const rowRange = {
...range,
startRow: 0,
endRow: Math.max(worksheet.getMaxRows() - 1, 0),
startRow,
endRow,
};

rowRanges.push(rowRange);

const removeColParams: IRemoveColMutationParams = {
unitId,
subUnitId,
range,
range: rowRange,
};
const undoRemoveColParams: IInsertColMutationParams = RemoveColMutationFactory(accessor, removeColParams);

const removedCols = worksheet.getCellMatrix().getSlice(0, worksheet.getRowCount() - 1, range.startColumn, range.endColumn);
const removedCols = worksheet.getCellMatrix().getSlice(0, worksheet.getRowCount() - 1, rowRange.startColumn, rowRange.endColumn);
const undoSetRangeValuesParams: ISetRangeValuesMutationParams = {
unitId,
subUnitId,
Expand All @@ -224,7 +232,7 @@ export const RemoveColCommand: ICommand<IRemoveRowColCommandParams> = {

const canPerform = await sheetInterceptorService.beforeCommandExecute({
id: RemoveColCommand.id,
params: { ranges } as IRemoveRowColCommandParams,
params: { ranges: rowRanges } as IRemoveRowColCommandParams,
});

if (!canPerform) {
Expand All @@ -233,7 +241,7 @@ export const RemoveColCommand: ICommand<IRemoveRowColCommandParams> = {

const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveColCommand.id,
params: { ranges } as IRemoveRowColCommandParams,
params: { ranges: rowRanges } as IRemoveRowColCommandParams,
});
const commandService = accessor.get(ICommandService);
const result = sequenceExecute(
Expand Down
Loading

0 comments on commit f6b5447

Please sign in to comment.