2017-03-12 18:02:43 +00:00
|
|
|
function write_zip(wb/*:Workbook*/, opts/*:WriteOpts*/)/*:ZIP*/ {
|
2017-04-02 06:47:25 +00:00
|
|
|
_shapeid = 1024;
|
2017-02-03 20:50:45 +00:00
|
|
|
if(opts.bookType == "ods") return write_ods(wb, opts);
|
2014-05-22 12:16:51 +00:00
|
|
|
if(wb && !wb.SSF) {
|
|
|
|
wb.SSF = SSF.get_table();
|
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
if(wb && wb.SSF) {
|
|
|
|
make_ssf(SSF); SSF.load_table(wb.SSF);
|
2017-03-12 18:02:43 +00:00
|
|
|
// $FlowIgnore
|
2014-06-29 18:29:45 +00:00
|
|
|
opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
|
2017-05-09 18:07:57 +00:00
|
|
|
opts.ssf = wb.SSF;
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
opts.rels = {}; opts.wbrels = {};
|
2017-03-12 18:02:43 +00:00
|
|
|
opts.Strings = /*::((*/[]/*:: :any):SST)*/; opts.Strings.Count = 0; opts.Strings.Unique = 0;
|
2014-05-16 00:33:34 +00:00
|
|
|
var wbext = opts.bookType == "xlsb" ? "bin" : "xml";
|
2017-03-28 04:41:01 +00:00
|
|
|
var vbafmt = opts.bookType == "xlsb" || opts.bookType == "xlsm";
|
2017-04-08 06:55:35 +00:00
|
|
|
var ct = ({
|
|
|
|
workbooks:[], sheets:[], charts:[], dialogs:[], macros:[],
|
|
|
|
rels:[], strs:[], comments:[],
|
|
|
|
coreprops:[], extprops:[], custprops:[], themes:[], styles:[],
|
|
|
|
calcchains:[], vba: [], drawings: [],
|
|
|
|
TODO:[], xmlns: "" }/*:any*/);
|
2014-05-16 00:33:34 +00:00
|
|
|
fix_write_opts(opts = opts || {});
|
2017-02-10 19:23:01 +00:00
|
|
|
/*:: if(!jszip) throw new Error("JSZip is not available"); */
|
2014-05-16 00:33:34 +00:00
|
|
|
var zip = new jszip();
|
|
|
|
var f = "", rId = 0;
|
|
|
|
|
|
|
|
opts.cellXfs = [];
|
2014-05-22 12:16:51 +00:00
|
|
|
get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
if(!wb.Props) wb.Props = {};
|
|
|
|
|
2014-05-16 00:33:34 +00:00
|
|
|
f = "docProps/core.xml";
|
|
|
|
zip.file(f, write_core_props(wb.Props, opts));
|
|
|
|
ct.coreprops.push(f);
|
2014-05-22 12:16:51 +00:00
|
|
|
add_rels(opts.rels, 2, f, RELS.CORE_PROPS);
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2017-03-12 18:02:43 +00:00
|
|
|
/*::if(!wb.Props) throw "unreachable"; */
|
2014-05-16 00:33:34 +00:00
|
|
|
f = "docProps/app.xml";
|
2017-05-09 18:07:57 +00:00
|
|
|
if(wb.Props && wb.Props.SheetNames){/* empty */}
|
2017-04-03 00:16:03 +00:00
|
|
|
else if(!wb.Workbook || !wb.Workbook.Sheets) wb.Props.SheetNames = wb.SheetNames;
|
2017-04-01 07:32:12 +00:00
|
|
|
// $FlowIgnore
|
2017-04-03 00:16:03 +00:00
|
|
|
else wb.Props.SheetNames = wb.SheetNames.map(function(x,i) { return [(wb.Workbook.Sheets[i]||{}).Hidden != 2, x];}).filter(function(x) { return x[0]; }).map(function(x) { return x[1]; });
|
2017-03-31 18:46:42 +00:00
|
|
|
wb.Props.Worksheets = wb.Props.SheetNames.length;
|
2014-05-16 00:33:34 +00:00
|
|
|
zip.file(f, write_ext_props(wb.Props, opts));
|
|
|
|
ct.extprops.push(f);
|
2014-05-22 12:16:51 +00:00
|
|
|
add_rels(opts.rels, 3, f, RELS.EXT_PROPS);
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
if(wb.Custprops !== wb.Props && keys(wb.Custprops||{}).length > 0) {
|
2014-05-16 00:33:34 +00:00
|
|
|
f = "docProps/custom.xml";
|
|
|
|
zip.file(f, write_cust_props(wb.Custprops, opts));
|
|
|
|
ct.custprops.push(f);
|
2014-05-22 12:16:51 +00:00
|
|
|
add_rels(opts.rels, 4, f, RELS.CUST_PROPS);
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f = "xl/workbook." + wbext;
|
|
|
|
zip.file(f, write_wb(wb, f, opts));
|
|
|
|
ct.workbooks.push(f);
|
|
|
|
add_rels(opts.rels, 1, f, RELS.WB);
|
|
|
|
|
2014-06-29 18:29:45 +00:00
|
|
|
for(rId=1;rId <= wb.SheetNames.length; ++rId) {
|
2017-03-31 00:47:35 +00:00
|
|
|
var wsrels = {'!id':{}};
|
2017-04-02 06:47:25 +00:00
|
|
|
var ws = wb.Sheets[wb.SheetNames[rId-1]];
|
2017-04-08 06:55:35 +00:00
|
|
|
var _type = (ws || {})["!type"] || "sheet";
|
|
|
|
switch(_type) {
|
|
|
|
case "chart": /*
|
|
|
|
f = "xl/chartsheets/sheet" + rId + "." + wbext;
|
|
|
|
zip.file(f, write_cs(rId-1, f, opts, wb, wsrels));
|
|
|
|
ct.charts.push(f);
|
|
|
|
add_rels(wsrels, -1, "chartsheets/sheet" + rId + "." + wbext, RELS.CS);
|
|
|
|
break; */
|
|
|
|
/* falls through */
|
|
|
|
default:
|
|
|
|
f = "xl/worksheets/sheet" + rId + "." + wbext;
|
|
|
|
zip.file(f, write_ws(rId-1, f, opts, wb, wsrels));
|
|
|
|
ct.sheets.push(f);
|
|
|
|
add_rels(opts.wbrels, -1, "worksheets/sheet" + rId + "." + wbext, RELS.WS[0]);
|
|
|
|
}
|
|
|
|
|
2017-04-02 06:47:25 +00:00
|
|
|
if(ws) {
|
|
|
|
var comments = ws['!comments'];
|
|
|
|
if(comments && comments.length > 0) {
|
|
|
|
var cf = "xl/comments" + rId + "." + wbext;
|
|
|
|
zip.file(cf, write_cmnt(comments, cf, opts));
|
|
|
|
ct.comments.push(cf);
|
|
|
|
add_rels(wsrels, -1, "../comments" + rId + "." + wbext, RELS.CMNT);
|
|
|
|
}
|
|
|
|
if(ws['!legacy']) {
|
|
|
|
zip.file("xl/drawings/vmlDrawing" + (rId) + ".vml", write_comments_vml(rId, ws['!comments']));
|
|
|
|
}
|
|
|
|
delete ws['!comments'];
|
|
|
|
delete ws['!legacy'];
|
|
|
|
}
|
|
|
|
|
2017-04-08 06:55:35 +00:00
|
|
|
if(wsrels['!id'].rId1) zip.file(get_rels_path(f), write_rels(wsrels));
|
2014-06-29 18:29:45 +00:00
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2014-06-29 18:29:45 +00:00
|
|
|
if(opts.Strings != null && opts.Strings.length > 0) {
|
2014-05-16 00:33:34 +00:00
|
|
|
f = "xl/sharedStrings." + wbext;
|
|
|
|
zip.file(f, write_sst(opts.Strings, f, opts));
|
|
|
|
ct.strs.push(f);
|
2017-04-02 06:47:25 +00:00
|
|
|
add_rels(opts.wbrels, -1, "sharedStrings." + wbext, RELS.SST);
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: something more intelligent with themes */
|
2014-05-16 00:50:55 +00:00
|
|
|
|
2014-05-22 12:16:51 +00:00
|
|
|
f = "xl/theme/theme1.xml";
|
2017-03-18 23:25:50 +00:00
|
|
|
zip.file(f, write_theme(wb.Themes, opts));
|
2014-05-16 00:33:34 +00:00
|
|
|
ct.themes.push(f);
|
2017-04-02 06:47:25 +00:00
|
|
|
add_rels(opts.wbrels, -1, "theme/theme1.xml", RELS.THEME);
|
2014-05-16 00:33:34 +00:00
|
|
|
|
|
|
|
/* TODO: something more intelligent with styles */
|
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
f = "xl/styles." + wbext;
|
2014-05-16 00:33:34 +00:00
|
|
|
zip.file(f, write_sty(wb, f, opts));
|
|
|
|
ct.styles.push(f);
|
2017-04-02 06:47:25 +00:00
|
|
|
add_rels(opts.wbrels, -1, "styles." + wbext, RELS.STY);
|
2014-05-16 00:33:34 +00:00
|
|
|
|
2017-03-28 04:41:01 +00:00
|
|
|
if(wb.vbaraw && vbafmt) {
|
|
|
|
f = "xl/vbaProject.bin";
|
|
|
|
zip.file(f, wb.vbaraw);
|
|
|
|
ct.vba.push(f);
|
2017-04-02 06:47:25 +00:00
|
|
|
add_rels(opts.wbrels, -1, "vbaProject.bin", RELS.VBA);
|
2017-03-28 04:41:01 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 00:33:34 +00:00
|
|
|
zip.file("[Content_Types].xml", write_ct(ct, opts));
|
2017-04-08 06:55:35 +00:00
|
|
|
zip.file('_rels/.rels', write_rels(opts.rels));
|
|
|
|
zip.file('xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
|
2017-05-09 18:07:57 +00:00
|
|
|
|
|
|
|
delete opts.revssf; delete opts.ssf;
|
2014-05-16 00:33:34 +00:00
|
|
|
return zip;
|
|
|
|
}
|