Skip to content

Commit

Permalink
fix: 修复 enableCopy 和 hideMeasureColumn 都开启为 true 时,复制报错问题 (#1984)
Browse files Browse the repository at this point in the history
* fix: 修复 enableCopy 和 hideMeasureColumn 都开启为 true 时,复制报错问题

* test: 添加单测

Co-authored-by: zishang <[email protected]>
  • Loading branch information
stone-lyl and zishang authored Dec 9, 2022
1 parent 7835214 commit b799ea4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 15 deletions.
101 changes: 86 additions & 15 deletions packages/s2-core/__tests__/unit/utils/export/copy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,28 @@ describe('Pivot Table Core Data Process', () => {
// 2 = ['province', 'city'].length 列头宽度
const ROW_HEADER_WIDTH = 2;

const s2 = new PivotSheet(
getContainer(),
assembleDataCfg({
function getDataCfg() {
return assembleDataCfg({
meta: [],
fields: {
columns: ['type', 'sub_type'],
rows: ['province', 'city'],
values: ['number'],
},
}),
assembleOptions({
});
}

function getOptions() {
return assembleOptions({
hierarchyType: 'grid',
interaction: {
enableCopy: true,
},
totals: TOTALS_OPTIONS,
}),
);
});
}

const s2 = new PivotSheet(getContainer(), getDataCfg(), getOptions());
s2.render();

it('should copy no data in grid mode', () => {
Expand Down Expand Up @@ -392,14 +396,7 @@ describe('Pivot Table Core Data Process', () => {
it('should copy row data in grid mode', () => {
const ss = new PivotSheet(
getContainer(),
assembleDataCfg({
meta: [],
fields: {
columns: ['type', 'sub_type'],
rows: ['province', 'city'],
values: ['number'],
},
}),
getDataCfg(),
assembleOptions({
hierarchyType: 'grid',
interaction: {
Expand Down Expand Up @@ -707,6 +704,80 @@ describe('Pivot Table Core Data Process', () => {
const data = getSelectedData(s2New);
expect(data).toBe(convertString(`7789\n元`));
});

it('should get correct data with hideMeasureColumn is true', () => {
const ss = new PivotSheet(getContainer(), getDataCfg(), getOptions());
ss.setOptions({
style: {
colCfg: {
hideMeasureColumn: true,
},
},
});
ss.render();
const cells = ss.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.DATA_CELL);
ss.interaction.changeState({
cells: map(cells, getCellMeta),
stateName: InteractionStateName.SELECTED,
});
const data = getSelectedData(ss);
expect(data).toMatchInlineSnapshot(`
"7789 5343 13132 945 1343
2367 632 2999 1304 1354
3877 7234 11111 1145 1523
4342 834 5176 1432 1634
18375 14043 32418 4826 5854
1723 2451 4174 2335 4004
1822 2244 4066 245 3077
1943 2333 4276 2457 3551
2330 2445 4775 2458 352
7818 9473 17291 7495 10984
26193 23516 49709 12321 16838"
`);
});

// https://github.com/antvis/S2/issues/1955
it('should get correct data with hideMeasureColumn、showSeriesNumber and copyWithHeader are all true', () => {
const ss = new PivotSheet(getContainer(), getDataCfg(), getOptions());
ss.setOptions({
style: {
colCfg: {
hideMeasureColumn: true,
},
},
interaction: {
enableCopy: true,
copyWithHeader: true,
},
showSeriesNumber: true,
});
ss.render();
const cells = ss.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.DATA_CELL);
ss.interaction.changeState({
cells: map(cells, getCellMeta),
stateName: InteractionStateName.SELECTED,
});
const data = getSelectedData(ss);
expect(data).toMatchInlineSnapshot(`
" 家具 家具 家具 办公用品
桌子 沙发 小计 笔
浙江省 杭州市 7789 5343 13132 945
浙江省 绍兴市 2367 632 2999 1304
浙江省 宁波市 3877 7234 11111 1145
浙江省 舟山市 4342 834 5176 1432
浙江省 小计 18375 14043 32418 4826
四川省 成都市 1723 2451 4174 2335
四川省 绵阳市 1822 2244 4066 245
四川省 南充市 1943 2333 4276 2457
四川省 乐山市 2330 2445 4775 2458
四川省 小计 7818 9473 17291 7495
总计 26193 23516 49709 12321"
`);
});
});

describe('List Table getCopyData', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/s2-core/src/utils/export/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
CellTypes,
CopyType,
EMPTY_PLACEHOLDER,
EXTRA_FIELD,
ID_SEPARATOR,
InteractionStateName,
VALUE_FIELD,
Expand Down Expand Up @@ -68,17 +69,37 @@ const getFormat = (colIndex: number, spreadsheet: SpreadSheet) => {
return (v: string) => v;
};

/**
* 兼容 hideMeasureColumn 方案:hideMeasureColumn 的隐藏实现是通过截取掉度量(measure)数据,但是又只截取了 Node 中的,像 pivotMeta 中的又是完整的。导致复制时,无法通过 Node 找出正确路径。
* https://github.com/antvis/S2/issues/1955
* @param spreadsheet
*/
const compatibleHideMeasureColumn = (spreadsheet: SpreadSheet) => {
const isHideMeasureColumn =
spreadsheet.options?.style?.colCfg?.hideMeasureColumn &&
spreadsheet.isValueInCols();
// 被 hideMeasureColumn 隐藏的 度量(measure) 值,手动添加上。
return isHideMeasureColumn
? {
[EXTRA_FIELD]: spreadsheet.dataCfg.fields.values[0],
}
: {};
};

const getValueFromMeta = (
meta: CellMeta,
displayData: DataType[],
spreadsheet: SpreadSheet,
) => {
if (spreadsheet.isPivotMode()) {
const [rowNode, colNode] = getHeaderNodeFromMeta(meta, spreadsheet);
const measureQuery = compatibleHideMeasureColumn(spreadsheet);

const cell = spreadsheet.dataSet.getCellData({
query: {
...rowNode.query,
...colNode.query,
...measureQuery,
},
rowNode,
isTotals:
Expand Down

0 comments on commit b799ea4

Please sign in to comment.