Skip to content

Instantly share code, notes, and snippets.

@azami
Last active March 7, 2018 08:59
Show Gist options
  • Save azami/8045d2d2249f3e384aacb865d5ee8509 to your computer and use it in GitHub Desktop.
Save azami/8045d2d2249f3e384aacb865d5ee8509 to your computer and use it in GitHub Desktop.
エクセルのコピペされたセルをいいかんじにパースしたい
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