diff --git a/packages/s2-core/__tests__/unit/cell/header-cell-spec.ts b/packages/s2-core/__tests__/unit/cell/header-cell-spec.ts index 19cd740a43..79c7f4ec74 100644 --- a/packages/s2-core/__tests__/unit/cell/header-cell-spec.ts +++ b/packages/s2-core/__tests__/unit/cell/header-cell-spec.ts @@ -53,6 +53,36 @@ describe('header cell formatter test', () => { expect(rowCell.getFieldValue()).toBe('杭州1'); }); + test('should not format pivot col and row total cell', () => { + const colNode = new Node({ + id: `root[&]总计`, + key: '', + value: '总计', + parent: root, + label: '总计', + isTotals: true, + }); + const rowNode = new Node({ + id: `root[&]杭州[&]小计`, + key: '', + value: '小计', + parent: root, + label: '小计', + isTotals: true, + }); + + const formatter: Formatter = (value) => { + return `${value}1`; + }; + jest.spyOn(s2.dataSet, 'getFieldFormatter').mockReturnValue(formatter); + + const colCell = new ColCell(colNode, s2); + const rowCell = new RowCell(rowNode, s2); + + expect(colCell.getFieldValue()).toBe('总计'); + expect(rowCell.getFieldValue()).toBe('小计'); + }); + test('pivot corner cell not formatter', () => { const formatter: Formatter = (value) => { return `${value}1`; diff --git a/packages/s2-core/src/cell/header-cell.ts b/packages/s2-core/src/cell/header-cell.ts index 0e639007dd..24fd214b00 100644 --- a/packages/s2-core/src/cell/header-cell.ts +++ b/packages/s2-core/src/cell/header-cell.ts @@ -85,16 +85,17 @@ export abstract class HeaderCell extends BaseCell { } protected getFormattedFieldValue(): FormatResult { - const { label } = this.meta; + const { label, isTotals } = this.meta; const formatter = this.spreadsheet.dataSet.getFieldFormatter( this.meta.field, ); - const isTableMode = this.spreadsheet.isTableMode(); + // 如果是 总计/小计 文字单元格,不需要被格式化 // 如果是 table mode,列头不需要被格式化 + const isTableMode = this.spreadsheet.isTableMode(); const formattedValue = - formatter && !isTableMode + formatter && !isTableMode && !isTotals ? formatter(label, undefined, this.meta) : label;