Skip to content

Commit

Permalink
fix: 修复明细表自定义多级列头导出格式错误 close #2664 (#2674)
Browse files Browse the repository at this point in the history
fix: 修复明细表自定义列头导出格式错误 close #2664
  • Loading branch information
lijinke666 committed Apr 25, 2024
1 parent a09d1fe commit ae9add9
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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
",
Expand Down Expand Up @@ -408,7 +408,7 @@ Array [
"序号",
"province",
"city",
"产品类型",
"产品类型产品",
"sub_type",
"number
",
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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 [
"省份,子类别,类别,子类别,子类别",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
Expand Down
45 changes: 45 additions & 0 deletions packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
67 changes: 65 additions & 2 deletions packages/s2-core/__tests__/unit/utils/export/export-table-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('TableSheet Export Test', () => {
'序号',
'province',
'city',
'产品类型',
'产品类型产品',
'sub_type',
'number',
]);
Expand Down Expand Up @@ -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({
Expand All @@ -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);
Expand Down Expand Up @@ -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();
},
);
});
53 changes: 23 additions & 30 deletions packages/s2-core/src/utils/export/copy/table-copy.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand All @@ -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[] {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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,
);
}
Expand All @@ -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,
);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/s2-react/playground/components/CustomGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ export const CustomGrid = React.forwardRef<SpreadSheet, CustomGridProps>(
<SheetComponent
{...props}
{...context}
header={{
export: {
open: true,
},
}}
sheetType={sheetType}
dataCfg={dataCfg}
options={options}
Expand Down

0 comments on commit ae9add9

Please sign in to comment.