ludc
2023-08-24 56c45e1f4be85d6bbfb3a03437021c6742b32ad9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function add_rels(rels, rId, f, type, relobj) {
    if(!relobj) relobj = {};
    if(!rels['!id']) rels['!id'] = {};
    relobj.Id = 'rId' + rId;
    relobj.Type = type;
    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) {
    if(wb && !wb.SSF) {
        wb.SSF = SSF.get_table();
    }
    if(wb && wb.SSF) {
        make_ssf(SSF); SSF.load_table(wb.SSF);
        opts.revssf = evert_num(wb.SSF); opts.revssf[wb.SSF[65535]] = 0;
    }
    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 = [];
    get_cell_style(opts.cellXfs, {}, {revssf:{"General":0}});
 
    f = "docProps/core.xml";
    zip.file(f, write_core_props(wb.Props, opts));
    ct.coreprops.push(f);
    add_rels(opts.rels, 2, f, RELS.CORE_PROPS);
 
    f = "docProps/app.xml";
    if(!wb.Props) wb.Props = {};
    wb.Props.SheetNames = wb.SheetNames;
    wb.Props.Worksheets = wb.SheetNames.length;
    zip.file(f, write_ext_props(wb.Props, opts));
    ct.extprops.push(f);
    add_rels(opts.rels, 3, f, RELS.EXT_PROPS);
 
    if(wb.Custprops !== wb.Props && keys(wb.Custprops||{}).length > 0) {
        f = "docProps/custom.xml";
        zip.file(f, write_cust_props(wb.Custprops, opts));
        ct.custprops.push(f);
        add_rels(opts.rels, 4, f, RELS.CUST_PROPS);
    }
 
    f = "xl/workbook." + wbext;
    zip.file(f, write_wb(wb, f, opts));
    ct.workbooks.push(f);
    add_rels(opts.rels, 1, f, RELS.WB);
 
    for(rId=1;rId <= wb.SheetNames.length; ++rId) {
        f = "xl/worksheets/sheet" + rId + "." + wbext;
        zip.file(f, write_ws(rId-1, f, opts, wb));
        ct.sheets.push(f);
        add_rels(opts.wbrels, rId, "worksheets/sheet" + rId + "." + wbext, RELS.WS);
    }
 
    if(opts.Strings != null && opts.Strings.length > 0) {
        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 */
 
    f = "xl/theme/theme1.xml";
  zip.file(f, write_theme(opts));
    ct.themes.push(f);
    add_rels(opts.wbrels, ++rId, "theme/theme1.xml", RELS.THEME);
 
    /* TODO: something more intelligent with styles */
 
    f = "xl/styles." + wbext;
    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));
    zip.file('xl/_rels/workbook.' + wbext + '.rels', write_rels(opts.wbrels));
    return zip;
}