Skip to content

Commit

Permalink
fix: 修复在维值缺失场景下导出数据占位符未解析的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jul 24, 2024
1 parent c76a510 commit f91726d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Miss Dimension Values Tests should get correctly empty dimension values when copy formatted data 1`] = `
"\\"一级维度\\" \\"二级维度\\" \\"三级维度\\" \\"数值\\"
\\"总计\\" \\"-\\" \\"-\\" \\"1732771\\"
\\"维值-1\\" \\"维值-2\\" \\"维度-3\\" \\"12222\\"
\\"维值-1\\" \\"维值-3\\" \\"维值-3\\" \\"11111\\"
\\"维值-1\\" \\"维值-3\\" \\"维度-3\\" \\"11111\\"
\\"维值-1\\" \\"维值-3\\" \\"小计\\"
\\"维值-1\\" \\"小计\\" \\"456\\"
\\"测试-1\\" \\"测试-2\\" \\"维度-3\\" \\"4444567\\"
\\"测试-1\\" \\"测试-3\\" \\"-\\" \\"785222\\"
\\"测试-1\\" \\"测试-4\\" \\"维度-3\\" \\"6455644\\"
\\"测试-1\\" \\"测试-5\\" \\"维度-3\\" \\"1111\\"
\\"测试-1\\" \\"小计\\" \\"125555\\"
\\"测试-6\\" \\"测试-x\\" \\"-\\" \\"111111\\"
\\"测试-6\\" \\"测试-7\\" \\"-\\" \\"67878\\"
\\"测试-6\\" \\"测试-8\\" \\"-\\" \\"456.464\\"
\\"测试-6\\" \\"小计\\" \\"123.416\\""
`;

exports[`Miss Dimension Values Tests should get correctly empty dimension values when copy original data 1`] = `
"\\"一级维度\\" \\"二级维度\\" \\"三级维度\\" \\"数值\\"
\\"总计\\" \\"-\\" \\"-\\" \\"1732771\\"
\\"维值-1\\" \\"维值-2\\" \\"维度-3\\" \\"12222\\"
\\"维值-1\\" \\"维值-3\\" \\"维值-3\\" \\"11111\\"
\\"维值-1\\" \\"维值-3\\" \\"维度-3\\" \\"11111\\"
\\"维值-1\\" \\"维值-3\\" \\"小计\\" \\"null\\"
\\"维值-1\\" \\"小计\\" \\"456\\"
\\"测试-1\\" \\"测试-2\\" \\"维度-3\\" \\"4444567\\"
\\"测试-1\\" \\"测试-3\\" \\"-\\" \\"785222\\"
\\"测试-1\\" \\"测试-4\\" \\"维度-3\\" \\"6455644\\"
\\"测试-1\\" \\"测试-5\\" \\"维度-3\\" \\"1111\\"
\\"测试-1\\" \\"小计\\" \\"125555\\"
\\"测试-6\\" \\"测试-x\\" \\"-\\" \\"111111\\"
\\"测试-6\\" \\"测试-7\\" \\"-\\" \\"67878\\"
\\"测试-6\\" \\"测试-8\\" \\"-\\" \\"456.464\\"
\\"测试-6\\" \\"小计\\" \\"123.416\\""
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type S2Options,
} from '@/common';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
import { copyData } from '@/';

