Skip to content

Commit

Permalink
feat: 当前只能复制数值可带表头复制 (#1590)
Browse files Browse the repository at this point in the history
* feat: 添加通过 cellMetas 获取单元格对应的列头文本的方法

* feat: 添加通过 cellMetas 获取单元格对应的行头文本的方法

* feat: 只复制数据单元格时可以携带行列头

* feat: 选择某X行/列数据时可以携带行列头

* feat: 提取矩阵转换为字符串的方法

* test: 添加带行列头复制的单测

* test: 明细表添加带行列头复制的单测

* docs: 复制数据是否带表头信息

* feat(interaction): 行列宽高支持控制拖拽范围 (#1583)

* feat(interaction): 行列宽高支持控制拖拽范围

* feat(interaction): 增加测试和文档

* feat(interaction): 增加主题色

* Update row-column-resize-spec.ts

* Update packages/s2-core/src/interaction/row-column-resize.ts

Co-authored-by: Wenjun Xu <[email protected]>

* fix: rename

Co-authored-by: Wenjun Xu <[email protected]>

* docs: 复制数据是否带表头信息

* refactor: 评审细节修改

Co-authored-by: zishang <[email protected]>
Co-authored-by: Jinke Li <[email protected]>
Co-authored-by: Wenjun Xu <[email protected]>
  • Loading branch information
4 people authored Jul 22, 2022
1 parent d4f7dda commit 94eaa90
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 49 deletions.
155 changes: 153 additions & 2 deletions packages/s2-core/__tests__/unit/utils/export/copy-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assembleDataCfg, assembleOptions, TOTALS_OPTIONS } from 'tests/util';
import { getContainer } from 'tests/util/helpers';
import { data as originalData, totalData } from 'tests/data/mock-dataset.json';
import { map } from 'lodash';
import { TableSheet, PivotSheet } from '@/sheet-type';

import {
Expand Down Expand Up @@ -102,6 +103,25 @@ describe('List Table Core Data Process', () => {
expect(getSelectedData(s2).split('\n')[2].split('\t').length).toBe(5);
});

it('should copy normal data with header in table mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: true,
},
});
s2.render();

const cell = s2.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.DATA_CELL)[0];

s2.interaction.changeState({
cells: [getCellMeta(cell)],
stateName: InteractionStateName.SELECTED,
});
expect(getSelectedData(s2)).toEqual('province\r\n浙江省');
});

it('should copy format data', () => {
const ss = new TableSheet(
getContainer(),
Expand Down Expand Up @@ -131,6 +151,13 @@ describe('List Table Core Data Process', () => {
});

it('should copy correct data with data filtered', () => {
s2.setOptions({
interaction: {
copyWithHeader: false,
},
});
s2.render();

s2.emit(S2Event.RANGE_FILTER, {
filterKey: 'province',
filteredValues: ['浙江省'],
Expand Down Expand Up @@ -221,6 +248,10 @@ describe('Pivot Table Core Data Process', () => {
const ROW_COUNT = 7;
// 11 = 8(维度节点) + 2(小计) + 1(总计)
const COL_COUNT = 11;
// 3 = ['type', 'sub_type', 'number'].length 行头高度
const COL_HEADER_HEIGHT = 3;
// 2 = ['province', 'city'].length 列头宽度
const ROW_HEADER_WIDTH = 2;

const s2 = new PivotSheet(
getContainer(),
Expand Down Expand Up @@ -363,11 +394,133 @@ describe('Pivot Table Core Data Process', () => {
expect(getSelectedData(ss)).toEqual(`${originalData[0].number}元`);
});

it('should copy normal data with header in grid mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: true,
},
});
s2.render();

const allDataCells = s2.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.DATA_CELL);

const hangzhouDeskCell = allDataCells[0];
const zhejiangCityDeskSubTotalCell = allDataCells[4];

// 普通数据节点
s2.interaction.changeState({
cells: [getCellMeta(hangzhouDeskCell)],
stateName: InteractionStateName.SELECTED,
});

expect(getSelectedData(s2)).toEqual(
`\t\t家具\r\n\t\t桌子\r\n\t\tnumber\r\n浙江省\t杭州市\t7789`,
);

// 小计节点
s2.interaction.changeState({
cells: [getCellMeta(zhejiangCityDeskSubTotalCell)],
stateName: InteractionStateName.SELECTED,
});
expect(getSelectedData(s2)).toEqual(
`\t\t家具\r\n\t\t桌子\r\n\t\tnumber\r\n浙江省\t小计\t18375`,
);
});

// 看图更清晰 https://gw.alipayobjects.com/zos/antfincdn/zK68PhcnX/d852ffb8-603a-43e5-b841-dbf3c7577638.png
it('should copy col data with header in grid mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: true,
},
});
s2.render();

const cell = s2.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.COL_CELL)[0];

s2.interaction.changeState({
cells: [getCellMeta(cell)],
stateName: InteractionStateName.SELECTED,
});

// 复制的数据高度 = 列头高度 + 数据高度
expect(getSelectedData(s2).split('\n')).toHaveLength(
COL_COUNT + COL_HEADER_HEIGHT,
);
// 复制的数据宽度 = 行头宽度 + 数据宽度
expect(getSelectedData(s2).split('\n')[0].split('\t')).toHaveLength(5);
});

