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
fix(sheets): preserve table borders after filtering (#7971)
wpxp123456caf226c43fbe
diff --git a/packages/sheets/src/models/__tests__/range-theme-model.spec.ts b/packages/sheets/src/models/__tests__/range-theme-model.spec.tsindex 89d8840aaf..d3bb6dbe55 100644--- a/packages/sheets/src/models/__tests__/range-theme-model.spec.ts+++ b/packages/sheets/src/models/__tests__/range-theme-model.spec.ts@@ -16,6 +16,7 @@import type { Injector, IRange, Univer, Workbook } from '@univerjs/core';import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';+import { ISheetRowFilteredService } from '@univerjs/engine-formula';import { afterEach, beforeEach, describe, expect, it } from 'vitest';import { createTestBase, TEST_WORKBOOK_DATA_DEMO } from '../../services/__tests__/util';import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';@@ -28,10 +29,17 @@ describe('SheetRangeThemeModel', () => {let model: SheetRangeThemeModel;let unitId: string;let subUnitId: string;+ let filteredRows: Set<number>;beforeEach(() => {+ filteredRows = new Set();const testBed = createTestBase(TEST_WORKBOOK_DATA_DEMO, [[SheetInterceptorService],+ [ISheetRowFilteredService, {+ useValue: {+ getRowFiltered: (_unitId: string, _subUnitId: string, row: number) => filteredRows.has(row),+ },+ }],[SheetRangeThemeModel],]);@@ -121,6 +129,23 @@ describe('SheetRangeThemeModel', () => {expect((model as any)._zebraCrossingCacheMap.get(unitId)?.get(subUnitId)?.size).toBe(0);});+ it('should apply the last row style to the last visible row after filtering', () => {+ const customTheme = new RangeThemeStyle('filtered-last-row-theme', {+ wholeStyle: { bg: { rgb: '#f5f5f5' } as any },+ lastRowStyle: { bd: { b: { s: 1, cl: { rgb: '#000000' } } } as any },+ });+ model.registerRangeThemeStyle(unitId, customTheme);++ const range: IRange = { startRow: 0, endRow: 3, startColumn: 0, endColumn: 1 };+ model.registerRangeThemeRule('filtered-last-row-theme', { unitId, subUnitId, range });+ filteredRows.add(3);+ model.refreshSheetRowVisibleFuncSet(unitId, subUnitId);+ model.refreshZebraCrossingCacheBySheet(unitId, subUnitId);++ expect(model.getCellStyle(unitId, subUnitId, 2, 0)?.bd?.b).toBeDefined();+ expect(model.getCellStyle(unitId, subUnitId, 3, 0)?.bd?.b).toBeUndefined();+ });+it('should serialize/deserialize and remove unit resources', () => {const customTheme = new RangeThemeStyle('json-theme', {wholeStyle: { bg: { rgb: '#ddddee' } as any },diff --git a/packages/sheets/src/models/range-theme-model.ts b/packages/sheets/src/models/range-theme-model.tsindex cf08203370..a97859d806 100644--- a/packages/sheets/src/models/range-theme-model.ts+++ b/packages/sheets/src/models/range-theme-model.ts@@ -15,12 +15,22 @@*/import type { ICellDataForSheetInterceptor, IRange, Nullable, Workbook } from '@univerjs/core';-import type { IRangeThemeStyleItem, IRangeThemeStyleJSON } from './range-theme-util';-import { Disposable, generateRandomId, Inject, InterceptorEffectEnum, IResourceManagerService, IUniverInstanceService, RTree, UniverInstanceType } from '@univerjs/core';+import {+ Disposable,+ generateRandomId,+ Inject,+ InterceptorEffectEnum,+ IResourceManagerService,+ IUniverInstanceService,+ Optional,+ RTree,+ UniverInstanceType,+} from '@univerjs/core';+import { ISheetRowFilteredService } from '@univerjs/engine-formula';import { Subject } from 'rxjs';+import type { IRangeThemeStyleItem, IRangeThemeStyleJSON } from './range-theme-util';import { INTERCEPTOR_POINT, RangeThemeInterceptorId } from '../services/sheet-interceptor/interceptor-const';import { SheetInterceptorService } from '../services/sheet-interceptor/sheet-interceptor.service';-import { RangeThemeStyle } from './range-theme-util';import { buildInThemes } from './range-themes/build-in-theme.factory';import { defaultRangeThemeStyle, defaultRangeThemeStyleJSONWithLastRowStyle } from './range-themes/default';@@ -62,7 +72,8 @@ export class SheetRangeThemeModel extends Disposable {constructor(@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,@Inject(IResourceManagerService) private _resourceManagerService: IResourceManagerService,- @Inject(IUniverInstanceService) private readonly _univerInstanceService: IUniverInstanceService+ @Inject(IUniverInstanceService) private readonly _univerInstanceService: IUniverInstanceService,+ @Optional(ISheetRowFilteredService) private readonly _sheetRowFilteredService?: ISheetRowFilteredService) {super();this._registerIntercept();@@ -132,12 +143,12 @@ export class SheetRangeThemeModel extends Disposable {if (sheet) {const rowCount = sheet.getRowCount();const rowManager = sheet.getRowManager();- for (let i = 1; i <= rowCount; i++) {+ for (let i = 0; i < rowCount; i++) {const visible = sheet.getRowVisible(i);if (!visible) {set.add(i);} else {- if (rowManager.getRowHeight(i) === 0) {+ if (rowManager.getRowHeight(i) === 0 || this._sheetRowFilteredService?.getRowFiltered(unitId, subUnitId, i)) {set.add(i);}}@@ -313,11 +324,16 @@ export class SheetRangeThemeModel extends Disposable {const offsetRow = row - rangeInfo.range.startRow;const offsetCol = col - rangeInfo.range.startColumn;const theme = this.getRangeThemeStyle(unitId, themeName);+ let lastVisibleRow = rangeInfo.range.endRow;+ const hiddenRows = this._getSheetRowVisibleFuncSet(unitId, subUnitId);+ while (lastVisibleRow >= rangeInfo.range.startRow && hiddenRows.has(lastVisibleRow)) {+ lastVisibleRow--;+ }const zebraCache = this._ensureZebraCrossingCache(unitId, subUnitId, themes[0] as string);const isToggled = zebraCache.getIsToggled(row);if (theme) {- return theme.getStyle(offsetRow, offsetCol, row === rangeInfo.range.endRow, col === rangeInfo.range.endColumn, isToggled);+ return theme.getStyle(offsetRow, offsetCol, row === lastVisibleRow, col === rangeInfo.range.endColumn, isToggled);}}}