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

[WIP]feat: 支持从excel粘贴值进编辑表 #2148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -3,13 +3,15 @@ import { BaseSheet } from '../base-sheet';
import type { SheetComponentsProps } from '../interface';
import { EditCell } from './edit-cell';
import { DragCopyPoint } from './drag-copy';
import { PastePlugin } from './paste';

export const EditableSheet: React.FC<SheetComponentsProps> = React.memo(
(props) => {
return (
<BaseSheet {...props} sheetType={'table'}>
<EditCell onChange={() => {}} />
<DragCopyPoint />
<PastePlugin />
</BaseSheet>
);
},
Expand Down
81 changes: 81 additions & 0 deletions packages/s2-react/src/components/sheets/editable-sheet/paste.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useEffect } from 'react';
import { CellTypes, SpreadSheet } from '@antv/s2';
import { useSpreadSheetRef } from '../../../utils/SpreadSheetContext';

const getDisplayRowMappedIndex = (
rowIndex: number,
spreadsheet: SpreadSheet,
) => {
const data = spreadsheet.dataSet.getDisplayDataSet()[rowIndex];
return spreadsheet.dataSet.originData.findIndex((val) => val === data);
};

/**
* 粘贴值功能
* @returns
*/
export const PastePlugin = () => {
const spreadsheet = useSpreadSheetRef();

useEffect(() => {
const handlePaste = async () => {
try {
// 批量黏贴数据
const meta = spreadsheet.interaction
.getInteractedCells()
.filter((cell) => cell.cellType === CellTypes.DATA_CELL)[0]
.getMeta();

const activeDom = document.activeElement;
if (meta && activeDom?.tagName === 'BODY') {
const { rowIndex, colIndex } = meta;
const source = spreadsheet.dataSet.originData;
const clipboardItems = await navigator.clipboard.read();
const result = await clipboardItems[0]?.getType('text/html');
const tableNode = await result.text();
const $doc = new DOMParser().parseFromString(tableNode, 'text/html');
const $trs = Array.from($doc.querySelectorAll('table tr'));
const lines = $trs.map((tr) =>
Array.from(tr.children).map((td) => td.textContent || ''),
);
const columns = spreadsheet.dataCfg.fields.columns;

lines.forEach((line, idx) => {
const originIndex = getDisplayRowMappedIndex(
rowIndex + idx,
spreadsheet,
);
// 当列不存在时,代表需要新增列
if (originIndex === -1) {
const addedRow = {};
columns.slice(colIndex, line.length).forEach((col, indx) => {
addedRow[col as string] = line[indx];
});
spreadsheet.dataSet.originData.push(addedRow);
} else {
line.forEach((val, colIdx) => {
const sanitizedVal = (val || '').replace(/^"|"$/g, '');
const originalColIdx = colIndex + colIdx;

if (columns[originalColIdx]) {
source[originIndex][columns[originalColIdx] as string] =
sanitizedVal;
}
});
}
});

spreadsheet.render();
}
} catch (error) {
// console.info(error);
}
};
window.addEventListener('paste', handlePaste);
return () => {
window.removeEventListener('paste', handlePaste);
};
}, [spreadsheet]);

return null;
};