Skip to content

Commit 5cd07c5

Browse files
committed
version bump 0.9.11: streaming HTML write
1 parent 865b8dd commit 5cd07c5

16 files changed

+1046
-364
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,14 @@ saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx");
410410

411411
### Streaming Write
412412

413-
`XLSX.stream.to_csv` is the streaming version of `XLSX.utils.sheet_to_csv`. It
414-
takes the same arguments but returns a readable stream.
413+
The streaming write functions are available in the `XLSX.stream` object. They
414+
take the same arguments as the normal write functions but return a readable
415+
stream. They are only exposed in node.
415416

416-
<https://github.com/sheetjs/sheetaki> pipes CSV write stream to nodejs response.
417+
- `XLSX.stream.to_csv` is the streaming version of `XLSX.utils.sheet_to_csv`.
418+
- `XLSX.stream.to_html` is the streaming version of the HTML output type.
419+
420+
<https://github.com/sheetjs/sheetaki> pipes write streams to nodejs response.
417421
## Interface
418422

419423
`XLSX` is the exposed variable in the browser and the exported node variable
@@ -439,6 +443,8 @@ Parse options are described in the [Parsing Options](#parsing-options) section.
439443
`XLSX.writeFileAsync(filename, wb, o, cb)` attempts to write `wb` to `filename`.
440444
If `o` is omitted, the writer will use the third argument as the callback.
441445

446+
`XLSX.stream` contains a set of streaming write functions.
447+
442448
Write options are described in the [Writing Options](#writing-options) section.
443449

444450
### Utilities

bits/01_version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
XLSX.version = '0.9.10';
1+
XLSX.version = '0.9.11';

bits/79_html.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,40 +51,42 @@ var HTML_ = (function() {
5151
function html_to_book(str/*:string*/, opts)/*:Workbook*/ {
5252
return sheet_to_workbook(html_to_sheet(str, opts), opts);
5353
}
54-
function sheet_to_html(ws/*:Worksheet*/, opts)/*:string*/ {
55-
var o/*:Array<string>*/ = [];
56-
var r = decode_range(ws['!ref']), cell/*:Cell*/;
57-
var dense = Array.isArray(ws);
54+
function make_html_row(ws/*:Worksheet*/, r/*:Range*/, R/*:number*/, o)/*:string*/ {
5855
var M = (ws['!merges'] ||[]);
59-
for(var R = r.s.r; R <= r.e.r; ++R) {
60-
var oo = [];
61-
for(var C = r.s.c; C <= r.e.c; ++C) {
62-
var RS = 0, CS = 0;
63-
for(var j = 0; j < M.length; ++j) {
64-
if(M[j].s.r > R || M[j].s.c > C) continue;
65-
if(M[j].e.r < R || M[j].e.c < C) continue;
66-
if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
67-
RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
68-
}
69-
if(RS < 0) continue;
70-
var coord = encode_cell({r:R,c:C});
71-
cell = dense ? (ws[R]||[])[C] : ws[coord];
72-
if(!cell || cell.v == null) { oo.push("<td></td>"); continue; }
73-
/* TODO: html entities */
74-
var w = cell.h || escapexml(cell.w || (format_cell(cell), cell.w) || "");
75-
var sp = {};
76-
if(RS > 1) sp.rowspan = RS;
77-
if(CS > 1) sp.colspan = CS;
78-
oo.push(writextag('td', w, sp));
56+
var oo = [];
57+
for(var C = r.s.c; C <= r.e.c; ++C) {
58+
var RS = 0, CS = 0;
59+
for(var j = 0; j < M.length; ++j) {
60+
if(M[j].s.r > R || M[j].s.c > C) continue;
61+
if(M[j].e.r < R || M[j].e.c < C) continue;
62+
if(M[j].s.r < R || M[j].s.c < C) { RS = -1; break; }
63+
RS = M[j].e.r - M[j].s.r + 1; CS = M[j].e.c - M[j].s.c + 1; break;
7964
}
80-
o.push("<tr>" + oo.join("") + "</tr>");
65+
if(RS < 0) continue;
66+
var coord = encode_cell({r:R,c:C});
67+
var cell = o.dense ? (ws[R]||[])[C] : ws[coord];
68+
if(!cell || cell.v == null) { oo.push("<td></td>"); continue; }
69+
/* TODO: html entities */
70+
var w = cell.h || escapexml(cell.w || (format_cell(cell), cell.w) || "");
71+
var sp = {};
72+
if(RS > 1) sp.rowspan = RS;
73+
if(CS > 1) sp.colspan = CS;
74+
oo.push(writextag('td', w, sp));
8175
}
76+
return "<tr>" + oo.join("") + "</tr>";
77+
}
78+
function sheet_to_html(ws/*:Worksheet*/, opts)/*:string*/ {
79+
var o/*:Array<string>*/ = [];
80+
var r = decode_range(ws['!ref']);
81+
o.dense = Array.isArray(ws);
82+
for(var R = r.s.r; R <= r.e.r; ++R) o.push(make_html_row(ws, r, R, o));
8283
return "<html><body><table>" + o.join("") + "</table></body></html>";
8384
}
8485

8586
return {
8687
to_workbook: html_to_book,
8788
to_sheet: html_to_sheet,
89+
_row: make_html_row,
8890
from_sheet: sheet_to_html
8991
};
9092
})();

bits/97_node.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,35 @@ if(has_buf && typeof require != 'undefined') (function() {
2828
return stream;
2929
};
3030

31+
var HTML_BEGIN = "<html><body><table>";
32+
var HTML_END = "</table></body></html>";
33+
34+
var write_html_stream = function(sheet/*:Worksheet*/, opts) {
35+
var stream = Readable();
36+
37+
var o/*:Array<string>*/ = [];
38+
var r = decode_range(sheet['!ref']), cell/*:Cell*/;
39+
o.dense = Array.isArray(sheet);
40+
stream.push(HTML_BEGIN);
41+
42+
var R = r.s.r;
43+
var end = false;
44+
stream._read = function() {
45+
if(R > r.e.r) {
46+
if(!end) { end = true; stream.push(HTML_END); }
47+
return stream.push(null);
48+
}
49+
while(R <= r.e.r) {
50+
stream.push(HTML_._row(sheet, r, R, o));
51+
++R;
52+
break;
53+
}
54+
};
55+
return stream;
56+
};
3157

3258
XLSX.stream = {
59+
to_html: write_html_stream,
3360
to_csv: write_csv_stream
3461
};
3562
})();

dist/xlsx.core.min.js

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/xlsx.core.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/xlsx.full.min.js

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/xlsx.full.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)