Skip to content

Commit

Permalink
fix: 修复自定义计算总计时, 复制的数据不正确 close #2928
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Oct 23, 2024
1 parent c40bbf9 commit a13eb27
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ province city number number number number
四川省 乐山市 2330 2445 2458 352"
`;

exports[`PivotSheet Export Test should export correct data in grid mode by custom calc grand totals 1`] = `
"省份 城市 总计 数量
浙江省 杭州市 15420 15420
浙江省 绍兴市 5657 5657
浙江省 宁波市 13779 13779
浙江省 舟山市 8242 8242
四川省 成都市 10513 10513
四川省 绵阳市 7388 7388
四川省 南充市 10284 10284
四川省 乐山市 7585 7585
总计 78868 78868"
`;

exports[`PivotSheet Export Test should export correct data in grid mode with totals in col 1`] = `
" 类别 家具 家具 家具 办公用品 办公用品 办公用品 总计
子类别 桌子 沙发 小计 笔 纸张 小计
Expand Down
44 changes: 41 additions & 3 deletions packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { data as originData } from 'tests/data/mock-dataset.json';
import { assembleDataCfg, assembleOptions } from 'tests/util';
import { createPivotSheet, getContainer } from 'tests/util/helpers';
import {
Aggregation,
PivotSheet,
asyncGetAllPlainData,
type DataItem,
Expand Down Expand Up @@ -49,19 +50,20 @@ describe('PivotSheet Export Test', () => {
);

await s2.render();
const data = await asyncGetAllPlainData({
const syncData = await asyncGetAllPlainData({
sheetInstance: s2,
split: TAB_SEPARATOR,
formatOptions: true,
async: true,
});

expect(data).toMatchSnapshot();
expect(syncData).toMatchSnapshot();

const asyncData = await asyncGetAllPlainData({
sheetInstance: s2,
split: TAB_SEPARATOR,
formatOptions: true,
async: true,
async: false,
});

expect(asyncData).toMatchSnapshot();
Expand Down Expand Up @@ -605,4 +607,40 @@ describe('PivotSheet Export Test', () => {

await expectMatchSnapshot(sheet);
});

// https://github.com/antvis/S2/issues/2928
it('should export correct data in grid mode by custom calc grand totals', async () => {
const sheet = new PivotSheet(
getContainer(),
assembleDataCfg({
fields: {
rows: ['province', 'city'],
columns: [],
values: ['number'],
valueInCols: true,
},
}),
assembleOptions({
hierarchyType: 'grid',
totals: {
col: {
showGrandTotals: true,
showSubTotals: true,
reverseGrandTotalsLayout: true,
calcGrandTotals: {
aggregation: Aggregation.SUM,
},
},
row: {
showGrandTotals: true,
calcGrandTotals: {
aggregation: Aggregation.AVG,
},
},
},
}),
);

await expectMatchSnapshot(sheet);
});
});
27 changes: 16 additions & 11 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 @@ -24,9 +24,10 @@ import type {
MeasureQuery,
SheetCopyConstructorParams,
} from '../../../common/interface/export';
import type { CellData } from '../../../data-set';
import type { CellData, Query } from '../../../data-set';
import type { Node } from '../../../facet/layout/node';
import type { SpreadSheet } from '../../../sheet-type';
import { getHeaderTotalStatus } from '../../dataset/pivot-data-set';
import {
convertString,
getColNodeFieldFromNode,
Expand Down Expand Up @@ -213,18 +214,22 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
};
}): DataItem => {
const { measureQuery } = config;
const query: Query = {
...rowNode.query,
...colNode.query,
...measureQuery,
};
const isTotals =
rowNode.isTotals ||
rowNode.isTotalMeasure ||
colNode.isTotals ||
colNode.isTotalMeasure;

const cellData = this.spreadsheet.dataSet.getCellData({
query: {
...rowNode.query,
...colNode.query,
...measureQuery,
},
query,
rowNode,
isTotals:
rowNode.isTotals ||
rowNode.isTotalMeasure ||
colNode.isTotals ||
colNode.isTotalMeasure,
isTotals,
totalStatus: getHeaderTotalStatus(rowNode, colNode),
});

const formatNode = this.spreadsheet.isValueInCols() ? colNode : rowNode;
Expand Down

0 comments on commit a13eb27

Please sign in to comment.