Last active
March 7, 2018 08:59
-
-
Save azami/8045d2d2249f3e384aacb865d5ee8509 to your computer and use it in GitHub Desktop.
エクセルのコピペされたセルをいいかんじにパースしたい
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse(data) { | |
function unescapeQuote(string) { | |
return string | |
.replace(/""/, '"') | |
.replace(/^"/, '') | |
.replace(/"$/, ''); | |
} | |
const parsed = []; | |
data.replace(/\r/g, '\n') | |
.replace(/"\n/g, '""\n') | |
.split('"\n') | |
.map(x => x.split('\t')) | |
.forEach((x) => { | |
parsed.push([]); | |
x.forEach((y) => { | |
if (y.includes('\n')) { | |
if (y.startsWith('"') && y.endsWith('"')) { | |
parsed[parsed.length - 1].push(unescapeQuote(y)); | |
} else { | |
const [end, first] = y.split('\n'); | |
parsed[parsed.length - 1].push(unescapeQuote(end)); | |
parsed.push([]); | |
parsed[parsed.length - 1].push(unescapeQuote(first)); | |
} | |
} else { | |
parsed[parsed.length - 1].push(unescapeQuote(y)); | |
} | |
}); | |
}); | |
return parsed; | |
} | |
export default parse; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment