From 12015efeaa7e6ea2561ab2e0227bb90438e984c8 Mon Sep 17 00:00:00 2001 From: Mike Kruk Date: Thu, 2 Mar 2023 11:12:25 -0500 Subject: [PATCH] fix issues when pasting multiple cells with quoted text and new lines --- src/utils/copyPasting.test.ts | 17 +++++++++++++++++ src/utils/copyPasting.ts | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/utils/copyPasting.test.ts b/src/utils/copyPasting.test.ts index 7f35922..c52f95c 100644 --- a/src/utils/copyPasting.test.ts +++ b/src/utils/copyPasting.test.ts @@ -138,6 +138,23 @@ describe('parsePlainTextData', () => { expect(parseTextPlainData('"foo\nbar""baz"""')).toEqual([['foo\nbar"baz"']]) }) + test('single cell multi line clean', () => { + expect(parseTextPlainData('"foo\nbar"')).toEqual([['foo\nbar']]) + }) + + test('multi cell multi line', () => { + expect(parseTextPlainData('"foo\nbar"\n"baz\nqux"')).toEqual([ + ['foo\nbar'], + ['baz\nqux'], + ]) + }) + + test('multi cell single line', () => { + expect(parseTextPlainData('"foo\nbar"\t"baz\nqux"')).toEqual([ + ['foo\nbar', 'baz\nqux'], + ]) + }) + test('quoted first cell', () => { expect(parseTextPlainData('"foo\nbar')).toEqual([['"foo'], ['bar']]) }) diff --git a/src/utils/copyPasting.ts b/src/utils/copyPasting.ts index 5fbc540..65da149 100644 --- a/src/utils/copyPasting.ts +++ b/src/utils/copyPasting.ts @@ -38,9 +38,13 @@ export const parseTextPlainData = (data: string): string[][] => { const saveCell = () => { let str = cleanData.slice(startCell, cursor) + if (str[0] === '"' && str[str.length - 1] === '"') { + quoted = true + } if (quoted && str[str.length - 1] === '"' && str.includes('\n')) { str = str.slice(1, str.length - 1).replace(/""/g, '"') + quoted = false } if (quoted && str[str.length - 1] !== '"') {