diff --git a/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts b/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts index f6ec28df..977ad823 100644 --- a/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts +++ b/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts @@ -2,9 +2,17 @@ import {FocusedCellUtils} from '../../focusedElements/focusedCellUtils'; import {InsertMatrix} from '../../programmaticUpdates/insertMatrix'; import {ParseCSVClipboardText} from './parseCSVClipboardText'; import {FocusedCell} from '../../../types/focusedCell'; +import {TableData} from '../../../types/tableData'; import {ActiveTable} from '../../../activeTable'; export class OverwriteCellsViaCSVOnPaste { + private static trimCSVRowsIfCantCreateNew(CSV: TableData, data: TableData, rowIndex: number) { + if (data.length < CSV.length + rowIndex) { + return CSV.slice(0, CSV.length - (CSV.length + rowIndex - data.length)); + } + return CSV; + } + private static focusOriginalCellAfterProcess(at: ActiveTable, process: () => void) { const {element, rowIndex, columnIndex} = at._focusedElements.cell as Required; process(); @@ -15,7 +23,8 @@ export class OverwriteCellsViaCSVOnPaste { public static overwrite(at: ActiveTable, clipboardText: string, event: ClipboardEvent, rowIndex: number, columnIndex: number) { event.preventDefault(); - const CSV = ParseCSVClipboardText.parse(clipboardText); + let CSV = ParseCSVClipboardText.parse(clipboardText); + if (!at.displayAddNewRow) CSV = OverwriteCellsViaCSVOnPaste.trimCSVRowsIfCantCreateNew(CSV, at.data, rowIndex); OverwriteCellsViaCSVOnPaste.focusOriginalCellAfterProcess(at, InsertMatrix.insert.bind(this, at, CSV, rowIndex, columnIndex)); }