2017-02-02 18 views
4

現在、Excel(MacOS上)からブラウザに1列の複数の行をコピーする際の問題を扱っています。私はeventを受けていますし、このようにコピーされたコンテンツへのアクセス:私は、コンテンツを受信した後event.clipboardData.getData('text/plain');ExcelからChromeにコンテンツをペーストする

を、私はこのような様々なマッチャでそれらを分割したい:

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

これはFirefoxで完璧に動作しますが、それがありません最新のChromeやSafariでは使用できません。私は、新しい行を\nまたは\tと一致させると思いました。私の目標は、行ごとに値の配列を取得することです。 AppleのNumbersを使用するとすべてが完璧に機能するので、これはExcelの特別な行末と関係していると思います。

本当にありがとうございました。前もって感謝します!

答えて

5

必要なのは、CR、キャリッジリターン、シンボルを(それはMS Officeドキュメントのデフォルトの改行のスタイルである)を追加することです。また、String#splitメソッドでは、正規表現を引数として渡したグローバル修飾子gを使用する必要はありません。これは、このメソッドの動作がデフォルトでそうであるためです。

使用

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

これはうまくいきました。

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

あなたが必要とするすべては '\のr'追加することです:' constの区切りを= [ ''、 ';'、 '\\('、 '\\)'、「\ \ * '、'/'、': '、' \\? '、' \ n '、' \ t '、' \ r '];また、 '' g ''修飾子は必要ありません。これは 'split'のデフォルトの動作です。 –

+0

@WiktorStribiżewああ、それはさらに良い!ありがとうございました – jonicious

関連する問題