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 #2928 #2937

Merged
merged 2 commits into from
Oct 25, 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 @@ -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
42 changes: 40 additions & 2 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,13 +50,14 @@ describe('PivotSheet Export Test', () => {
);

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

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

const asyncData = await asyncGetAllPlainData({
sheetInstance: s2,
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),
wjgogogo marked this conversation as resolved.
Show resolved Hide resolved
});

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