2017-05-17 04:23:36 +00:00
|
|
|
/* note: browser DOM element cannot see mso- style attrs, must parse */
|
2017-04-16 04:32:13 +00:00
|
|
|
var HTML_ = (function() {
|
|
|
|
function html_to_sheet(str/*:string*/, _opts)/*:Workbook*/ {
|
|
|
|
var opts = _opts || {};
|
|
|
|
if(DENSE != null && opts.dense == null) opts.dense = DENSE;
|
|
|
|
var ws/*:Worksheet*/ = opts.dense ? ([]/*:any*/) : ({}/*:any*/);
|
|
|
|
var i = str.indexOf("<table"), j = str.indexOf("</table");
|
|
|
|
if(i == -1 || j == -1) throw new Error("Invalid HTML: missing <table> / </table> pair");
|
|
|
|
var rows = str.slice(i, j).split(/(:?<tr[^>]*>)/);
|
|
|
|
var R = -1, C = 0, RS = 0, CS = 0;
|
|
|
|
var range = {s:{r:10000000, c:10000000},e:{r:0,c:0}};
|
|
|
|
var merges = [], midx = 0;
|
|
|
|
for(i = 0; i < rows.length; ++i) {
|
|
|
|
var row = rows[i].trim();
|
|
|
|
if(row.substr(0,3) == "<tr") { ++R; C = 0; continue; }
|
|
|
|
if(row.substr(0,3) != "<td") continue;
|
|
|
|
var cells = row.split("</td>");
|
|
|
|
for(j = 0; j < cells.length; ++j) {
|
|
|
|
var cell = cells[j].trim();
|
|
|
|
if(cell.substr(0,3) != "<td") continue;
|
|
|
|
var m = cell, cc = 0;
|
|
|
|
/* TODO: parse styles etc */
|
|
|
|
while(m.charAt(0) == "<" && (cc = m.indexOf(">")) > -1) m = m.slice(cc+1);
|
|
|
|
while(m.indexOf(">") > -1) m = m.slice(0, m.lastIndexOf("<"));
|
|
|
|
var tag = parsexmltag(cell.slice(0, cell.indexOf(">")));
|
|
|
|
CS = tag.colspan ? +tag.colspan : 1;
|
|
|
|
if((RS = +tag.rowspan)>0 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
|
|
|
|
/* TODO: generate stub cells */
|
|
|
|
if(!m.length) { C += CS; continue; }
|
|
|
|
m = unescapexml(m).replace(/[\r\n]/g,"");
|
|
|
|
if(range.s.r > R) range.s.r = R;
|
|
|
|
if(range.e.r < R) range.e.r = R;
|
|
|
|
if(range.s.c > C) range.s.c = C;
|
|
|
|
if(range.e.c < C) range.e.c = C;
|
|
|
|
if(opts.dense) {
|
|
|
|
if(!ws[R]) ws[R] = [];
|
|
|
|
if(Number(m) == Number(m)) ws[R][C] = {t:'n', v:+m};
|
|
|
|
else ws[R][C] = {t:'s', v:m};
|
|
|
|
} else {
|
|
|
|
var coord/*:string*/ = encode_cell({r:R, c:C});
|
|
|
|
/* TODO: value parsing */
|
|
|
|
if(Number(m) == Number(m)) ws[coord] = {t:'n', v:+m};
|
|
|
|
else ws[coord] = {t:'s', v:m};
|
|
|
|
}
|
|
|
|
C += CS;
|
2017-04-08 06:55:35 +00:00
|
|
|
}
|
2017-03-09 05:24:32 +00:00
|
|
|
}
|
2017-04-16 04:32:13 +00:00
|
|
|
ws['!ref'] = encode_range(range);
|
|
|
|
return ws;
|
2017-03-09 05:24:32 +00:00
|
|
|
}
|
2017-04-16 04:32:13 +00:00
|
|
|
function html_to_book(str/*:string*/, opts)/*:Workbook*/ {
|
|
|
|
return sheet_to_workbook(html_to_sheet(str, opts), opts);
|
|
|
|
}
|
2017-05-16 17:45:35 +00:00
|
|
|
function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o/*:Sheet2HTMLOpts*/)/*:string*/ {
|
2017-04-16 04:32:13 +00:00
|
|
|
var M = (ws['!merges'] ||[]);
|
2017-04-16 07:31:21 +00:00
|
|
|
var oo = [];
|
2017-05-16 17:45:35 +00:00
|
|
|
var nullcell = "<td" + (o.editable ? ' contenteditable="true"' : "" ) + "></td>";
|
2017-04-16 07:31:21 +00:00
|
|
|
for(var C = r.s.c; C <= r.e.c; ++C) {
|
|
|
|
var RS = 0, CS = 0;
|
|
|
|
for(var j = 0; j < M.length; ++j) {
|
|
|
|
if(M[j].s.r > R || M[j].s.c > C) continue;
|
|
|
|
if(M[j].e.r < R || M[j].e.c < C) continue;
|
|
|
|
if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
|
|
|
|
RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
|
2017-04-16 04:32:13 +00:00
|
|
|
}
|
2017-04-16 07:31:21 +00:00
|
|
|
if(RS < 0) continue;
|
|
|
|
var coord = encode_cell({r:R,c:C});
|
|
|
|
var cell = o.dense ? (ws[R]||[])[C] : ws[coord];
|
2017-05-16 17:45:35 +00:00
|
|
|
if(!cell || cell.v == null) { oo.push(nullcell); continue; }
|
2017-04-16 07:31:21 +00:00
|
|
|
/* TODO: html entities */
|
|
|
|
var w = cell.h || escapexml(cell.w || (format_cell(cell), cell.w) || "");
|
|
|
|
var sp = {};
|
|
|
|
if(RS > 1) sp.rowspan = RS;
|
|
|
|
if(CS > 1) sp.colspan = CS;
|
2017-05-16 17:45:35 +00:00
|
|
|
if(o.editable) sp.contenteditable = "true";
|
2017-06-10 01:47:42 +00:00
|
|
|
sp.id = "sjs-" + coord;
|
2017-04-16 07:31:21 +00:00
|
|
|
oo.push(writextag('td', w, sp));
|
2017-04-16 04:32:13 +00:00
|
|
|
}
|
2017-06-10 01:47:42 +00:00
|
|
|
var preamble = "<tr>";
|
|
|
|
return preamble + oo.join("") + "</tr>";
|
2017-04-16 07:31:21 +00:00
|
|
|
}
|
2017-06-10 01:47:42 +00:00
|
|
|
function make_html_preamble(ws/*:Worksheet*/, R/*:Range*/, o/*:Sheet2HTMLOpts*/)/*:string*/ {
|
|
|
|
var out = [];
|
|
|
|
return out.join("") + '<table>';
|
|
|
|
}
|
|
|
|
var _BEGIN = '<html><head><meta charset="utf-8"/><title>SheetJS Table Export</title></head><body>';
|
|
|
|
var _END = '</body></html>';
|
|
|
|
function sheet_to_html(ws/*:Worksheet*/, opts/*:?Sheet2HTMLOpts*/, wb/*:?Workbook*/)/*:string*/ {
|
2017-05-13 18:21:22 +00:00
|
|
|
var o = opts || {};
|
2017-06-10 01:47:42 +00:00
|
|
|
var header = o.header != null ? o.header : _BEGIN;
|
|
|
|
var footer = o.footer != null ? o.footer : _END;
|
|
|
|
var out/*:Array<string>*/ = [header];
|
2017-04-16 07:31:21 +00:00
|
|
|
var r = decode_range(ws['!ref']);
|
|
|
|
o.dense = Array.isArray(ws);
|
2017-06-10 01:47:42 +00:00
|
|
|
out.push(make_html_preamble(ws, r, o));
|
2017-05-13 18:21:22 +00:00
|
|
|
for(var R = r.s.r; R <= r.e.r; ++R) out.push(make_html_row(ws, r, R, o));
|
2017-06-10 01:47:42 +00:00
|
|
|
out.push("</table>" + footer);
|
|
|
|
return out.join("");
|
2017-04-16 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
to_workbook: html_to_book,
|
|
|
|
to_sheet: html_to_sheet,
|
2017-04-16 07:31:21 +00:00
|
|
|
_row: make_html_row,
|
2017-05-16 17:45:35 +00:00
|
|
|
BEGIN: _BEGIN,
|
|
|
|
END: _END,
|
2017-06-10 01:47:42 +00:00
|
|
|
_preamble: make_html_preamble,
|
2017-04-16 04:32:13 +00:00
|
|
|
from_sheet: sheet_to_html
|
|
|
|
};
|
|
|
|
})();
|
2017-03-29 19:14:15 +00:00
|
|
|
|
2017-04-08 06:55:35 +00:00
|
|
|
function parse_dom_table(table/*:HTMLElement*/, _opts/*:?any*/)/*:Worksheet*/ {
|
|
|
|
var opts = _opts || {};
|
|
|
|
if(DENSE != null) opts.dense = DENSE;
|
|
|
|
var ws/*:Worksheet*/ = opts.dense ? ([]/*:any*/) : ({}/*:any*/);
|
2017-03-29 19:14:15 +00:00
|
|
|
var rows = table.getElementsByTagName('tr');
|
|
|
|
var range = {s:{r:0,c:0},e:{r:rows.length - 1,c:0}};
|
|
|
|
var merges = [], midx = 0;
|
|
|
|
var R = 0, _C = 0, C = 0, RS = 0, CS = 0;
|
|
|
|
for(; R < rows.length; ++R) {
|
|
|
|
var row = rows[R];
|
|
|
|
var elts = row.children;
|
|
|
|
for(_C = C = 0; _C < elts.length; ++_C) {
|
2017-05-17 04:23:36 +00:00
|
|
|
var elt = elts[_C], v = elts[_C].innerText || elts[_C].textContent;
|
2017-03-29 19:14:15 +00:00
|
|
|
for(midx = 0; midx < merges.length; ++midx) {
|
|
|
|
var m = merges[midx];
|
|
|
|
if(m.s.c == C && m.s.r <= R && R <= m.e.r) { C = m.e.c+1; midx = -1; }
|
|
|
|
}
|
|
|
|
/* TODO: figure out how to extract nonstandard mso- style */
|
|
|
|
CS = +elt.getAttribute("colspan") || 1;
|
2017-04-16 04:32:13 +00:00
|
|
|
if((RS = +elt.getAttribute("rowspan"))>0 || CS>1) merges.push({s:{r:R,c:C},e:{r:R + (RS||1) - 1, c:C + CS - 1}});
|
2017-05-17 04:23:36 +00:00
|
|
|
var o/*:Cell*/ = {t:'s', v:v};
|
|
|
|
if(v != null && v.length) {
|
|
|
|
if(!isNaN(Number(v))) o = {t:'n', v:Number(v)};
|
|
|
|
else if(!isNaN(fuzzydate(v).getDate())) {
|
|
|
|
o = ({t:'d', v:parseDate(v)}/*:any*/);
|
|
|
|
if(!opts.cellDates) o = ({t:'n', v:datenum(o.v)}/*:any*/);
|
|
|
|
o.z = opts.dateNF || SSF._table[14];
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 06:55:35 +00:00
|
|
|
if(opts.dense) { if(!ws[R]) ws[R] = []; ws[R][C] = o; }
|
|
|
|
else ws[encode_cell({c:C, r:R})] = o;
|
2017-04-04 16:09:41 +00:00
|
|
|
if(range.e.c < C) range.e.c = C;
|
2017-03-29 19:14:15 +00:00
|
|
|
C += CS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws['!merges'] = merges;
|
2017-04-01 07:32:12 +00:00
|
|
|
ws['!ref'] = encode_range(range);
|
2017-03-29 19:14:15 +00:00
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
function table_to_book(table/*:HTMLElement*/, opts/*:?any*/)/*:Workbook*/ {
|
|
|
|
return sheet_to_workbook(parse_dom_table(table, opts), opts);
|
|
|
|
}
|