Exploring the Issue
I am currently working on a project using Next.js 14 where users can paste data copied from an Excel file into a spreadsheet-like component called react-data-grid. However, I have encountered some inconsistencies when copy-pasting on MacOS and Windows. Let's delve into the example below:
https://i.sstatic.net/YDEVB.png
Objective, Outcome, and Current Strategy
In this scenario, the goal is to receive 6 cells: Rows separated by \n
and columns by \r
. Unfortunately, the actual results are as follows:
For MacOS:
1\r\n2\r\n3\r\n\r\n5
. The empty cell at the end is missing for MacOS, while the unnecessary\r
is included even though the selected area does not span over two columns.For Windows:
1\n2\n3\n\n\n
. An extra line break is added at the end and the\r
characters are absent. Due to this inconsistency, the results differ significantly between operating systems:
const text = await navigator.clipboard.readText();
const rows = text.split("\n")
const columns = breaks.map(line => line.split("\r"));
// Desired output should be [["1"], ["2"], ["3"], [""], ["5"], [""]]
//
// 1. MacOS output: [["1". ""], ["2", ""], ["3", ""], ["", ""], ["5"]]
//
// 2. Windows output: ["1"], ["2"], ["3"], [""], ["5"], [""], [""]]
Although I can differentiate between the two operating systems, I am struggling to process these inconsistencies to achieve the desired format.