From a04de6bb78327377fbcf2c4e9952f044f81f1aed Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Mon, 19 Aug 2024 18:20:12 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=88=97=E5=A4=B4=E5=AF=BC=E5=87=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=97=B6,=20=E8=A7=92=E5=A4=B4=E6=96=87=E6=9C=AC=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=E9=94=99=E8=AF=AF=20close=20#2844?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/data/custom-grid-fields.ts | 2 +- .../__snapshots__/export-pivot-spec.ts.snap | 24 ++++++++++++++- .../unit/utils/export/export-pivot-spec.ts | 30 +++++++++++++++++++ .../utils/export/copy/pivot-data-cell-copy.ts | 13 ++++++-- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/packages/s2-core/__tests__/data/custom-grid-fields.ts b/packages/s2-core/__tests__/data/custom-grid-fields.ts index 277275fdc7..601db74667 100644 --- a/packages/s2-core/__tests__/data/custom-grid-fields.ts +++ b/packages/s2-core/__tests__/data/custom-grid-fields.ts @@ -108,7 +108,7 @@ export const customRowGridFields: S2DataConfig['fields'] = { }; export const customColGridFields: S2DataConfig['fields'] = { - rows: ['type', 'sub_type'], + rows: ['type'], columns: customGridTreeNodes, values: ['measure-1', 'measure-2', 'measure-3', 'measure-4', 'measure-5'], valueInCols: true, diff --git a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap index c8675559a2..b8f97942dd 100644 --- a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap +++ b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap @@ -228,7 +228,7 @@ province city 数值 数值 数值 数值 exports[`PivotSheet Export Test should export correct data with formatter for custom column headers 1`] = ` " 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 - 自定义节点 a-2 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 + 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 type sub_type 指标1 指标2 家具 桌子 13 2 - - 家具 椅子 11 8 - -" @@ -243,6 +243,28 @@ exports[`PivotSheet Export Test should export correct data with formatter for cu 自定义节点 a-2 - -" `; +exports[`PivotSheet Export Test should export correctly corner text for custom columns 1`] = ` +" 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 + 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 +type sub_type 指标1 指标2 +家具 桌子 13 2 - - +家具 椅子 11 8 - -" +`; + +exports[`PivotSheet Export Test should export correctly corner text for custom columns and signle rows 1`] = ` +"自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 +自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 +type 指标1 指标2 +家具 11 8 - -" +`; + +exports[`PivotSheet Export Test should export correctly corner text for custom columns and single rows 1`] = ` +"自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 +自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 +type 指标1 指标2 +家具 11 8 - -" +`; + exports[`PivotSheet Export Test should export correctly data for single row data by { async: false } 1`] = ` Array [ " type 笔 笔", diff --git a/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts b/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts index 67dff86427..ab3f56c7d5 100644 --- a/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts @@ -575,4 +575,34 @@ describe('PivotSheet Export Test', () => { formatData: true, }); }); + + it('should export correctly corner text for custom columns', async () => { + const sheet = new PivotSheet( + getContainer(), + { + data: CustomGridData, + fields: customColGridSimpleFields, + }, + assembleOptions(), + ); + + await expectMatchSnapshot(sheet); + }); + + // https://github.com/antvis/S2/issues/2844 + it('should export correctly corner text for custom columns and single rows', async () => { + const sheet = new PivotSheet( + getContainer(), + { + data: CustomGridData, + fields: { + ...customColGridSimpleFields, + rows: ['type'], + }, + }, + assembleOptions(), + ); + + await expectMatchSnapshot(sheet); + }); }); diff --git a/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts b/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts index ef1e0b6387..0d1cf0e4b8 100644 --- a/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts +++ b/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts @@ -305,16 +305,23 @@ export class PivotDataCellCopy extends BaseDataCellCopy { return this.getCustomRowCornerMatrix(rowMatrix); } + const { colsHierarchy } = this.spreadsheet.facet.getLayoutResult(); const { fields } = this.spreadsheet.dataCfg; const { columns = [], rows = [] } = fields; - // 为了对齐数值 - const customColumns = [...columns, '']; + // 为了对齐数值, 增加 "" 占位 + // 自定义列头不需要占位, 但是为树状结构, 需要根据采样节点解析: https://github.com/antvis/S2/issues/2844) + const customColumns = this.spreadsheet.isCustomColumnFields() + ? colsHierarchy + .getNodes() + .slice(0, colsHierarchy.sampleNodesForAllLevels.length) + .map((node) => node.field) + : [...columns, '']; const maxRowLen = this.spreadsheet.isHierarchyTreeType() ? getMaxRowLen(rowMatrix ?? []) : rows.length; const customRows = slice(rows, 0, maxRowLen); - /* + /** * cornerMatrix 形成的矩阵为 rows.length(宽) * columns.length(高) */ return map(customColumns, (colField, colIndex) => From e746a55862f72a90d5140678017c7ec1751336cd Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Mon, 19 Aug 2024 18:24:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=E8=BF=98=E5=8E=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/s2-core/__tests__/data/custom-grid-fields.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/s2-core/__tests__/data/custom-grid-fields.ts b/packages/s2-core/__tests__/data/custom-grid-fields.ts index 601db74667..277275fdc7 100644 --- a/packages/s2-core/__tests__/data/custom-grid-fields.ts +++ b/packages/s2-core/__tests__/data/custom-grid-fields.ts @@ -108,7 +108,7 @@ export const customRowGridFields: S2DataConfig['fields'] = { }; export const customColGridFields: S2DataConfig['fields'] = { - rows: ['type'], + rows: ['type', 'sub_type'], columns: customGridTreeNodes, values: ['measure-1', 'measure-2', 'measure-3', 'measure-4', 'measure-5'], valueInCols: true, From 3ad37d1b369181a536b9b4e0570ed4002dd41d83 Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Thu, 22 Aug 2024 11:11:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=BF=AB?= =?UTF-8?q?=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/export/__snapshots__/export-pivot-spec.ts.snap | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap index b8f97942dd..e5aab4ae9b 100644 --- a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap +++ b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap @@ -251,13 +251,6 @@ type sub_type 指标1 指标2 家具 椅子 11 8 - -" `; -exports[`PivotSheet Export Test should export correctly corner text for custom columns and signle rows 1`] = ` -"自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 -自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 -type 指标1 指标2 -家具 11 8 - -" -`; - exports[`PivotSheet Export Test should export correctly corner text for custom columns and single rows 1`] = ` "自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2