DataTables Excel Export - How to remove HTML tags from the export

Screenshot from an Excel file showing HTML tags inside cells

Screenshot from an Excel file showing HTML tags have been removed

By default, DataTables include HTML tags in an Excel export if you have links or text styles on your View columns. With the help of the code snippet below, you'll be able to remove those tags from your Excel export files.

document.addEventListener("DOMContentLoaded",()=>{wp.hooks.addFilter("gk.datatables.options","gv-strip-html-excel",t=>(t.buttons=t.buttons.map(t=>("excel"===t.extend&&(t.exportOptions=t.exportOptions||{},t.exportOptions.stripHtml=!0,t.exportOptions.format=t.exportOptions.format||{},t.exportOptions.format.body=(t,o,e,p)=>p?p.innerText:t.replace(/<[^>]*?>/g,"")),t)),t))});

This code has been minified to make it easy to copy. Here's a link to the full code.

This JavaScript code should be copied and pasted, as it is, on the Custom JavaScript editor that sits inside View Editor > View Settings metabox > Custom Code tab.

Screenshot showing the View's Settings metabox highlighting the Custom Javascript section inside the Custom Code tab

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us