From 3a26260c8499365460e70ce90f4b45243524a839 Mon Sep 17 00:00:00 2001 From: reviewher <24845478+reviewher@users.noreply.github.com> Date: Fri, 5 Nov 2021 20:34:54 -0700 Subject: [PATCH] HTML string parse multiple tables [ci skip] --- bits/79_html.js | 7 ++++++- test.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/bits/79_html.js b/bits/79_html.js index 904b724..b8849cf 100644 --- a/bits/79_html.js +++ b/bits/79_html.js @@ -59,7 +59,12 @@ var HTML_ = (function() { return ws; } function html_to_book(str/*:string*/, opts)/*:Workbook*/ { - return sheet_to_workbook(html_to_sheet(str, opts), opts); + var mtch = str.match(/[\s\S]*?<\/table>/gi); + if(!mtch || mtch.length == 0) throw new Error("Invalid HTML: could not find "); + if(mtch.length == 1) return sheet_to_workbook(html_to_sheet(mtch[0], opts), opts); + var wb = utils.book_new(); + mtch.forEach(function(s, idx) { utils.book_append_sheet(wb, html_to_sheet(s, opts), "Sheet" + (idx+1)); }); + return wb; } function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o/*:Sheet2HTMLOpts*/)/*:string*/ { var M/*:Array*/ = (ws['!merges'] ||[]); diff --git a/test.js b/test.js index 465b2e6..528f9db 100644 --- a/test.js +++ b/test.js @@ -2160,6 +2160,18 @@ describe('HTML', function() { var wb = X.read(table, {type:"string"}); assert.equal(get_cell(wb.Sheets.Sheet1, "A1").v, "foo\nbar"); }); + it.skip('should generate multi-sheet workbooks', function() { + var table = ""; + for(var i = 0; i < 4; ++i) table += "
" + X.utils.encode_col(i) + "" + i + "
"; + table += table; table += table; + var wb = X.read(table, {type: "string"}); + assert.equal(wb.SheetNames.length, 4); + assert.equal(wb.SheetNames[1], "Sheet2"); + for(var j = 0; j < 4; ++j) { + assert.equal(get_cell(wb.Sheets.Sheet + (j+1), "A1"), X.utils.encode_col(j)); + assert.equal(get_cell(wb.Sheets.Sheet + (j+1), "B1"), j); + } + }); }); (domtest ? describe : describe.skip)('input DOM', function() { it('should interpret values by default', function() { plaintext_test(X.utils.table_to_book(get_dom_element(html_str)), false); });