Skip to content

Commit

Permalink
fix(export): export corner bug (#856)
Browse files Browse the repository at this point in the history
* fix: export corner bug

* fix: add test

* fix: test

* fix: pr problem

* fix: pr problem

Co-authored-by: wengyidong.wyd <[email protected]>
  • Loading branch information
YardWill and YardWill authored Dec 3, 2021
1 parent 114e7fc commit ccbc851
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/s2-core/__tests__/bugs/issue-565-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 5 additions & 0 deletions packages/s2-core/__tests__/unit/utils/export/export-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
43 changes: 30 additions & 13 deletions packages/s2-core/src/utils/export/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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('')
Expand Down

0 comments on commit ccbc851

Please sign in to comment.