将内容从Excel粘贴到Chrome

我目前正在处理从Excel(在macOS上)复制一列的多行到浏览器的问题。 我正在接收一个event并访问复制的内容,如下所示: event.clipboardData.getData('text/plain');

收到内容之后,我想用这样的各种匹配器来分割它们:

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

这在Firefox中完美运行,但是在最新的Chrome和Safari中没有。 我想,你会匹配\n\t新行。 我的目标是每行检索一个值的数组。 我想这与Excel的特殊行结尾有关,因为当使用苹果公司的Numbers时,一切正常。

任何帮助真的很感激。 提前致谢!

您只需要添加一个CR, 回车符号(这是MS Office文档中的默认换行符样式)。 此外, String#split方法不要求使用带有作为parameter passing的正则expression式的g全局修饰符,因为此方法行为与默认类似。

使用

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

好的,我得到了这个工作。

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