2017-09-30 06:18:11 +00:00
|
|
|
function write_zip_type(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
|
2014-05-16 00:33:34 +00:00
|
|
|
var o = opts||{};
|
|
|
|
var z = write_zip(wb, o);
|
2017-02-03 20:50:45 +00:00
|
|
|
var oopts = {};
|
2017-02-10 19:23:01 +00:00
|
|
|
if(o.compression) oopts.compression = 'DEFLATE';
|
2014-05-16 00:33:34 +00:00
|
|
|
switch(o.type) {
|
2017-02-03 20:50:45 +00:00
|
|
|
case "base64": oopts.type = "base64"; break;
|
|
|
|
case "binary": oopts.type = "string"; break;
|
2018-02-03 20:46:32 +00:00
|
|
|
case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
|
2017-02-03 20:50:45 +00:00
|
|
|
case "buffer":
|
2018-02-03 20:46:32 +00:00
|
|
|
case "file": oopts.type = has_buf ? "nodebuffer" : "string"; break;
|
2014-05-16 00:33:34 +00:00
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
2018-02-03 20:46:32 +00:00
|
|
|
if(o.type === "file") return write_dl(o.file, z.generate(oopts));
|
2017-09-30 06:18:11 +00:00
|
|
|
var out = z.generate(oopts);
|
|
|
|
return o.type == "string" ? utf8read(out) : out;
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 06:18:11 +00:00
|
|
|
function write_cfb_type(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
|
2017-09-22 22:18:51 +00:00
|
|
|
var o = opts||{};
|
|
|
|
var cfb/*:CFBContainer*/ = write_xlscfb(wb, o);
|
|
|
|
switch(o.type) {
|
|
|
|
case "base64": case "binary": break;
|
|
|
|
case "buffer": case "array": o.type = ""; break;
|
2018-02-03 20:46:32 +00:00
|
|
|
case "file": return write_dl(o.file, CFB.write(cfb, {type:has_buf ? 'buffer' : ""}));
|
|
|
|
case "string": throw new Error("'string' output type invalid for '" + o.bookType + "' files");
|
2017-09-22 22:18:51 +00:00
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
|
|
|
return CFB.write(cfb, o);
|
|
|
|
}
|
|
|
|
|
2017-09-30 06:18:11 +00:00
|
|
|
function write_string_type(out/*:string*/, opts/*:WriteOpts*/, bom/*:?string*/)/*:any*/ {
|
|
|
|
if(!bom) bom = "";
|
|
|
|
var o = bom + out;
|
2017-04-03 00:16:03 +00:00
|
|
|
switch(opts.type) {
|
2017-09-30 06:18:11 +00:00
|
|
|
case "base64": return Base64.encode(utf8write(o));
|
|
|
|
case "binary": return utf8write(o);
|
|
|
|
case "string": return out;
|
2018-02-03 20:46:32 +00:00
|
|
|
case "file": return write_dl(opts.file, o, 'utf8');
|
2017-04-03 00:16:03 +00:00
|
|
|
case "buffer": {
|
2018-05-05 06:34:37 +00:00
|
|
|
if(has_buf) return Buffer.from(o, 'utf8');
|
2017-09-30 06:18:11 +00:00
|
|
|
else return write_string_type(o, {type:'binary'}).split("").map(function(c) { return c.charCodeAt(0); });
|
2017-04-03 00:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("Unrecognized type " + opts.type);
|
|
|
|
}
|
|
|
|
|
2017-09-30 06:18:11 +00:00
|
|
|
function write_stxt_type(out/*:string*/, opts/*:WriteOpts*/)/*:any*/ {
|
2017-02-22 06:57:59 +00:00
|
|
|
switch(opts.type) {
|
2017-02-24 10:33:01 +00:00
|
|
|
case "base64": return Base64.encode(out);
|
|
|
|
case "binary": return out;
|
2017-09-30 06:18:11 +00:00
|
|
|
case "string": return out; /* override in sheet_to_txt */
|
2018-02-03 20:46:32 +00:00
|
|
|
case "file": return write_dl(opts.file, out, 'binary');
|
2017-02-24 10:33:01 +00:00
|
|
|
case "buffer": {
|
2018-05-05 06:34:37 +00:00
|
|
|
if(has_buf) return Buffer.from(out, 'binary');
|
2017-02-24 10:33:01 +00:00
|
|
|
else return out.split("").map(function(c) { return c.charCodeAt(0); });
|
2017-03-12 18:02:43 +00:00
|
|
|
}
|
2017-02-22 06:57:59 +00:00
|
|
|
}
|
2017-03-12 18:02:43 +00:00
|
|
|
throw new Error("Unrecognized type " + opts.type);
|
2017-02-22 06:57:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 10:33:01 +00:00
|
|
|
/* TODO: test consistency */
|
2017-09-30 06:18:11 +00:00
|
|
|
function write_binary_type(out, opts/*:WriteOpts*/)/*:any*/ {
|
2017-02-22 06:57:59 +00:00
|
|
|
switch(opts.type) {
|
2017-09-30 06:18:11 +00:00
|
|
|
case "string":
|
2017-02-24 10:33:01 +00:00
|
|
|
case "base64":
|
2017-02-22 06:57:59 +00:00
|
|
|
case "binary":
|
|
|
|
var bstr = "";
|
2017-11-20 01:51:14 +00:00
|
|
|
// $FlowIgnore
|
2017-02-22 06:57:59 +00:00
|
|
|
for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
|
2017-09-30 06:18:11 +00:00
|
|
|
return opts.type == 'base64' ? Base64.encode(bstr) : opts.type == 'string' ? utf8read(bstr) : bstr;
|
2018-02-03 20:46:32 +00:00
|
|
|
case "file": return write_dl(opts.file, out);
|
2017-02-22 06:57:59 +00:00
|
|
|
case "buffer": return out;
|
|
|
|
default: throw new Error("Unrecognized type " + opts.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function writeSync(wb/*:Workbook*/, opts/*:?WriteOpts*/) {
|
2017-03-10 08:39:51 +00:00
|
|
|
check_wb(wb);
|
2015-04-02 20:32:22 +00:00
|
|
|
var o = opts||{};
|
2017-12-30 05:40:35 +00:00
|
|
|
if(o.type == "array") { o.type = "binary"; var out/*:string*/ = (writeSync(wb, o)/*:any*/); o.type = "array"; return s2ab(out); }
|
2017-04-02 06:47:25 +00:00
|
|
|
switch(o.bookType || 'xlsb') {
|
2017-03-14 08:19:51 +00:00
|
|
|
case 'xml':
|
|
|
|
case 'xlml': return write_string_type(write_xlml(wb, o), o);
|
2017-04-01 07:32:12 +00:00
|
|
|
case 'slk':
|
|
|
|
case 'sylk': return write_string_type(write_slk_str(wb, o), o);
|
2018-02-08 18:21:39 +00:00
|
|
|
case 'htm':
|
2017-04-16 04:32:13 +00:00
|
|
|
case 'html': return write_string_type(write_htm_str(wb, o), o);
|
2017-09-30 06:18:11 +00:00
|
|
|
case 'txt': return write_stxt_type(write_txt_str(wb, o), o);
|
|
|
|
case 'csv': return write_string_type(write_csv_str(wb, o), o, "\ufeff");
|
2017-04-01 07:32:12 +00:00
|
|
|
case 'dif': return write_string_type(write_dif_str(wb, o), o);
|
2017-10-27 16:25:54 +00:00
|
|
|
case 'dbf': return write_binary_type(write_dbf_buf(wb, o), o);
|
2017-04-03 00:16:03 +00:00
|
|
|
case 'prn': return write_string_type(write_prn_str(wb, o), o);
|
2017-08-05 06:32:57 +00:00
|
|
|
case 'rtf': return write_string_type(write_rtf_str(wb, o), o);
|
2017-12-04 04:41:41 +00:00
|
|
|
case 'eth': return write_string_type(write_eth_str(wb, o), o);
|
2017-02-22 06:57:59 +00:00
|
|
|
case 'fods': return write_string_type(write_ods(wb, o), o);
|
2017-09-30 06:18:11 +00:00
|
|
|
case 'biff2': if(!o.biff) o.biff = 2; /* falls through */
|
|
|
|
case 'biff3': if(!o.biff) o.biff = 3; /* falls through */
|
2017-09-22 22:18:51 +00:00
|
|
|
case 'biff4': if(!o.biff) o.biff = 4; return write_binary_type(write_biff_buf(wb, o), o);
|
2017-10-27 16:25:54 +00:00
|
|
|
case 'biff5': if(!o.biff) o.biff = 5; /* falls through */
|
2017-09-22 22:18:51 +00:00
|
|
|
case 'biff8':
|
2017-12-30 05:40:35 +00:00
|
|
|
case 'xla':
|
2017-09-22 22:18:51 +00:00
|
|
|
case 'xls': if(!o.biff) o.biff = 8; return write_cfb_type(wb, o);
|
2017-02-24 10:33:01 +00:00
|
|
|
case 'xlsx':
|
|
|
|
case 'xlsm':
|
2017-12-30 05:40:35 +00:00
|
|
|
case 'xlam':
|
2017-02-24 10:33:01 +00:00
|
|
|
case 'xlsb':
|
|
|
|
case 'ods': return write_zip_type(wb, o);
|
|
|
|
default: throw new Error ("Unrecognized bookType |" + o.bookType + "|");
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 16:25:54 +00:00
|
|
|
function resolve_book_type(o/*:WriteFileOpts*/) {
|
|
|
|
if(o.bookType) return;
|
|
|
|
var _BT = {
|
|
|
|
"xls": "biff8",
|
|
|
|
"htm": "html",
|
|
|
|
"slk": "sylk",
|
2017-12-04 04:41:41 +00:00
|
|
|
"socialcalc": "eth",
|
2017-10-27 16:25:54 +00:00
|
|
|
"Sh33tJS": "WTF"
|
|
|
|
};
|
|
|
|
var ext = o.file.slice(o.file.lastIndexOf(".")).toLowerCase();
|
|
|
|
if(ext.match(/^\.[a-z]+$/)) o.bookType = ext.slice(1);
|
|
|
|
o.bookType = _BT[o.bookType] || o.bookType;
|
2017-03-25 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeFileSync(wb/*:Workbook*/, filename/*:string*/, opts/*:?WriteFileOpts*/) {
|
|
|
|
var o = opts||{}; o.type = 'file';
|
|
|
|
o.file = filename;
|
|
|
|
resolve_book_type(o);
|
2014-05-16 00:33:34 +00:00
|
|
|
return writeSync(wb, o);
|
|
|
|
}
|
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
function writeFileAsync(filename/*:string*/, wb/*:Workbook*/, opts/*:?WriteFileOpts*/, cb/*:?(e?:ErrnoError)=>void*/) {
|
|
|
|
var o = opts||{}; o.type = 'file';
|
|
|
|
o.file = filename;
|
|
|
|
resolve_book_type(o);
|
|
|
|
o.type = 'buffer';
|
|
|
|
var _cb = cb; if(!(_cb instanceof Function)) _cb = (opts/*:any*/);
|
|
|
|
return _fs.writeFile(filename, writeSync(wb, o), _cb);
|
|
|
|
}
|