const s2Options: S2Options = {
debug: true,
Expand Down Expand Up @@ -259,4 +260,16 @@ describe('Miss Dimension Values Tests', () => {
]
`);
});

test('should get correctly empty dimension values when copy original data', () => {
const data = copyData(s2, '\t', false);

expect(data).toMatchSnapshot();
});

test('should get correctly empty dimension values when copy formatted data', () => {
const data = copyData(s2, '\t', true);

expect(data).toMatchSnapshot();
});
});
6 changes: 4 additions & 2 deletions packages/s2-core/src/data-set/base-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
memoize,
min,
} from 'lodash';
import type { Indexes } from '../utils/indexes';
import type { CellMeta, Data, RowData, ViewMeta } from '../common';
import type {
Fields,
Expand All @@ -21,12 +20,14 @@ import type {
SortParams,
} from '../common/interface';
import type { ValueRange } from '../common/interface/condition';
import { replaceEmptyFieldValue } from '../facet/utils';
import type { SpreadSheet } from '../sheet-type';
import {
getValueRangeState,
setValueRangeState,
} from '../utils/condition/state-controller';
import { generateExtraFieldMeta } from '../utils/dataset/pivot-data-set';
import type { Indexes } from '../utils/indexes';
import type { CellDataParams, DataType, MultiDataParams, Query } from './index';

export abstract class BaseDataSet {
Expand Down Expand Up @@ -74,7 +75,8 @@ export abstract class BaseDataSet {
* @param field
*/
public getFieldName(field: string): string {
return get(this.getFieldMeta(field, this.meta), 'name', field);
const name = get(this.getFieldMeta(field, this.meta), 'name', field);
return replaceEmptyFieldValue(name);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions packages/s2-core/src/facet/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import type { IGroup, SimpleBBox } from '@antv/g-canvas';
import { findIndex, isEmpty, isNil } from 'lodash';

import { EMPTY_FIELD_VALUE, EMPTY_PLACEHOLDER } from '../common';
import type { FrozenCellIndex, FrozenOpts } from '../common/constant/frozen';
import { FrozenCellType } from '../common/constant/frozen';
import { DEFAULT_PAGE_INDEX } from '../common/constant/pagination';
import type {
ColumnNode,
Columns,
Fields,
Pagination,
S2Options,
S2PivotSheetOptions,
S2TableSheetOptions,
ScrollSpeedRatio,
SpreadSheetFacetCfg,
} from '../common/interface';
import type { Fields } from '../common/interface';
import type { Indexes } from '../utils/indexes';
import { DEFAULT_PAGE_INDEX } from '../common/constant/pagination';
import type { Node } from './layout/node';
import type { ViewCellHeights } from './layout/interface';
import type { Node } from './layout/node';

export const isFrozenCol = (colIndex: number, frozenCount: number) => {
return frozenCount > 0 && colIndex < frozenCount;
Expand Down Expand Up @@ -575,3 +574,6 @@ export const getFrozenRowCfgPivot = (
frozenRowHeight: effectiveFrozenFirstRow ? headNode.height : 0,
};
};

export const replaceEmptyFieldValue = (value: string) =>
value === EMPTY_FIELD_VALUE ? EMPTY_PLACEHOLDER : value;
4 changes: 3 additions & 1 deletion packages/s2-core/src/utils/export/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import type { ColCell, HeaderCell, RowCell } from '../../cell';
import {
CellTypes,
DATA_CELL_ID_SEPARATOR,
CopyType,
DATA_CELL_ID_SEPARATOR,
EXTRA_FIELD,
ID_SEPARATOR,
InteractionStateName,
Expand All @@ -28,6 +28,7 @@ import {
} from '../../common';
import type { DataType } from '../../data-set/interface';
import type { Node } from '../../facet/layout/node';
import { replaceEmptyFieldValue } from '../../facet/utils';
import type { SpreadSheet } from '../../sheet-type';
import { copyToClipboard } from '../../utils/export';
import { customFlattenDeep } from '../data-set-operate';
Expand Down Expand Up @@ -151,6 +152,7 @@ export const convertString = (v: string) => {
*/
const getHeaderMeasureFields = (headerId: string, startLevel?: number) => {
const headerList = headerId.split(ID_SEPARATOR);
// .map((value) => (value === EMPTY_FIELD_VALUE ? '-' : value));
if (startLevel) {
return headerList.slice(headerList.length - startLevel);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/s2-core/src/utils/export/export-worker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { replaceEmptyFieldValue } from '../../facet/utils';

export function getCsvString(v: any): string {
if (!v) {
return v;
}

if (typeof v === 'string') {
const out = v;
const value = replaceEmptyFieldValue(v);

if (typeof value === 'string') {
const out = value;
// 需要替换", https://en.wikipedia.org/wiki/Comma-separated_values#Example
return `"${out.replace(/"/g, '""')}"`;
}

return `"${v}"`;
return `"${value}"`;
}

0 comments on commit f91726d

Please sign in to comment.