2017-02-10 19:23:01 +00:00
|
|
|
function write_zip_type(wb/*:Workbook*/, opts/*:?WriteOpts*/) {
|
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;
|
|
|
|
case "buffer":
|
|
|
|
case "file": oopts.type = "nodebuffer"; break;
|
2014-05-16 00:33:34 +00:00
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
2017-02-03 20:50:45 +00:00
|
|
|
if(o.type === "file") return _fs.writeFileSync(o.file, z.generate(oopts));
|
|
|
|
return z.generate(oopts);
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 00:16:03 +00:00
|
|
|
/* TODO: test consistency */
|
|
|
|
function write_bstr_type(out/*:string*/, opts/*:WriteOpts*/) {
|
|
|
|
switch(opts.type) {
|
|
|
|
case "base64": return Base64.encode(out);
|
|
|
|
case "binary": return out;
|
|
|
|
case "file": return _fs.writeFileSync(opts.file, out, 'binary');
|
|
|
|
case "buffer": {
|
|
|
|
if(has_buf) return new Buffer(out, 'utf8');
|
|
|
|
else return out.split("").map(function(c) { return c.charCodeAt(0); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error("Unrecognized type " + opts.type);
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:33:01 +00:00
|
|
|
/* TODO: test consistency */
|
2017-02-22 06:57:59 +00:00
|
|
|
function write_string_type(out/*:string*/, opts/*:WriteOpts*/) {
|
|
|
|
switch(opts.type) {
|
2017-02-24 10:33:01 +00:00
|
|
|
case "base64": return Base64.encode(out);
|
|
|
|
case "binary": return out;
|
2017-03-09 05:24:32 +00:00
|
|
|
case "file": return _fs.writeFileSync(opts.file, out, 'utf8');
|
2017-02-24 10:33:01 +00:00
|
|
|
case "buffer": {
|
|
|
|
if(has_buf) return new Buffer(out, 'utf8');
|
|
|
|
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-02-22 06:57:59 +00:00
|
|
|
function write_binary_type(out, opts/*:WriteOpts*/) {
|
|
|
|
switch(opts.type) {
|
2017-02-24 10:33:01 +00:00
|
|
|
case "base64":
|
2017-02-22 06:57:59 +00:00
|
|
|
case "binary":
|
|
|
|
var bstr = "";
|
|
|
|
for(var i = 0; i < out.length; ++i) bstr += String.fromCharCode(out[i]);
|
2017-02-24 10:33:01 +00:00
|
|
|
return opts.type == 'base64' ? Base64.encode(bstr) : bstr;
|
2017-02-22 06:57:59 +00:00
|
|
|
case "file": return _fs.writeFileSync(opts.file, out);
|
|
|
|
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-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);
|
2017-04-03 00:16:03 +00:00
|
|
|
case 'txt': return write_bstr_type(write_txt_str(wb, o), o);
|
2017-02-22 06:57:59 +00:00
|
|
|
case 'csv': return write_string_type(write_csv_str(wb, o), o);
|
2017-04-01 07:32:12 +00:00
|
|
|
case 'dif': return write_string_type(write_dif_str(wb, o), o);
|
2017-04-03 00:16:03 +00:00
|
|
|
case 'prn': return write_string_type(write_prn_str(wb, o), o);
|
2017-02-22 06:57:59 +00:00
|
|
|
case 'fods': return write_string_type(write_ods(wb, o), o);
|
|
|
|
case 'biff2': return write_binary_type(write_biff_buf(wb, o), o);
|
2017-02-24 10:33:01 +00:00
|
|
|
case 'xlsx':
|
|
|
|
case 'xlsm':
|
|
|
|
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-03-25 01:36:40 +00:00
|
|
|
function resolve_book_type(o/*?WriteFileOpts*/) {
|
2017-04-02 06:47:25 +00:00
|
|
|
if(!o.bookType) switch(o.file.slice(o.file.lastIndexOf(".")).toLowerCase()) {
|
2015-04-02 20:32:22 +00:00
|
|
|
case '.xlsx': o.bookType = 'xlsx'; break;
|
2014-05-16 00:33:34 +00:00
|
|
|
case '.xlsm': o.bookType = 'xlsm'; break;
|
|
|
|
case '.xlsb': o.bookType = 'xlsb'; break;
|
2017-02-22 06:57:59 +00:00
|
|
|
case '.fods': o.bookType = 'fods'; break;
|
2017-03-14 08:19:51 +00:00
|
|
|
case '.xlml': o.bookType = 'xlml'; break;
|
2017-04-01 07:32:12 +00:00
|
|
|
case '.sylk': o.bookType = 'sylk'; break;
|
2017-02-10 19:23:01 +00:00
|
|
|
case '.xls': o.bookType = 'biff2'; break;
|
2015-04-02 20:32:22 +00:00
|
|
|
case '.xml': o.bookType = 'xml'; break;
|
2017-02-03 20:50:45 +00:00
|
|
|
case '.ods': o.bookType = 'ods'; break;
|
2017-02-22 06:57:59 +00:00
|
|
|
case '.csv': o.bookType = 'csv'; break;
|
2017-04-03 00:16:03 +00:00
|
|
|
case '.txt': o.bookType = 'txt'; break;
|
2017-04-01 07:32:12 +00:00
|
|
|
case '.dif': o.bookType = 'dif'; break;
|
2017-04-03 00:16:03 +00:00
|
|
|
case '.prn': o.bookType = 'prn'; break;
|
2017-04-01 07:32:12 +00:00
|
|
|
case '.slk': o.bookType = 'sylk'; break;
|
2017-04-02 06:47:25 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|