javascript - Paste content from Excel to Chrome - Stack Overflow

I am currently dealing with an issue of copying multiple rows of one column from Excel (on macOS) to th

I am currently dealing with an issue of copying multiple rows of one column from Excel (on macOS) to the browser. I am receiving an event and access the copied content like this: event.clipboardData.getData('text/plain');

After I received the content, I want to split them with various matchers like this:

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

This works perfectly in Firefox but it does not in latest Chrome and Safari. I thought, you would match new rows with \n or \t. My goal is to retrieve an array of values per line. I imagine this has something to do with special line endings of Excel because when using Numbers from Apple everything works perfectly.

Any help really appreciated. Thanks in advance!

I am currently dealing with an issue of copying multiple rows of one column from Excel (on macOS) to the browser. I am receiving an event and access the copied content like this: event.clipboardData.getData('text/plain');

After I received the content, I want to split them with various matchers like this:

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

This works perfectly in Firefox but it does not in latest Chrome and Safari. I thought, you would match new rows with \n or \t. My goal is to retrieve an array of values per line. I imagine this has something to do with special line endings of Excel because when using Numbers from Apple everything works perfectly.

Any help really appreciated. Thanks in advance!

Share Improve this question asked Feb 2, 2017 at 16:50 joniciousjonicious 3641 gold badge4 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

All you need is to add a CR, carriage return, symbol (it is the default line break style in MS Office documents). Also, String#split method does not require the use of the g global modifier with the regex passed as an argument, as this method behavior is like that by default.

Use

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

Okay, I got this working.

    /*
     * 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's 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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745341646a4623353.html

相关推荐

  • javascript - Paste content from Excel to Chrome - Stack Overflow

    I am currently dealing with an issue of copying multiple rows of one column from Excel (on macOS) to th

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信