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: 修复在维值缺失场景下导出数据占位符未解析的问题 #2829

Merged
merged 2 commits into from
Jul 26, 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
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Miss Dimension Values Tests should replace empty dimension value placeholder 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 replace empty dimension value placeholder 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 replace empty dimension value placeholder when copy original data', () => {
const data = copyData(s2, '\t', false);

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

test('should replace empty dimension value placeholder 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;
2 changes: 1 addition & 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 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}"`;
}
Loading