From 6e982917d15ad66d6caecac6da30050527811875 Mon Sep 17 00:00:00 2001 From: Wenjun Xu <906626481@qq.com> Date: Mon, 15 May 2023 12:00:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=80=BB=E8=AE=A1?= =?UTF-8?q?=E5=B0=8F=E8=AE=A1=E8=A2=AB=E6=84=8F=E5=A4=96=20format=20?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#2209)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/unit/cell/header-cell-spec.ts | 30 +++++++++++++++++++ packages/s2-core/src/cell/header-cell.ts | 7 +++-- 2 files changed, 34 insertions(+), 3 deletions(-) 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;