Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复自定义列头导出数据时, 角头文本展示错误 close #2844 #2869

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的快照是错的

type sub_type 指标1 指标2
家具 桌子 13 2 - -
家具 椅子 11 8 - -"
Expand All @@ -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 笔 笔",
Expand Down
30 changes: 30 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 @@ -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);
});
});
13 changes: 10 additions & 3 deletions packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
Loading