2022-03-09 01:44:10 +00:00
|
|
|
var _Readable;
|
|
|
|
function set_readable(R) { _Readable = R; }
|
2017-04-09 04:03:19 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
function write_csv_stream(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) {
|
|
|
|
var stream = _Readable();
|
|
|
|
var o = opts == null ? {} : opts;
|
|
|
|
if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
|
|
|
|
var r = safe_decode_range(sheet["!ref"]);
|
|
|
|
var FS = o.FS !== undefined ? o.FS : ",", fs = FS.charCodeAt(0);
|
|
|
|
var RS = o.RS !== undefined ? o.RS : "\n", rs = RS.charCodeAt(0);
|
|
|
|
var row/*:?string*/ = "", cols/*:Array<string>*/ = [];
|
|
|
|
var colinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
|
|
|
|
var rowinfo/*:Array<RowInfo>*/ = o.skipHidden && sheet["!rows"] || [];
|
|
|
|
for(var C = r.s.c; C <= r.e.c; ++C) if (!((colinfo[C]||{}).hidden)) cols[C] = encode_col(C);
|
|
|
|
var R = r.s.r;
|
|
|
|
var BOM = false, w = 0;
|
|
|
|
stream._read = function() {
|
|
|
|
if(!BOM) { BOM = true; return stream.push("\uFEFF"); }
|
|
|
|
while(R <= r.e.r) {
|
|
|
|
++R;
|
|
|
|
if ((rowinfo[R-1]||{}).hidden) continue;
|
|
|
|
row = make_csv_row(sheet, r, R-1, cols, fs, rs, FS, o);
|
|
|
|
if(row != null) {
|
|
|
|
if(row || (o.blankrows !== false)) return stream.push((w++ ? RS : "") + row);
|
2017-04-09 04:03:19 +00:00
|
|
|
}
|
2022-03-09 01:44:10 +00:00
|
|
|
}
|
|
|
|
return stream.push(null);
|
2017-04-09 04:03:19 +00:00
|
|
|
};
|
2022-03-09 01:44:10 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2017-04-09 04:03:19 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
function write_html_stream(ws/*:Worksheet*/, opts/*:?Sheet2HTMLOpts*/) {
|
|
|
|
var stream = _Readable();
|
2017-04-16 07:31:21 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
var o = opts || {};
|
2022-03-20 01:54:41 +00:00
|
|
|
var header = o.header != null ? o.header : HTML_BEGIN;
|
|
|
|
var footer = o.footer != null ? o.footer : HTML_END;
|
2022-03-09 01:44:10 +00:00
|
|
|
stream.push(header);
|
|
|
|
var r = decode_range(ws['!ref']);
|
2022-03-20 01:54:41 +00:00
|
|
|
stream.push(make_html_preamble(ws, r, o));
|
2022-03-09 01:44:10 +00:00
|
|
|
var R = r.s.r;
|
|
|
|
var end = false;
|
|
|
|
stream._read = function() {
|
|
|
|
if(R > r.e.r) {
|
|
|
|
if(!end) { end = true; stream.push("</table>" + footer); }
|
|
|
|
return stream.push(null);
|
|
|
|
}
|
|
|
|
while(R <= r.e.r) {
|
2022-03-20 01:54:41 +00:00
|
|
|
stream.push(make_html_row(ws, r, R, o));
|
2022-03-09 01:44:10 +00:00
|
|
|
++R;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-16 07:31:21 +00:00
|
|
|
};
|
2022-03-09 01:44:10 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2017-04-09 04:03:19 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
function write_json_stream(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) {
|
|
|
|
var stream = _Readable({objectMode:true});
|
2018-07-09 03:46:11 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
if(sheet == null || sheet["!ref"] == null) { stream.push(null); return stream; }
|
|
|
|
var val = {t:'n',v:0}, header = 0, offset = 1, hdr/*:Array<any>*/ = [], v=0, vv="";
|
|
|
|
var r = {s:{r:0,c:0},e:{r:0,c:0}};
|
|
|
|
var o = opts || {};
|
|
|
|
var range = o.range != null ? o.range : sheet["!ref"];
|
|
|
|
if(o.header === 1) header = 1;
|
|
|
|
else if(o.header === "A") header = 2;
|
|
|
|
else if(Array.isArray(o.header)) header = 3;
|
|
|
|
switch(typeof range) {
|
|
|
|
case 'string': r = safe_decode_range(range); break;
|
|
|
|
case 'number': r = safe_decode_range(sheet["!ref"]); r.s.r = range; break;
|
|
|
|
default: r = range;
|
|
|
|
}
|
|
|
|
if(header > 0) offset = 0;
|
|
|
|
var rr = encode_row(r.s.r);
|
|
|
|
var cols/*:Array<string>*/ = [];
|
|
|
|
var counter = 0;
|
2022-10-24 01:05:59 +00:00
|
|
|
var dense = sheet["!data"] != null;
|
2022-03-10 05:31:13 +00:00
|
|
|
var R = r.s.r, C = 0;
|
|
|
|
var header_cnt = {};
|
2022-10-24 01:05:59 +00:00
|
|
|
if(dense && !sheet["!data"][R]) sheet["!data"][R] = [];
|
2022-03-12 00:24:23 +00:00
|
|
|
var colinfo/*:Array<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
|
|
|
|
var rowinfo/*:Array<RowInfo>*/ = o.skipHidden && sheet["!rows"] || [];
|
2022-03-09 01:44:10 +00:00
|
|
|
for(C = r.s.c; C <= r.e.c; ++C) {
|
2022-03-12 00:24:23 +00:00
|
|
|
if(((colinfo[C]||{}).hidden)) continue;
|
2022-03-09 01:44:10 +00:00
|
|
|
cols[C] = encode_col(C);
|
2022-10-24 01:05:59 +00:00
|
|
|
val = dense ? sheet["!data"][R][C] : sheet[cols[C] + rr];
|
2022-03-09 01:44:10 +00:00
|
|
|
switch(header) {
|
|
|
|
case 1: hdr[C] = C - r.s.c; break;
|
|
|
|
case 2: hdr[C] = cols[C]; break;
|
|
|
|
case 3: hdr[C] = o.header[C - r.s.c]; break;
|
|
|
|
default:
|
|
|
|
if(val == null) val = {w: "__EMPTY", t: "s"};
|
|
|
|
vv = v = format_cell(val, null, o);
|
2022-03-10 05:31:13 +00:00
|
|
|
counter = header_cnt[v] || 0;
|
|
|
|
if(!counter) header_cnt[v] = 1;
|
|
|
|
else {
|
|
|
|
do { vv = v + "_" + (counter++); } while(header_cnt[vv]); header_cnt[v] = counter;
|
|
|
|
header_cnt[vv] = 1;
|
|
|
|
}
|
2022-03-09 01:44:10 +00:00
|
|
|
hdr[C] = vv;
|
2018-07-09 03:46:11 +00:00
|
|
|
}
|
2022-03-09 01:44:10 +00:00
|
|
|
}
|
|
|
|
R = r.s.r + offset;
|
|
|
|
stream._read = function() {
|
|
|
|
while(R <= r.e.r) {
|
2022-03-12 00:24:23 +00:00
|
|
|
if ((rowinfo[R-1]||{}).hidden) continue;
|
2022-10-24 01:05:59 +00:00
|
|
|
var row = make_json_row(sheet, r, R, cols, header, hdr, o);
|
2022-03-09 01:44:10 +00:00
|
|
|
++R;
|
|
|
|
if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) {
|
|
|
|
stream.push(row.row);
|
|
|
|
return;
|
2018-07-09 03:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 01:44:10 +00:00
|
|
|
return stream.push(null);
|
2017-04-09 04:03:19 +00:00
|
|
|
};
|
2022-03-09 01:44:10 +00:00
|
|
|
return stream;
|
2022-03-10 05:31:13 +00:00
|
|
|
}
|
2017-05-11 07:29:59 +00:00
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
var __stream = {
|
|
|
|
to_json: write_json_stream,
|
|
|
|
to_html: write_html_stream,
|
|
|
|
to_csv: write_csv_stream,
|
2022-05-22 23:51:41 +00:00
|
|
|
set_readable: set_readable
|
2022-03-09 01:44:10 +00:00
|
|
|
};
|