2019年10月16日 星期三

整理csv雙引號工具

Input:
Output:


原始碼:
<div>
Input:
<br />
<textarea id="rowdata" cols="40" rows="10" onclick="select()"></textarea>
</div>
<div>
Output:
<br />
<textarea id="newdata" cols="40" rows="10" onclick="select()"></textarea>
</div>
<script type="text/javascript">
let timer = 0;
document.getElementById("rowdata").onkeyup = () => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
const lines = document.getElementById("rowdata").value.split("\n");
const opt = document.getElementById("newdata");
opt.value = "";
lines.forEach(line => {
const isMatch = line.match(/^[^,]+,[^\"\s]/i);
if(isMatch) {
opt.value += line.replace(",", ",\"") + "\"\r\n";
} else {
const isNullMatch = line.match(/^[^,]+,$/i);
if(isNullMatch) {
opt.value += line + "\"\"\r\n";
} else {
const isTailMatch = line.match(/\"$/i);
if(isTailMatch) {
opt.value += line + "\r\n";
} else {
opt.value += line + "\\r\\n";
}
}
}
});
}, 1000);
};
</script>