forked from sheetjs/docs.sheetjs.com
20 lines
878 B
JavaScript
20 lines
878 B
JavaScript
const XLSX = require("xlsx");
|
|
const { DOMParser, XMLSerializer } = require("@xmldom/xmldom");
|
|
|
|
(async() => {
|
|
const text = await (await fetch('https://docs.sheetjs.com/dom/SheetJSTable.html')).text();
|
|
const doc = new DOMParser().parseFromString( text, "text/html");
|
|
const tbl = doc.getElementsByTagName("table")[0];
|
|
|
|
/* patch XMLDOM */
|
|
tbl.rows = Array.from(tbl.getElementsByTagName("tr"));
|
|
tbl.rows.forEach(row => row.cells = Array.from(row.getElementsByTagName("td")));
|
|
Object.defineProperty(tbl.__proto__, "innerHTML", { get: function() {
|
|
var outerHTML = new XMLSerializer().serializeToString(this);
|
|
if(outerHTML.match(/</g).length == 1) return "";
|
|
return outerHTML.slice(0, outerHTML.lastIndexOf("</")).replace(/<[^"'>]*(("[^"]*"|'[^']*')[^"'>]*)*>/, "");
|
|
}});
|
|
|
|
const workbook = XLSX.utils.table_to_book(tbl);
|
|
XLSX.writeFile(workbook, "SheetJSXMLDOM.xlsx");
|
|
})(); |