// https://gw.alipayobjects.com/zos/antfincdn/q3mBlV9Ii/1d68499a-b529-4594-93ce-8b04f8b4c4bc.png
it('should copy row data with header in grid mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: true,
},
});
s2.render();

const allRowCells = s2.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.ROW_CELL);

const hangzhouDeskCell = allRowCells[1];
const zhejiangCityDeskSubTotalCell = allRowCells[0];

// 选择某一行, city 维度下
s2.interaction.changeState({
cells: [getCellMeta(hangzhouDeskCell)],
stateName: InteractionStateName.SELECTED,
});

expect(getSelectedData(s2).split('\n')).toHaveLength(4);
expect(getSelectedData(s2).split('\n')[0].split('\t')).toHaveLength(9);

// 选择某几行,province 维度
s2.interaction.changeState({
cells: [getCellMeta(zhejiangCityDeskSubTotalCell)],
stateName: InteractionStateName.SELECTED,
});

expect(getSelectedData(s2).split('\n')).toHaveLength(8);
expect(getSelectedData(s2).split('\n')[0].split('\t')).toHaveLength(9);
});

it('should copy all data with header in grid mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: true,
},
});
s2.render();

s2.interaction.changeState({
stateName: InteractionStateName.ALL_SELECTED,
});

expect(getSelectedData(s2).split('\n').length).toBe(
COL_COUNT + COL_HEADER_HEIGHT,
);
expect(getSelectedData(s2).split('\n')[1].split('\t').length).toBe(
ROW_COUNT + ROW_HEADER_WIDTH,
);
});

it('should copy correct data with data sorted in grid mode', () => {
s2.setOptions({
interaction: {
copyWithHeader: false,
},
});
const node = s2.getColumnNodes().find((node) => node.isLeaf);
s2.groupSortByMethod('ASC' as SortMethodType, node);
s2.setDataCfg(s2.dataCfg);
s2.render();

const cell = s2.interaction
.getAllCells()
.filter(({ cellType }) => cellType === CellTypes.ROW_CELL)
Expand All @@ -386,8 +539,6 @@ describe('Pivot Table Core Data Process', () => {
});

it('should copy correct data with \n data in grid mode', () => {
const newLineText = `1
2`;
const sss = new PivotSheet(
getContainer(),
assembleDataCfg({
Expand Down
2 changes: 2 additions & 0 deletions packages/s2-core/src/common/interface/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export interface InteractionOptions {
enableCopy?: boolean;
// copy with filed format
copyWithFormat?: boolean;
// copy with header info
copyWithHeader?: boolean;
// auto reset sheet style when click outside or press ecs key, default true
autoResetSheetStyle?: boolean;
hiddenColumnFields?: string[];
Expand Down
Loading

0 comments on commit 94eaa90

Please sign in to comment.