diff --git a/packages/s2-core/__tests__/bugs/issue-565-spec.ts b/packages/s2-core/__tests__/bugs/issue-565-spec.ts index daceb0d351..a69ea23ad8 100644 --- a/packages/s2-core/__tests__/bugs/issue-565-spec.ts +++ b/packages/s2-core/__tests__/bugs/issue-565-spec.ts @@ -28,6 +28,6 @@ describe('Export data in pivot tree mode', () => { expect(rows[1].split('\t')[0]).toEqual(''); expect(rows[7].split('\t')[0]).toEqual('"row0"'); expect(rows[8].split('\t')[0]).toEqual('"row0"'); - expect(data.length).toEqual(245); + expect(data.length).toEqual(263); }); }); diff --git a/packages/s2-core/__tests__/unit/utils/export/export-spec.ts b/packages/s2-core/__tests__/unit/utils/export/export-spec.ts index 94217e9da9..8c34137045 100644 --- a/packages/s2-core/__tests__/unit/utils/export/export-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/export/export-spec.ts @@ -86,6 +86,9 @@ describe('PivotSheet Export Test', () => { const data = copyData(s2, '\t'); const rows = data.split('\n'); expect(rows).toHaveLength(14); + expect(rows[0].split('\t')[1]).toEqual('"province"'); + expect(rows[1].split('\t')[1]).toEqual('"city"'); + rows.forEach((e) => { expect(e.split('\t')).toHaveLength(34); }); @@ -108,6 +111,8 @@ describe('PivotSheet Export Test', () => { const data = copyData(s2, '\t'); const rows = data.split('\n'); expect(rows).toHaveLength(16); + expect(rows[0].split('\t')[1]).toEqual('"province"'); + expect(rows[1].split('\t')[1]).toEqual('"city"'); rows.forEach((e) => { expect(e.split('\t')).toHaveLength(34); }); diff --git a/packages/s2-core/src/utils/export/index.ts b/packages/s2-core/src/utils/export/index.ts index 9d0a28db9b..64907f0c06 100644 --- a/packages/s2-core/src/utils/export/index.ts +++ b/packages/s2-core/src/utils/export/index.ts @@ -1,17 +1,7 @@ -import { - head, - last, - isEmpty, - get, - clone, - trim, - max, - isObject, - forEach, -} from 'lodash'; +import { last, isEmpty, clone, trim, max, isObject, forEach } from 'lodash'; import { getCsvString } from './export-worker'; import { SpreadSheet } from '@/sheet-type'; -import { ViewMeta } from '@/common/interface'; +import { CornerNodeType, ViewMeta } from '@/common/interface'; import { ID_SEPARATOR, EMPTY_PLACEHOLDER, @@ -129,7 +119,7 @@ const processValueInRow = ( const tempCell = []; if (viewMeta) { - const { data, fieldValue, valueField } = viewMeta; + const { fieldValue, valueField } = viewMeta; // The main measure. if (!isFormat) { tempCell.push(fieldValue); @@ -209,6 +199,33 @@ export const copyData = ( // Generate the table header. headers = colHeader.map((item, index) => { + if (sheetInstance.isPivotMode()) { + const { columns, rows, data } = sheetInstance.facet.cornerHeader.cfg; + const colNodes = data.filter( + ({ cornerType }) => cornerType === CornerNodeType.Col, + ); + const rowNodes = data.filter( + ({ cornerType }) => cornerType === CornerNodeType.Row, + ); + + if (index < colHeader.length - 1) { + return [ + ...Array(rowLength - 1).fill(''), + colNodes.find(({ field }) => field === columns[index])?.label || '', + ...item, + ]; + } + if (index < colHeader.length) { + return [ + ...rows.map( + (row) => rowNodes.find(({ field }) => field === row)?.label || '', + ), + ...item, + ]; + } + return rowsHeader.concat(...item); + } + return index < colHeader.length ? Array(rowLength) .fill('')