Skip to content

Commit

Permalink
fix: 修复自定义列头导出数据时, 角头文本展示错误 close #2844 (#2869)
Browse files Browse the repository at this point in the history
* fix: 修复自定义列头导出数据时, 角头文本展示错误 close #2844

* chore: 还原

* test: 更新快照
  • Loading branch information
lijinke666 authored Aug 23, 2024
1 parent 8684fb2 commit 63dba54
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
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
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

0 comments on commit 63dba54

Please sign in to comment.