i have this simple function in which i try to pass an array to the clipboard in order to paste in in excel later on
hiddenInputOnCopyHandler = ev => {
ev.clipboardData.setData('text/plain', ev.currentTarget.value.split(','));
ev.preventDefault();
};
but i'm getting a string instead, i guess because the type passed in the setData is 'text/plain' but i couldn't find better in its documentation
now the problem is that when it's pasted in excel, the whole serialized array will take only one cell
According to the docs , you should fire a copy
command to copy the selection to clipboard. Also you should check for browser compatibility.
Also currentTarget.value
gives undefined so you should use textContent
instead.
When you click on paragraph, then it fires the copy
command in copyAction
function and then eventListener
with copy
event executes the hiddenInputonCopyHandler
function.
function init(){
let el = document.getElementById("CpToClip");
el.addEventListener('click',copyAction);
el.addEventListener('copy', hiddenInputOnCopyHandler);
}
function copyAction(){
document.execCommand('copy');
}
hiddenInputOnCopyHandler = ev => {
let copiedVal = ev.currentTarget.textContent.trim().split(',').join('\t');
console.log(copiedVal);// "HHHhhh" "mkjf" "Tendue" "Bonne" "10-07-2020" "khgkg@kjk.od"
ev.clipboardData.setData('text/plain', copiedVal);
ev.preventDefault();
}
addEventListener('load',init);
Html
<p id="CpToClip">
HHHhhh,mkjf,Tendue,Bonne,10-07-2020,khgkg@kjk.od
</p>
Check my solution and let me know.
You should try tab \t
instead of comma ,
( reference )
ev.currentTarget.value.split(',').join('\t')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.