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..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 @@ -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,21 @@ 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 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) =>