Paste content from Excel to Chrome

I am currently dealing with the problem of copying multiple rows of one column from Excel (on macOS) to the browser. I get eventand access the copied content as follows:event.clipboardData.getData('text/plain');

After I got the content, I want to break them down with the following types:

const separators = [',', ';', '\\(', '\\)', '\\*', '/', ':', '\\?', '\n', '\t'];
return data.split(new RegExp(separators.join('|'), 'g')).map(d => d.trim());

This works great in Firefox, but not in the latest Chrome and Safari browsers. I thought you would match newlines with \nor \t. My goal is to get an array of values ​​per line. I guess this has something to do with Excel's special endings, because when using Numbers from Apple everything works fine.

Any help is really appreciated. Thanks in advance!

+4
source share
2 answers

All you need to do is add a CR character, carriage return , this is the default line break style in MS Office documents). In addition, the method String#splitdoes not require the use of a global modifier gwith a regular expression passed as an argument, since by default this method is like it.

Using

const separators = [',', ';', '\\(', '\\)', '\\*', '/', ':', '\\?', '\n', '\r'];
return data.split(new RegExp(separators.join('|'))).map(d => d.trim());
+5
source

Ok, I got this job.

    /*
     * Chrome and Safari seem to treat new lines pasted from Excel like an Enter press.
     * Enter has a CharCode of 13, so checking if this string contains CharCode 13 is sufficient.
     * If the string contains a CharCode 13, we split the string after every CharCode 13.
     * And kids, that how I made pasting from Excel possible (HIMYM voice)
     */

    const enterCharCode = String.fromCharCode(13);

    if (data.includes(enterCharCode)) {
        return data.split(enterCharCode);
    }

    const separators = [',', ';', '\\(', '\\)', '\\*', '/', ':', '\\?', '\n'];
    return data.split(new RegExp(separators.join('|'), 'g')).map(d => d.trim());
+2
source

Source: https://habr.com/ru/post/1668658/


All Articles