2014-05-16 00:33:34 +00:00
|
|
|
function add_rels(rels, rId, f, type, relobj) {
|
|
|
|
if(!relobj) relobj = {};
|
|
|
|
if(!rels['!id']) rels['!id'] = {};
|
|
|
|
relobj.Id = 'rId' + rId;
|
2014-05-16 00:50:55 +00:00
|
|
|
relobj.Type = type;
|
2014-05-16 00:33:34 +00:00
|
|
|
relobj.Target = f;
|
|
|
|
if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
|
|
|
|
rels['!id'][relobj.Id] = relobj;
|
|
|
|
rels[('/' + relobj.Target).replace("//","/")] = relobj;
|
|
|
|
}
|
|
|
|
|
|
|
|
function write_zip(wb, opts) {
|
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);
|
2014-06-29 18:29:45 +00:00
|
|
|
opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
|
2014-05-16 00:33:34 +00:00
|
|
|
}
|
|
|
|
opts.rels = {}; opts.wbrels = {};
|
|
|
|
opts.Strings = []; opts.Strings.Count = 0; opts.Strings.Unique = 0;
|
|
|
|
var wbext = opts.bookType == "xlsb" ? "bin" : "xml";
|
|
|
|
var ct = { workbooks: [], sheets: [], calcchains: [], themes: [], styles: [],
|
|
|
|
coreprops: [], extprops: [], custprops: [], strs:[], comments: [], vba: [],
|
|
|
|
TODO:[], rels:[], xmlns: "" };
|
|
|
|
fix_write_opts(opts = opts || {});
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
f = "docProps/app.xml";
|
2014-05-22 12:16:51 +00:00
|
|
|
if(!wb.Props) wb.Props = {};
|
2014-05-16 00:33:34 +00:00
|
|
|
wb.Props.SheetNames = wb.SheetNames;
|
|
|
|
wb.Props.Worksheets = wb.SheetNames.length;
|
|
|
|
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) {
|
|
|
|
f = "xl/worksheets/sheet" + rId + "." + wbext;
|
|
|
|
zip.file(f, write_ws(rId-1, f, opts, wb));
|
2014-05-16 00:33:34 +00:00
|
|
|
ct.sheets.push(f);
|
|
|
|
add_rels(opts.wbrels, rId, "worksheets/sheet" + rId + "." + wbext, RELS.WS);
|
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);
|
|
|
|
add_rels(opts.wbrels, ++rId, "sharedStrings." + wbext, RELS.SST);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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";
|
2014-05-16 00:33:34 +00:00
|
|
|
zip.file(f, write_theme());
|
|
|
|
ct.themes.push(f);
|
2014-05-22 12:16:51 +00:00
|
|
|
add_rels(opts.wbrels, ++rId, "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);
|
|
|
|
add_rels(opts.wbrels, ++rId, "styles." + wbext, RELS.STY);
|
|
|
|
|
|
|
|
zip.file("[Content_Types].xml", write_ct(ct, opts));
|
|
|
|
zip.file('_rels/.rels', write_rels(opts.rels));
|
2014-05-29 22:30:03 +00:00
|
|
|
zip.file('xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
|
2014-05-16 00:33:34 +00:00
|
|
|
return zip;
|
|
|
|
}
|