2018-06-22 21:40:01 +00:00
|
|
|
function write_cfb_ctr(cfb/*:CFBContainer*/, o/*:WriteOpts*/)/*:any*/ {
|
|
|
|
switch(o.type) {
|
|
|
|
case "base64": case "binary": break;
|
|
|
|
case "buffer": case "array": o.type = ""; break;
|
|
|
|
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");
|
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
|
|
|
return CFB.write(cfb, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*:: declare var encrypt_agile:any; */
|
2017-09-30 06:18:11 +00:00
|
|
|
function write_zip_type(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
|
2021-10-13 07:20:25 +00:00
|
|
|
var o = dup(opts||{});
|
2014-05-16 00:33:34 +00:00
|
|
|
var z = write_zip(wb, o);
|
2022-02-14 01:28:13 +00:00
|
|
|
return write_zip_denouement(z, o);
|
|
|
|
}
|
|
|
|
function write_zip_typeXLSX(wb/*:Workbook*/, opts/*:?WriteOpts*/)/*:any*/ {
|
|
|
|
var o = dup(opts||{});
|
|
|
|
var z = write_zip_xlsx(wb, o);
|
|
|
|
return write_zip_denouement(z, o);
|
|
|
|
}
|
|
|
|
function write_zip_denouement(z/*:any*/, o/*:?WriteOpts*/)/*:any*/ {
|
2017-02-03 20:50:45 +00:00
|
|
|
var oopts = {};
|
2022-03-03 08:35:39 +00:00
|
|
|
var ftype = has_buf ? "nodebuffer" : (typeof Uint8Array !== "undefined" ? "array" : "string");
|
2017-02-10 19:23:01 +00:00
|
|
|
if(o.compression) oopts.compression = 'DEFLATE';
|
2022-03-03 08:35:39 +00:00
|
|
|
if(o.password) oopts.type = ftype;
|
2018-06-22 21:40:01 +00:00
|
|
|
else 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":
|
2022-03-03 08:35:39 +00:00
|
|
|
case "file": oopts.type = ftype; break;
|
2014-05-16 00:33:34 +00:00
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
|
|
|
}
|
2022-02-12 23:26:50 +00:00
|
|
|
var out = z.FullPaths ? CFB.write(z, {fileType:"zip", type: /*::(*/{"nodebuffer": "buffer", "string": "binary"}/*:: :any)*/[oopts.type] || oopts.type, compression: !!o.compression}) : z.generate(oopts);
|
2022-02-13 09:35:34 +00:00
|
|
|
if(typeof Deno !== "undefined") {
|
|
|
|
if(typeof out == "string") {
|
|
|
|
if(o.type == "binary" || o.type == "base64") return out;
|
|
|
|
out = new Uint8Array(s2ab(out));
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 07:42:05 +00:00
|
|
|
/*jshint -W083 */
|
|
|
|
if(o.password && typeof encrypt_agile !== 'undefined') return write_cfb_ctr(encrypt_agile(out, o.password), o); // eslint-disable-line no-undef
|
|
|
|
/*jshint +W083 */
|
2018-06-22 21:40:01 +00:00
|
|
|
if(o.type === "file") return write_dl(o.file, out);
|
2019-11-01 03:09:14 +00:00
|
|
|
return o.type == "string" ? utf8read(/*::(*/out/*:: :any)*/) : 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);
|
2018-06-22 21:40:01 +00:00
|
|
|
return write_cfb_ctr(cfb, o);
|
2017-09-22 22:18:51 +00:00
|
|
|
}
|
|
|
|
|
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-07-09 03:46:11 +00:00
|
|
|
if(has_buf) return Buffer_from(o, 'utf8');
|
2022-02-13 09:35:34 +00:00
|
|
|
else if(typeof TextEncoder !== "undefined") return new TextEncoder().encode(o);
|
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-07-09 03:46:11 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 01:28:13 +00:00
|
|
|
function writeSyncXLSX(wb/*:Workbook*/, opts/*:?WriteOpts*/) {
|
|
|
|
reset_cp();
|
|
|
|
check_wb(wb);
|
|
|
|
var o = dup(opts||{});
|
|
|
|
if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
|
|
|
|
if(o.type == "array") { o.type = "binary"; var out/*:string*/ = (writeSyncXLSX(wb, o)/*:any*/); o.type = "array"; return s2ab(out); }
|
|
|
|
return write_zip_typeXLSX(wb, o);
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function writeSync(wb/*:Workbook*/, opts/*:?WriteOpts*/) {
|
2019-11-01 03:09:14 +00:00
|
|
|
reset_cp();
|
2017-03-10 08:39:51 +00:00
|
|
|
check_wb(wb);
|
2021-10-13 07:20:25 +00:00
|
|
|
var o = dup(opts||{});
|
2020-03-15 07:42:05 +00:00
|
|
|
if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; }
|
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); }
|
2022-03-12 14:05:57 +00:00
|
|
|
var idx = 0;
|
|
|
|
if(o.sheet) {
|
|
|
|
if(typeof o.sheet == "number") idx = o.sheet;
|
|
|
|
else idx = wb.SheetNames.indexOf(o.sheet);
|
|
|
|
if(!wb.SheetNames[idx]) throw new Error("Sheet not found: " + o.sheet + " : " + (typeof o.sheet));
|
|
|
|
}
|
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':
|
2022-03-12 14:05:57 +00:00
|
|
|
case 'sylk': return write_string_type(SYLK.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
2018-02-08 18:21:39 +00:00
|
|
|
case 'htm':
|
2022-03-12 14:05:57 +00:00
|
|
|
case 'html': return write_string_type(HTML_.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'txt': return write_stxt_type(sheet_to_txt(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'csv': return write_string_type(sheet_to_csv(wb.Sheets[wb.SheetNames[idx]], o), o, "\ufeff");
|
|
|
|
case 'dif': return write_string_type(DIF.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'dbf': return write_binary_type(DBF.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'prn': return write_string_type(PRN.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'rtf': return write_string_type(RTF.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
|
|
|
case 'eth': return write_string_type(ETH.from_sheet(wb.Sheets[wb.SheetNames[idx]], o), o);
|
2017-02-22 06:57:59 +00:00
|
|
|
case 'fods': return write_string_type(write_ods(wb, o), o);
|
2022-03-12 14:05:57 +00:00
|
|
|
case 'wk1': return write_binary_type(WK_.sheet_to_wk1(wb.Sheets[wb.SheetNames[idx]], o), o);
|
2021-11-14 04:38:00 +00:00
|
|
|
case 'wk3': return write_binary_type(WK_.book_to_wk3(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);
|
|
|
|
}
|
|
|
|
|
2022-02-14 01:28:13 +00:00
|
|
|
function writeFileSyncXLSX(wb/*:Workbook*/, filename/*:string*/, opts/*:?WriteFileOpts*/) {
|
|
|
|
var o = opts||{}; o.type = 'file';
|
|
|
|
o.file = filename;
|
|
|
|
resolve_book_type(o);
|
|
|
|
return writeSyncXLSX(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);
|
|
|
|
}
|