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: 修复在局部复制和导出场景时, 格式化函数获取不到单元格信息 close #2866 #2871

Merged
merged 2 commits into from
Aug 23, 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
Expand Up @@ -31,6 +31,20 @@ exports[`PivotSheet Export Test Custom Tree Export Test should export correct da
自定义节点 a-2 - -"
`;

exports[`PivotSheet Export Test should called with cell view meta when export formatted data 1`] = `
" type 家具 家具 办公用品 办公用品
sub_type 桌子 沙发 笔 纸张
province city number number number number
浙江省 杭州市 - - - -
浙江省 绍兴市 - - - -
浙江省 宁波市 - - - -
浙江省 舟山市 - - - -
四川省 成都市 - - - -
四川省 绵阳市 - - - -
四川省 南充市 - - - -
四川省 乐山市 - - - -"
`;

exports[`PivotSheet Export Test should export correct data by {formatHeader: true} 1`] = `
" 类型 家具-type 家具-type 办公用品-type 办公用品-type
子类型 桌子-sub_type 沙发-sub_type 笔-sub_type 纸张-sub_type
Expand Down Expand Up @@ -234,6 +248,87 @@ province city 数值 数值 数值 数值
四川省 乐山市 2330 2445 2458 352"
`;

exports[`TableSheet Export Test should called with cell view meta when export formatted data 1`] = `
"province city type sub_type number
浙江省 杭州市 家具 桌子 -
浙江省 绍兴市 家具 桌子 -
浙江省 宁波市 家具 桌子 -
浙江省 舟山市 家具 桌子 -
浙江省 杭州市 家具 沙发 -
浙江省 绍兴市 家具 沙发 -
浙江省 宁波市 家具 沙发 -
浙江省 舟山市 家具 沙发 -
浙江省 杭州市 办公用品 笔 -
浙江省 绍兴市 办公用品 笔 -
浙江省 宁波市 办公用品 笔 -
浙江省 舟山市 办公用品 笔 -
浙江省 杭州市 办公用品 纸张 -
浙江省 绍兴市 办公用品 纸张 -
浙江省 宁波市 办公用品 纸张 -
浙江省 舟山市 办公用品 纸张 -
四川省 成都市 家具 桌子 -
四川省 绵阳市 家具 桌子 -
四川省 南充市 家具 桌子 -
四川省 乐山市 家具 桌子 -
四川省 成都市 家具 沙发 -
四川省 绵阳市 家具 沙发 -
四川省 南充市 家具 沙发 -
四川省 乐山市 家具 沙发 -
四川省 成都市 办公用品 笔 -
四川省 绵阳市 办公用品 笔 -
四川省 南充市 办公用品 笔 -
四川省 乐山市 办公用品 笔 -
四川省 成都市 办公用品 纸张 -
四川省 绵阳市 办公用品 纸张 -
四川省 南充市 办公用品 纸张 -
四川省 乐山市 办公用品 纸张 -
- - 家具 桌子 -
- - 家具 - -
- - 家具 沙发 -
- - 办公用品 - -
- - 办公用品 笔 -
- - 办公用品 纸张 -
浙江省 - 家具 桌子 -
浙江省 - 家具 沙发 -
浙江省 - 办公用品 笔 -
浙江省 - 办公用品 纸张 -
四川省 - 家具 桌子 -
四川省 - 家具 沙发 -
四川省 - 办公用品 笔 -
四川省 - 办公用品 纸张 -
浙江省 杭州市 家具 - -
浙江省 杭州市 办公用品 - -
浙江省 杭州市 - - -
浙江省 绍兴市 家具 - -
浙江省 绍兴市 办公用品 - -
浙江省 绍兴市 - - -
浙江省 宁波市 家具 - -
浙江省 宁波市 办公用品 - -
浙江省 宁波市 - - -
浙江省 舟山市 家具 - -
浙江省 舟山市 办公用品 - -
浙江省 舟山市 - - -
四川省 成都市 家具 - -
四川省 成都市 办公用品 - -
四川省 成都市 - - -
四川省 绵阳市 家具 - -
四川省 绵阳市 办公用品 - -
四川省 绵阳市 - - -
四川省 南充市 家具 - -
四川省 南充市 办公用品 - -
四川省 南充市 - - -
四川省 乐山市 家具 - -
四川省 乐山市 办公用品 - -
四川省 乐山市 - - -
浙江省 - 家具 - -
浙江省 - 办公用品 - -
浙江省 - - - -
四川省 - 家具 - -
四川省 - 办公用品 - -
四川省 - - - -
- - - - -"
`;

exports[`TableSheet Export Test should export correct data with no series number 1`] = `
"province city type sub_type number
浙江省 杭州市 家具 桌子 7789
Expand Down
49 changes: 49 additions & 0 deletions packages/s2-core/__tests__/unit/utils/export/copy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,33 @@ describe('List Table Core Data Process', () => {

expect(getCopyPlainContent(s2)).toMatchInlineSnapshot(`"custom data"`);
});

it('should called with cell view meta when brush select formatted data', async () => {
const formatter = jest.fn();

s2.setOptions({
interaction: {
copy: {
withFormat: true,
},
},
});
s2.setDataCfg({
meta: [{ field: 'number', formatter }],
});

await s2.render();

s2.interaction.changeState({
cells: s2.facet.getDataCells().map((cell) => getCellMeta(cell)),
stateName: InteractionStateName.SELECTED,
});

const viewMeta = s2.facet.getCellMeta(18, 5);

getCopyPlainContent(s2);
expect(formatter).toHaveBeenLastCalledWith(1943, viewMeta.data, viewMeta);
});
});

describe('Pivot Table Core Data Process', () => {
Expand Down Expand Up @@ -1226,6 +1253,28 @@ describe('Pivot Table Core Data Process', () => {

expect(getCopyPlainContent(s2)).toMatchInlineSnapshot(`"custom data"`);
});

// https://github.com/antvis/S2/issues/2866
it('should called with cell view meta when brush select formatted data', async () => {
const formatter = jest.fn();

s2.setOptions({
interaction: {
copy: { enable: true, withHeader: true, withFormat: true },
},
});

const meta: Meta[] = [{ field: 'number', formatter }];

s2.setDataCfg(getDataCfg(meta));

await s2.render();

const viewMeta = s2.facet.getCellMeta(10, 4);

getCopyPlainContent(s2);
expect(formatter).toHaveBeenLastCalledWith(16838, viewMeta.data, viewMeta);
});
});

describe('Tree Table Core Data Process', () => {
Expand Down
45 changes: 45 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 @@ -54,6 +54,27 @@ describe('TableSheet Export Test', () => {

await expectMatchSnapshot(s2);
});

it('should called with cell view meta when export formatted data', async () => {
const formatter = jest.fn();

const s2 = new TableSheet(
getContainer(),
assembleDataCfg({
meta: [{ field: 'number', formatter }],
fields: {
columns: ['province', 'city', 'type', 'sub_type', 'number'],
},
}),
assembleOptions(),
);

await expectMatchSnapshot(s2);

const viewMeta = s2.facet.getCellMeta(76, 4);

expect(formatter).toHaveBeenLastCalledWith(78868, viewMeta.data, viewMeta);
});
});

describe('PivotSheet Export Test', () => {
Expand Down Expand Up @@ -390,6 +411,30 @@ describe('PivotSheet Export Test', () => {
});
});

// https://github.com/antvis/S2/issues/2866
it('should called with cell view meta when export formatted data', async () => {
const formatter = jest.fn();

const s2 = new PivotSheet(
getContainer(),
assembleDataCfg({
meta: [
{
field: 'number',
formatter,
},
],
}),
s2Options,
);

await expectMatchSnapshot(s2);

const viewMeta = s2.facet.getCellMeta(7, 3);

expect(formatter).toHaveBeenLastCalledWith(352, viewMeta.data, viewMeta);
});

describe('Custom Tree Export Test', () => {
let s2: SpreadSheet;

Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type { GetCellMeta, LayoutResult } from './facet';
export type Formatter = (
value: DataItem,
data?: DataItem | ViewMetaData | ViewMetaData[],
meta?: Node | ViewMeta,
meta?: Node | ViewMeta | null | undefined,
) => SimpleData;

export interface FormatResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class PivotFacet extends FrozenFacet {
/**
* 根据行列索引获取单元格元数据
*/
public getCellMeta(rowIndex = 0, colIndex = 0) {
public getCellMeta(rowIndex: number = 0, colIndex: number = 0) {
const { options, dataSet } = this.spreadsheet;
const { rowLeafNodes, colLeafNodes } = this.getLayoutResult();
const row = rowLeafNodes[rowIndex];
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/table-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class TableFacet extends FrozenFacet {
};
}

public getCellMeta(rowIndex = 0, colIndex = 0) {
public getCellMeta(rowIndex: number = 0, colIndex: number = 0) {
const { options, dataSet } = this.spreadsheet;
const colLeafNodes = this.getColLeafNodes();
const colNode = colLeafNodes[colIndex];
Expand Down
21 changes: 21 additions & 0 deletions packages/s2-core/src/utils/export/copy/base-data-cell-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AsyncRenderThreshold,
TAB_SEPARATOR,
type DataItem,
type Formatter,
type SimpleData,
} from '../../../common';
import type {
Expand Down Expand Up @@ -70,4 +71,24 @@ export abstract class BaseDataCellCopy {
this.matrixHtmlTransformer(dataMatrix),
];
}

protected getFormatter(options: {
field: string;
rowIndex: number;
colIndex: number;
}): Formatter {
const { field, rowIndex, colIndex } = options;

if (this.config.formatData) {
const viewMeta = this.spreadsheet.facet.getCellMeta(rowIndex, colIndex);

return (value) => {
const formatter = this.spreadsheet.dataSet.getFieldFormatter(field!);

return formatter?.(value, viewMeta?.data, viewMeta);
};
}

return ((value) => value) as Formatter;
}
}
15 changes: 1 addition & 14 deletions packages/s2-core/src/utils/export/copy/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { escape, map, max } from 'lodash';
import type { DataItem, Formatter, SimpleData } from '../../../common';
import type { DataItem, SimpleData } from '../../../common';
import { LINE_SEPARATOR, ROOT_NODE_ID, TAB_SEPARATOR } from '../../../common';
import {
CopyMIMEType,
Expand All @@ -13,7 +13,6 @@ import {
type Transformer,
} from '../../../common/interface/export';
import type { Node } from '../../../facet/layout/node';
import type { BaseDataSet } from './../../../data-set/base-data-set';

// 把 string[][] 矩阵转换成 CopyablePlain
export const matrixPlainTextTransformer = (
Expand Down Expand Up @@ -60,18 +59,6 @@ export const Transformers: {
[CopyMIMEType.HTML]: matrixHtmlTransformer,
};

export function getFormatter(
field: string,
formatData = false,
dataSet: BaseDataSet,
): Formatter {
if (formatData) {
return dataSet.getFieldFormatter(field!);
}

return ((value) => value) as Formatter;
}

// 生成矩阵:https://gw.alipayobjects.com/zos/antfincdn/bxBVt0nXx/a182c1d4-81bf-469f-b868-8b2e29acfc5f.png
export const assembleMatrix = ({
rowMatrix,
Expand Down
Loading
Loading