将内容从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());
- 将行号作为variables传递,并引用其他表单
- 使用喜欢比较一个string在Excel中
- C#正则expression式 – 从Excel公式中获取具有R1C1引用的行数据
- VBA Excel正则expression式 – \ b字边界不匹配,如果字是在string的开始
- 将数字串分成单独的单元格,包括括号/括号内的数字
- RegEx提取电子邮件
- Excel 2007中的VBAmacros。我想在Excel VBA中使用正则expression式来将“他”replace为“她”,“他”replace为“她”,“他”replace为“她”
- 使用Regex和PowerShellreplaceCSV中的多个行模式?
- 如何在Excel中使用Excelreplace<*>如在Excel中? (常用expression)