Skip to content

Commit 3a26260

Browse files
committed
HTML string parse multiple tables [ci skip]
1 parent b5c697e commit 3a26260

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

bits/79_html.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ var HTML_ = (function() {
5959
return ws;
6060
}
6161
function html_to_book(str/*:string*/, opts)/*:Workbook*/ {
62-
return sheet_to_workbook(html_to_sheet(str, opts), opts);
62+
var mtch = str.match(/<table.*?>[\s\S]*?<\/table>/gi);
63+
if(!mtch || mtch.length == 0) throw new Error("Invalid HTML: could not find <table>");
64+
if(mtch.length == 1) return sheet_to_workbook(html_to_sheet(mtch[0], opts), opts);
65+
var wb = utils.book_new();
66+
mtch.forEach(function(s, idx) { utils.book_append_sheet(wb, html_to_sheet(s, opts), "Sheet" + (idx+1)); });
67+
return wb;
6368
}
6469
function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o/*:Sheet2HTMLOpts*/)/*:string*/ {
6570
var M/*:Array<Range>*/ = (ws['!merges'] ||[]);

test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,6 +2160,18 @@ describe('HTML', function() {
21602160
var wb = X.read(table, {type:"string"});
21612161
assert.equal(get_cell(wb.Sheets.Sheet1, "A1").v, "foo\nbar");
21622162
});
2163+
it.skip('should generate multi-sheet workbooks', function() {
2164+
var table = "";
2165+
for(var i = 0; i < 4; ++i) table += "<table><tr><td>" + X.utils.encode_col(i) + "</td><td>" + i + "</td></tr></table>";
2166+
table += table; table += table;
2167+
var wb = X.read(table, {type: "string"});
2168+
assert.equal(wb.SheetNames.length, 4);
2169+
assert.equal(wb.SheetNames[1], "Sheet2");
2170+
for(var j = 0; j < 4; ++j) {
2171+
assert.equal(get_cell(wb.Sheets.Sheet + (j+1), "A1"), X.utils.encode_col(j));
2172+
assert.equal(get_cell(wb.Sheets.Sheet + (j+1), "B1"), j);
2173+
}
2174+
});
21632175
});
21642176
(domtest ? describe : describe.skip)('input DOM', function() {
21652177
it('should interpret values by default', function() { plaintext_test(X.utils.table_to_book(get_dom_element(html_str)), false); });

0 commit comments

Comments
 (0)