Khi bạn copy các ô từ file excel muốn chuyển dữ liệu qua thì hãy làm như sau
<h1>Handle paste clipboard from excel by JS</h1>
<div id='out'></div>
<script>
// https://stackoverflow.com/a/38326762/466693
document.addEventListener('paste', function (event) {
window.clipText = event.clipboardData.getData('Text');
render_table(clipText);
});
function render_table(cb){
var html = "<table border='1'>";
cb.split("\n").forEach(function(line){
if(line){
html += "<tr>";
html += line.split('\t').map(function(l){ return "<td>"+ l +"</td>"; }).join('');
html += "</tr>";
}
});
html += "</table>";
document.querySelector('#out').innerHTML = html;
}
</script>
Kiến thức về Clipboard
Input1.oncopy = function(event) {
length = Input1.selectionEnd - Input1.selectionStart;
selectedText = Mid(Input1.value, Input1.selectionStart + 1, length);
modifiedText = UCase(selectedText);
clipboardData = event.clipboardData;
clipboardData.setData("text/plain", modifiedText);
//Stop the default copy from the field.
event.preventDefault();
NSB.MsgBox("Selected text:" + selectedText + '\n' + "Changed to:" + modifiedText);
};
Input2.onpaste = function(event) {
clipboardData = event.clipboardData;
tobePasted = clipboardData.getData("text/plain");
Input2.value = tobePasted + " was pasted";
//Stop the default paste to the field.
event.preventDefault();
NSB.MsgBox("Pasted text:" + tobePasted + '\n' + "Change to:" + Input2.value);
};
Textarea1.onpaste = function(event) { //Paste as HTML
clipboardData = event.clipboardData;
tobePasted = clipboardData.getData("text/html");
Textarea1.innerHTML = tobePasted + " was pasted";
//Stop the default paste to the field.
event.preventDefault();
NSB.MsgBox("Pasted text:" + tobePasted + '\n' + "Change to:" + Input2.value);
};
btnCopy.onclick = function() {
//execCommand does not work on all browsers and platforms
Input1.select();
document.execCommand("Copy");
};