Bookmark this code to run on other sites!
Last active
November 18, 2021 08:37
-
-
Save Ephellon/ba126402d4dacfc02f7d0b47a1db1f17 to your computer and use it in GitHub Desktop.
Export HTML tables to Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:{let $=(e="",t=!1,o=document)=>t?[...o.querySelectorAll(e)]:o.querySelector(e),{body}=document,exportButton=document.createElement("button");function exportToExcel(e,t="download"){let o=e?.outerHTML?.replace(/<a[^>]*>(?:.*?)<\/a>/gi,"")?.replace(/<img[^>]*>/gi,"")?.replace(/<input[^>]*>/gi,"")?.replace(/cursor:default/g,"$_; background:#dddddd");if(!o)throw"Expected a table element";let{userAgent:r}=top.navigator;if(!!~r.indexOf("MSIE ")||/Trident.*rv\:11\./.test(r))throw error="Internet Explorer is not supported",alert(error),error;{let e=document.createElement("a");e.href=`data:application/vnd.ms-excel,${encodeURIComponent(o)}`,e.download=`${t}.xls`,document.body.append(e),e.click(),e.remove()}}exportButton.innerText="Export table to Excel",exportButton.onmouseup=e=>exportToExcel($(".tableContainer table[summary]"),prompt("Enter a filename:","download")),body.append(exportButton)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Saves table data as an Excel document */ | |
let $ = (query = '', multiple = false, container = document) => multiple? [...container.querySelectorAll(query)]: container.querySelector(query); | |
let { body } = document; | |
let exportButton = document.createElement('button'); | |
// Job specific... | |
exportButton.innerText = 'Export table to Excel'; | |
exportButton.onmouseup = event => exportToExcel($('.tableContainer table[summary]'), prompt('Enter a filename:', 'download')); | |
body.append(exportButton); | |
function exportToExcel(table, name = 'download') { | |
let data = table?.outerHTML | |
?.replace(/<a[^>]*>(?:.*?)<\/a>/gi, '') // Removes links | |
?.replace(/<img[^>]*>/gi, '') // Removes images | |
?.replace(/<input[^>]*>/gi, '') // Removes inputs | |
// Add some of the styling back... | |
?.replace(/cursor:default/g, `$_; background:#dddddd`) | |
; | |
if(!data) | |
throw 'Expected a table element'; | |
let { userAgent } = top.navigator, | |
MSIE = !!~userAgent.indexOf('MSIE '); | |
if(MSIE || /Trident.*rv\:11\./.test(userAgent)) { | |
// Internet Explorer - it shouldn't even get here... | |
error = 'Internet Explorer is not supported'; | |
alert(error); | |
throw error; | |
} else { | |
// All other browsers | |
let link = document.createElement('a'); | |
link.href = `data:application/vnd.ms-excel,${ encodeURIComponent(data) }`; | |
link.download = `${ name }.xls`; | |
document.body.append(link); | |
link.click(); | |
link.remove(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment