Skip to content

Instantly share code, notes, and snippets.

@drodsou
Last active March 17, 2022 18:46
Show Gist options
  • Save drodsou/9989ed5c8f30bf9e213117f795c72f67 to your computer and use it in GitHub Desktop.
Save drodsou/9989ed5c8f30bf9e213117f795c72f67 to your computer and use it in GitHub Desktop.
Copy HTML table to clipboard, for pasting in Excel (javascript)
// In Excel make sure to have this option enabled: Home / Clipboard / Options / Collect without showing the Office clipboard
// Tested in Windows 10 only
let table = document.querySelector('#table').outerHTML;
table = table
.replaceAll('\n','<br style="mso-data-placement:same-cell;"/>') // new lines inside html cells => Alt+Enter in Excel
.replaceAll('<td','<td style="vertical-align: top;"'); // align top
navigator.clipboard.writeText(table).then(
()=>console.log("success"),
(e)=>console.log("error", e),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment