From ae9add9c722877aced481340f798408a09ad98fb Mon Sep 17 00:00:00 2001 From: Jinke Li Date: Thu, 25 Apr 2024 10:11:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=98=8E=E7=BB=86?= =?UTF-8?q?=E8=A1=A8=E8=87=AA=E5=AE=9A=E4=B9=89=E5=A4=9A=E7=BA=A7=E5=88=97?= =?UTF-8?q?=E5=A4=B4=E5=AF=BC=E5=87=BA=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= =?UTF-8?q?=20close=20#2664=20(#2674)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 修复明细表自定义列头导出格式错误 close #2664 --- .../__snapshots__/export-pivot-spec.ts.snap | 21 ++++++ .../export/__snapshots__/export-spec.ts.snap | 4 +- .../__snapshots__/export-table-spec.ts.snap | 30 +++++++-- .../unit/utils/export/export-pivot-spec.ts | 45 +++++++++++++ .../unit/utils/export/export-table-spec.ts | 67 ++++++++++++++++++- .../src/utils/export/copy/table-copy.ts | 53 +++++++-------- .../playground/components/CustomGrid.tsx | 5 ++ 7 files changed, 186 insertions(+), 39 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 f0c57844d7..5223ccb987 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 @@ -21,3 +21,24 @@ type sub_type 数值 数值 数值 数值 数值 数值 数值 数值 办公用品 笔 945 1304 1145 1432 2335 245 2457 2458 办公用品 纸张 1343 1354 1523 1634 4004 3077 3551 352" `; + +exports[`PivotSheet Export Test should export correct data with formatter for custom column headers 1`] = ` +Array [ + " 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-1 自定义节点 a-2", + " 自定义节点 a-2 自定义节点 a-1-1 自定义节点 a-1-1 自定义节点 a-1-2 ", + "type sub_type 指标1 指标2 ", + "家具 桌子 13 2 ", + "家具 椅子 11 8 ", +] +`; + +exports[`PivotSheet Export Test should export correct data with formatter for custom row headers 1`] = ` +Array [ + " type 家具 家具", + "自定义节点 a-1 自定义节点 a-1-1 指标1 桌子 椅子", + "自定义节点 a-1 自定义节点 a-1-1 指标1 13 11", + "自定义节点 a-1 自定义节点 a-1-1 指标2 2 8", + "自定义节点 a-1 自定义节点 a-1-2 ", + "自定义节点 a-2 ", +] +`; diff --git a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-spec.ts.snap b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-spec.ts.snap index c09b9e7171..9d1d366c5a 100644 --- a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-spec.ts.snap +++ b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-spec.ts.snap @@ -245,7 +245,7 @@ Array [ exports[`TableSheet Export Test should export correct data with series number 1`] = ` Array [ - "序号 province city 产品类型 sub_type number + "序号 province city 产品类型产品 sub_type number ", "1 浙江省 杭州市 家具 桌子 7789 ", @@ -408,7 +408,7 @@ Array [ "序号", "province", "city", - "产品类型", + "产品类型产品", "sub_type", "number ", diff --git a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-table-spec.ts.snap b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-table-spec.ts.snap index 9045c3229f..1ccf6d70a4 100644 --- a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-table-spec.ts.snap +++ b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-table-spec.ts.snap @@ -1,8 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`TableSheet Export Test should export correct data When the split separator is configured 1`] = ` +exports[`TableSheet Export Test should export correct data when the split separator is configured 1`] = ` Array [ - "province,city,type,sub_type,number", + "省份,城市,类别,子类别,数量", "浙江省,杭州市,家具,桌子,7789", "浙江省,绍兴市,家具,桌子,2367", "浙江省,宁波市,家具,桌子,3877", @@ -93,6 +93,26 @@ Array [ ] `; +exports[`TableSheet Export Test should export correct data with formatter for custom column headers by { formatOptions: false } 1`] = ` +Array [ + "板块 二级板块 二级板块 二级板块 二级板块", + " 上年度收入基数 本年度收入指标 本年累计收入指标 收入增长率指标", + "集团 0.1 0.2 3222 55555", + "集团1 0.1 0.2 3222 55555", + "集团2 0.1 0.2 3222 55555", +] +`; + +exports[`TableSheet Export Test should export correct data with formatter for custom column headers by { formatOptions: true } 1`] = ` +Array [ + "板块 二级板块 二级板块 二级板块 二级板块", + " 上年度收入基数 本年度收入指标 本年累计收入指标 收入增长率指标", + "集团 0.1 0.2 3222 55555", + "集团1 0.1 0.2 3222 55555", + "集团2 0.1 0.2 3222 55555", +] +`; + exports[`TableSheet Export Test should export correct data with formatter if contain repeat column name 1`] = ` Array [ "省份,子类别,类别,子类别,子类别", @@ -178,7 +198,7 @@ Array [ exports[`TableSheet Export Test should export correct data with series number 1`] = ` Array [ - "序号 province city 产品类型 sub_type number", + "序号 province city 产品类型产品 sub_type number", "1 浙江省 杭州市 家具 桌子 7789", "2 浙江省 绍兴市 家具 桌子 2367", "3 浙江省 宁波市 家具 桌子 3877", @@ -188,7 +208,7 @@ Array [ exports[`TableSheet Export Test should export correct data with series number 2`] = ` Array [ - "序号 province city 产品类型 sub_type number", + "序号 province city 产品类型产品 sub_type number", "1 浙江省 杭州市 家具 桌子 7789", "2 浙江省 绍兴市 家具 桌子 2367", "3 浙江省 宁波市 家具 桌子 3877", @@ -197,7 +217,7 @@ Array [ `; exports[`TableSheet Export Test should export correct data with totals 1`] = ` -"province type sub_type number +"province-province type-type sub_type number 浙江省-province 家具-type 桌子 7789 浙江省-province 家具-type 桌子 2367 浙江省-province 家具-type 桌子 3877 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 f0d4d23ad1..fc251db914 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 @@ -3,6 +3,11 @@ import { map, omit } from 'lodash'; import { data as originData } from 'tests/data/mock-dataset.json'; import { assembleDataCfg, assembleOptions } from 'tests/util'; import { getContainer } from 'tests/util/helpers'; +import { + customColGridSimpleFields, + customRowGridSimpleFields, +} from '../../../data/custom-grid-simple-fields'; +import { CustomGridData } from '../../../data/data-custom-grid'; import { CopyMIMEType } from '@/common/interface/export'; describe('PivotSheet Export Test', () => { @@ -556,4 +561,44 @@ describe('PivotSheet Export Test', () => { 四川省,乐山市,2330,2445,2458,352" `); }); + + it('should export correct data with formatter for custom row headers', async () => { + const sheet = new PivotSheet( + getContainer(), + { + data: CustomGridData, + fields: customRowGridSimpleFields, + }, + assembleOptions(), + ); + + await sheet.render(); + const data = await asyncGetAllPlainData({ + sheetInstance: sheet, + split: '\t', + formatOptions: true, + }); + + expect(data.split(NewLine)).toMatchSnapshot(); + }); + + it('should export correct data with formatter for custom column headers', async () => { + const sheet = new PivotSheet( + getContainer(), + { + data: CustomGridData, + fields: customColGridSimpleFields, + }, + assembleOptions(), + ); + + await sheet.render(); + const data = await asyncGetAllPlainData({ + sheetInstance: sheet, + split: '\t', + formatOptions: true, + }); + + expect(data.split(NewLine)).toMatchSnapshot(); + }); }); diff --git a/packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts b/packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts index fcb03b61d8..c24cffbd3e 100644 --- a/packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts @@ -51,7 +51,7 @@ describe('TableSheet Export Test', () => { '序号', 'province', 'city', - '产品类型', + '产品类型产品', 'sub_type', 'number', ]); @@ -184,7 +184,7 @@ describe('TableSheet Export Test', () => { }); // https://github.com/antvis/S2/issues/2236 - it('should export correct data When the split separator is configured', async () => { + it('should export correct data when the split separator is configured', async () => { const tableSheet = new TableSheet( getContainer(), assembleDataCfg({ @@ -199,6 +199,7 @@ describe('TableSheet Export Test', () => { const data = await asyncGetAllPlainData({ sheetInstance: tableSheet, split: ',', + formatOptions: true, }); // 只取前10行数据 const result = slice(data.split(NewLine), 0, 5); @@ -300,4 +301,66 @@ describe('TableSheet Export Test', () => { expect(data.split(NewLine)).toMatchSnapshot(); }); + + // https://github.com/antvis/S2/issues/2664 + it.each([{ formatOptions: true }, { formatOptions: false }])( + 'should export correct data with formatter for custom column headers by %o', + async (options) => { + const tableSheet = new TableSheet( + getContainer(), + assembleDataCfg({ + meta: [], + fields: { + columns: [ + { field: 'name1', title: '板块' }, + { + field: 'name2', + title: '二级板块', + children: [ + { field: 'IncomeQuota', title: '上年度收入基数' }, + { field: 'Quota', title: '本年度收入指标' }, + { field: 'CheckTotal', title: '本年累计收入指标' }, + { field: 'GrowthRate', title: '收入增长率指标' }, + ], + }, + ], + }, + data: [ + { + name1: '集团', + IncomeQuota: 0.1, + Quota: 0.2, + CheckTotal: 3222, + GrowthRate: 55555, + }, + { + name1: '集团1', + IncomeQuota: 0.1, + Quota: 0.2, + CheckTotal: 3222, + GrowthRate: 55555, + }, + { + name1: '集团2', + IncomeQuota: 0.1, + Quota: 0.2, + CheckTotal: 3222, + GrowthRate: 55555, + }, + ], + }), + assembleOptions(), + ); + + await tableSheet.render(); + const data = await asyncGetAllPlainData({ + sheetInstance: tableSheet, + split: '\t', + ...options, + }); + + // 自定义列头, 不管有没有开启格式化, 配置 meta, 都使用 field.title 展示 + expect(data.split(NewLine)).toMatchSnapshot(); + }, + ); }); diff --git a/packages/s2-core/src/utils/export/copy/table-copy.ts b/packages/s2-core/src/utils/export/copy/table-copy.ts index ff447f79f6..e475a1c6e9 100644 --- a/packages/s2-core/src/utils/export/copy/table-copy.ts +++ b/packages/s2-core/src/utils/export/copy/table-copy.ts @@ -1,27 +1,27 @@ -import { map } from 'lodash'; -import type { SpreadSheet } from '../../../sheet-type'; +import { map, zip } from 'lodash'; import { + AsyncRenderThreshold, + SERIES_NUMBER_FIELD, + getDefaultSeriesNumberText, type CellMeta, type RawData, - getDefaultSeriesNumberText, - SERIES_NUMBER_FIELD, - AsyncRenderThreshold, } from '../../../common'; -import type { Node } from '../../../facet/layout/node'; import type { - CopyableList, CopyAllDataParams, + CopyableList, SheetCopyConstructorParams, } from '../../../common/interface/export'; +import type { Node } from '../../../facet/layout/node'; +import type { SpreadSheet } from '../../../sheet-type'; import { convertString, getColNodeFieldFromNode, getSelectedCols, getSelectedRows, } from '../method'; -import { assembleMatrix, getFormatter } from './common'; -import { getHeaderNodeFromMeta } from './core'; import { BaseDataCellCopy } from './base-data-cell-copy'; +import { assembleMatrix, getFormatter, getNodeFormatData } from './common'; +import { getHeaderNodeFromMeta } from './core'; class TableDataCellCopy extends BaseDataCellCopy { private displayData: RawData[]; @@ -37,13 +37,13 @@ class TableDataCellCopy extends BaseDataCellCopy { private getSelectedColNodes(): Node[] { const selectedCols = getSelectedCols(this.config.selectedCells); - const allColNodes = this.spreadsheet.facet.getColNodes(); + const colLeafNodes = this.spreadsheet.facet.getColLeafNodes(); if (selectedCols.length === 0) { - return allColNodes; + return colLeafNodes; } - return map(selectedCols, (meta) => allColNodes[meta.colIndex]); + return map(selectedCols, (meta) => colLeafNodes[meta.colIndex]); } private getSelectedDisplayData(): RawData[] { @@ -146,21 +146,14 @@ class TableDataCellCopy extends BaseDataCellCopy { return SERIES_NUMBER_FIELD === field && seriesNumber?.enable; } - private getColMatrix(): string[] { - const { formatHeader } = this.config; - - // 明细表的表头,没有格式化 - return this.columnNodes.map((node) => { - const field: string = node.field; - - if (!formatHeader) { - return field; - } - - return this.isSeriesNumberField(field) - ? getDefaultSeriesNumberText() - : this.spreadsheet.dataSet.getFieldName(field); - }) as string[]; + private getColMatrix(): string[][] { + return zip( + ...this.columnNodes.map((node) => { + return this.isSeriesNumberField(node.field) + ? [getDefaultSeriesNumberText()] + : getNodeFormatData(node); + }), + ) as string[][]; } private getValueFromMeta = (meta: CellMeta) => { @@ -198,7 +191,7 @@ class TableDataCellCopy extends BaseDataCellCopy { const colMatrix = this.getColMatrix(); return this.matrixTransformer( - assembleMatrix({ colMatrix: [colMatrix], dataMatrix }), + assembleMatrix({ colMatrix, dataMatrix }), this.config.separator, ); } @@ -218,7 +211,7 @@ class TableDataCellCopy extends BaseDataCellCopy { const colMatrix = this.getColMatrix(); return this.matrixTransformer( - assembleMatrix({ colMatrix: [colMatrix], dataMatrix: matrix }), + assembleMatrix({ colMatrix, dataMatrix: matrix }), this.config.separator, ); } @@ -235,7 +228,7 @@ class TableDataCellCopy extends BaseDataCellCopy { const colMatrix = this.getColMatrix(); return this.matrixTransformer( - assembleMatrix({ colMatrix: [colMatrix], dataMatrix: matrix }), + assembleMatrix({ colMatrix, dataMatrix: matrix }), this.config.separator, ); } diff --git a/packages/s2-react/playground/components/CustomGrid.tsx b/packages/s2-react/playground/components/CustomGrid.tsx index 8e70775f68..32d1de0e9b 100644 --- a/packages/s2-react/playground/components/CustomGrid.tsx +++ b/packages/s2-react/playground/components/CustomGrid.tsx @@ -170,6 +170,11 @@ export const CustomGrid = React.forwardRef(