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-filter): recalculate after auto-extending filter range (#7760)

Lucienfaec65c8c518
diff --git a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts
index e55abc58fd..e62fda1642 100644
--- a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts
+++ b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts
@@ -17,7 +17,7 @@
/* eslint-disable perfectionist/sort-imports */
import type { IWorkbookData, Workbook } from '@univerjs/core';
-import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, LocaleType, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
+import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, LocaleType, Plugin, RANGE_TYPE, RedoCommandId, UndoCommand, UndoCommandId, Univer, UniverInstanceType } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { CopySheetCommand, InsertColByRangeCommand, InsertColMutation, InsertRowByRangeCommand, InsertSheetMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetLazyExecuteScheduleService, SheetRangeThemeModel, SheetsSelectionsService, ZebraCrossingCacheController } from '@univerjs/sheets';
@@ -307,6 +307,50 @@ describe('test controller of sheets filter', () => {
expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getRange())
.toEqual({ startRow: 3, startColumn: 0, endRow: 6, endColumn: 5 });
});
+
+ it('should apply existing criteria to newly included rows', async () => {
+ const filterModel = sheetsFilterService.getFilterModel('test', 'sheet1')!;
+ expect(filterModel.filteredOutRows.has(6)).toBe(false);
+
+ expect(await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: { 6: { 0: { v: 'test' } } },
+ } as ISetRangeValuesMutationParams)).toBeTruthy();
+
+ expect(filterModel.getRange().endRow).toBe(6);
+ expect(filterModel.filteredOutRows.has(6)).toBe(true);
+ });
+
+ it('should not extend the range when restoring values during undo', async () => {
+ const filterModel = sheetsFilterService.getFilterModel('test', 'sheet1')!;
+ const originalRange = { ...filterModel.getRange() };
+ const originalFilteredRows = [...filterModel.filteredOutRows];
+
+ expect(await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: { 6: { 0: { v: 'test' } } },
+ trigger: UndoCommandId,
+ } as ISetRangeValuesMutationParams)).toBeTruthy();
+
+ expect(filterModel.getRange()).toEqual(originalRange);
+ expect([...filterModel.filteredOutRows]).toEqual(originalFilteredRows);
+ });
+
+ it('should apply criteria when redo extends the range', async () => {
+ const filterModel = sheetsFilterService.getFilterModel('test', 'sheet1')!;
+
+ expect(await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: { 6: { 0: { v: 'test' } } },
+ trigger: RedoCommandId,
+ } as ISetRangeValuesMutationParams)).toBeTruthy();
+
+ expect(filterModel.getRange().endRow).toBe(6);
+ expect(filterModel.filteredOutRows.has(6)).toBe(true);
+ });
});
describe('test interceptor of remove-rows-command', () => {
diff --git a/packages/sheets-filter/src/controllers/sheets-filter.controller.ts b/packages/sheets-filter/src/controllers/sheets-filter.controller.ts
index 991bf63cd4..d2fbb6af0d 100644
--- a/packages/sheets-filter/src/controllers/sheets-filter.controller.ts
+++ b/packages/sheets-filter/src/controllers/sheets-filter.controller.ts
@@ -54,6 +54,7 @@ import {
moveMatrixArray,
Optional,
Rectangle,
+ UndoCommandId,
UniverInstanceType,
} from '@univerjs/core';
import { DataSyncPrimaryController } from '@univerjs/rpc';
@@ -912,7 +913,8 @@ export class SheetsFilterController extends Disposable {
}
// extend filter range when set range values
- if (command.id === SetRangeValuesMutation.id && !options?.onlyLocal) {
+ const trigger = (command.params as ISetRangeValuesMutationParams | undefined)?.trigger;
+ if (command.id === SetRangeValuesMutation.id && !options?.onlyLocal && trigger !== UndoCommandId) {
const extendRegion = this._getExtendRegion(unitId, subUnitId);
if (extendRegion) {
const cellValue = (command.params as ISetRangeValuesMutationParams).cellValue;
@@ -929,7 +931,9 @@ export class SheetsFilterController extends Disposable {
...filterRange,
endRow: extendedRange.endRow,
});
+ filterModel.reCalc();
this._registerRefRange(unitId, subUnitId);
+ break;
}
}
}