From 077e82305806894df4d5dcd908f5e7dc913cd6fc Mon Sep 17 00:00:00 2001 From: Jinke Li Date: Fri, 26 Jul 2024 10:16:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=E7=BB=B4?= =?UTF-8?q?=E5=80=BC=E7=BC=BA=E5=A4=B1=E5=9C=BA=E6=99=AF=E4=B8=8B=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=95=B0=E6=8D=AE=E5=8D=A0=E4=BD=8D=E7=AC=A6=E6=9C=AA?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E7=9A=84=E9=97=AE=E9=A2=98=20(#2829)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复在维值缺失场景下导出数据占位符未解析的问题 * test: 更新快照 --- .../miss-dimension-values-spec.ts.snap | 39 +++++++++++++++++++ .../spreadsheet/miss-dimension-values-spec.ts | 13 +++++++ .../s2-core/src/data-set/base-data-set.ts | 6 ++- packages/s2-core/src/facet/utils.ts | 12 +++--- packages/s2-core/src/utils/export/copy.ts | 2 +- .../s2-core/src/utils/export/export-worker.ts | 10 +++-- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 packages/s2-core/__tests__/spreadsheet/__snapshots__/miss-dimension-values-spec.ts.snap diff --git a/packages/s2-core/__tests__/spreadsheet/__snapshots__/miss-dimension-values-spec.ts.snap b/packages/s2-core/__tests__/spreadsheet/__snapshots__/miss-dimension-values-spec.ts.snap new file mode 100644 index 0000000000..f20fa5d3e0 --- /dev/null +++ b/packages/s2-core/__tests__/spreadsheet/__snapshots__/miss-dimension-values-spec.ts.snap @@ -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\\"" +`; diff --git a/packages/s2-core/__tests__/spreadsheet/miss-dimension-values-spec.ts b/packages/s2-core/__tests__/spreadsheet/miss-dimension-values-spec.ts index 439d0bad71..4ab47a5046 100644 --- a/packages/s2-core/__tests__/spreadsheet/miss-dimension-values-spec.ts +++ b/packages/s2-core/__tests__/spreadsheet/miss-dimension-values-spec.ts @@ -6,6 +6,7 @@ import { type S2Options, } from '@/common'; import { PivotSheet, SpreadSheet } from '@/sheet-type'; +import { copyData } from '@/'; const s2Options: S2Options = { debug: true, @@ -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(); + }); }); diff --git a/packages/s2-core/src/data-set/base-data-set.ts b/packages/s2-core/src/data-set/base-data-set.ts index 335cc770a8..c4abc36118 100644 --- a/packages/s2-core/src/data-set/base-data-set.ts +++ b/packages/s2-core/src/data-set/base-data-set.ts @@ -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, @@ -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 { @@ -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); } /** diff --git a/packages/s2-core/src/facet/utils.ts b/packages/s2-core/src/facet/utils.ts index 4e5c33e430..0c5194f472 100644 --- a/packages/s2-core/src/facet/utils.ts +++ b/packages/s2-core/src/facet/utils.ts @@ -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; @@ -575,3 +574,6 @@ export const getFrozenRowCfgPivot = ( frozenRowHeight: effectiveFrozenFirstRow ? headNode.height : 0, }; }; + +export const replaceEmptyFieldValue = (value: string) => + value === EMPTY_FIELD_VALUE ? EMPTY_PLACEHOLDER : value; diff --git a/packages/s2-core/src/utils/export/copy.ts b/packages/s2-core/src/utils/export/copy.ts index 9a56e3f301..59eb0dc0e6 100644 --- a/packages/s2-core/src/utils/export/copy.ts +++ b/packages/s2-core/src/utils/export/copy.ts @@ -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, diff --git a/packages/s2-core/src/utils/export/export-worker.ts b/packages/s2-core/src/utils/export/export-worker.ts index b52fdcdb5c..92a02d5ea0 100644 --- a/packages/s2-core/src/utils/export/export-worker.ts +++ b/packages/s2-core/src/utils/export/export-worker.ts @@ -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}"`; }