diff --git a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap index e5aab4ae9b..f19802e2cf 100644 --- a/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap +++ b/packages/s2-core/__tests__/unit/utils/export/__snapshots__/export-pivot-spec.ts.snap @@ -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`] = ` " 类别 家具 家具 家具 办公用品 办公用品 办公用品 总计 子类别 桌子 沙发 小计 笔 纸张 小计 diff --git a/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts b/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts index ab3f56c7d5..8361ae3c95 100644 --- a/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/export/export-pivot-spec.ts @@ -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, @@ -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(); @@ -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); + }); }); diff --git a/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts b/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts index 6ee4fe1db7..de0759c258 100644 --- a/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts +++ b/packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts @@ -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, @@ -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;