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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Parts enumerated in OPC spec, MS-XLSB and MS-XLSX */
/* 12.3 Part Summary <SpreadsheetML> */
/* 14.2 Part Summary <DrawingML> */
/* [MS-XLSX] 2.1 Part Enumerations */
/* [MS-XLSB] 2.1.7 Part Enumeration */
var ct2type = {
    /* Workbook */
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml": "workbooks",
 
    /* Worksheet */
    "application/vnd.ms-excel.binIndexWs": "TODO", /* Binary Index */
 
    /* Chartsheet */
    "application/vnd.ms-excel.chartsheet": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml": "TODO",
 
    /* Dialogsheet */
    "application/vnd.ms-excel.dialogsheet": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml": "TODO",
 
    /* Macrosheet */
    "application/vnd.ms-excel.macrosheet": "TODO",
    "application/vnd.ms-excel.macrosheet+xml": "TODO",
    "application/vnd.ms-excel.intlmacrosheet": "TODO",
    "application/vnd.ms-excel.binIndexMs": "TODO", /* Binary Index */
 
    /* File Properties */
    "application/vnd.openxmlformats-package.core-properties+xml": "coreprops",
    "application/vnd.openxmlformats-officedocument.custom-properties+xml": "custprops",
    "application/vnd.openxmlformats-officedocument.extended-properties+xml": "extprops",
 
    /* Custom Data Properties */
    "application/vnd.openxmlformats-officedocument.customXmlProperties+xml": "TODO",
 
    /* Comments */
    "application/vnd.ms-excel.comments": "comments",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml": "comments",
 
    /* PivotTable */
    "application/vnd.ms-excel.pivotTable": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml": "TODO",
 
    /* Calculation Chain */
    "application/vnd.ms-excel.calcChain": "calcchains",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml": "calcchains",
 
    /* Printer Settings */
    "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings": "TODO",
 
    /* ActiveX */
    "application/vnd.ms-office.activeX": "TODO",
    "application/vnd.ms-office.activeX+xml": "TODO",
 
    /* Custom Toolbars */
    "application/vnd.ms-excel.attachedToolbars": "TODO",
 
    /* External Data Connections */
    "application/vnd.ms-excel.connections": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml": "TODO",
 
    /* External Links */
    "application/vnd.ms-excel.externalLink": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml": "TODO",
 
    /* Metadata */
    "application/vnd.ms-excel.sheetMetadata": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml": "TODO",
 
    /* PivotCache */
    "application/vnd.ms-excel.pivotCacheDefinition": "TODO",
    "application/vnd.ms-excel.pivotCacheRecords": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml": "TODO",
 
    /* Query Table */
    "application/vnd.ms-excel.queryTable": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml": "TODO",
 
    /* Shared Workbook */
    "application/vnd.ms-excel.userNames": "TODO",
    "application/vnd.ms-excel.revisionHeaders": "TODO",
    "application/vnd.ms-excel.revisionLog": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml": "TODO",
 
    /* Single Cell Table */
    "application/vnd.ms-excel.tableSingleCells": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml": "TODO",
 
    /* Slicer */
    "application/vnd.ms-excel.slicer": "TODO",
    "application/vnd.ms-excel.slicerCache": "TODO",
    "application/vnd.ms-excel.slicer+xml": "TODO",
    "application/vnd.ms-excel.slicerCache+xml": "TODO",
 
    /* Sort Map */
    "application/vnd.ms-excel.wsSortMap": "TODO",
 
    /* Table */
    "application/vnd.ms-excel.table": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml": "TODO",
 
    /* Themes */
    "application/vnd.openxmlformats-officedocument.theme+xml": "themes",
 
    /* Timeline */
    "application/vnd.ms-excel.Timeline+xml": "TODO", /* verify */
    "application/vnd.ms-excel.TimelineCache+xml": "TODO", /* verify */
 
    /* VBA */
    "application/vnd.ms-office.vbaProject": "vba",
    "application/vnd.ms-office.vbaProjectSignature": "vba",
 
    /* Volatile Dependencies */
    "application/vnd.ms-office.volatileDependencies": "TODO",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml": "TODO",
 
    /* Control Properties */
    "application/vnd.ms-excel.controlproperties+xml": "TODO",
 
    /* Data Model */
    "application/vnd.openxmlformats-officedocument.model+data": "TODO",
 
    /* Survey */
    "application/vnd.ms-excel.Survey+xml": "TODO",
 
    /* Drawing */
    "application/vnd.openxmlformats-officedocument.drawing+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.chart+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml": "TODO",
    "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml": "TODO",
 
    /* VML */
    "application/vnd.openxmlformats-officedocument.vmlDrawing": "TODO",
 
    "application/vnd.openxmlformats-package.relationships+xml": "rels",
    "application/vnd.openxmlformats-officedocument.oleObject": "TODO",
 
    "sheet": "js"
};
 
var CT_LIST = (function(){
    var o = {
        workbooks: {
            xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
            xlsm: "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
            xlsb: "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
            xltx: "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml"
        },
        strs: { /* Shared Strings */
            xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
            xlsb: "application/vnd.ms-excel.sharedStrings"
        },
        sheets: {
            xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
            xlsb: "application/vnd.ms-excel.worksheet"
        },
        styles: {/* Styles */
            xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
            xlsb: "application/vnd.ms-excel.styles"
        }
    };
    keys(o).forEach(function(k) { if(!o[k].xlsm) o[k].xlsm = o[k].xlsx; });
    keys(o).forEach(function(k){ keys(o[k]).forEach(function(v) { ct2type[o[k][v]] = k; }); });
    return o;
})();
 
var type2ct = evert_arr(ct2type);
 
XMLNS.CT = 'http://schemas.openxmlformats.org/package/2006/content-types';
 
function parse_ct(data, opts) {
    var ctext = {};
    if(!data || !data.match) return data;
    var ct = { workbooks: [], sheets: [], calcchains: [], themes: [], styles: [],
        coreprops: [], extprops: [], custprops: [], strs:[], comments: [], vba: [],
        TODO:[], rels:[], xmlns: "" };
    (data.match(tagregex)||[]).forEach(function(x) {
        var y = parsexmltag(x);
        switch(y[0].replace(nsregex,"<")) {
            case '<?xml': break;
            case '<Types': ct.xmlns = y['xmlns' + (y[0].match(/<(\w+):/)||["",""])[1] ]; break;
            case '<Default': ctext[y.Extension] = y.ContentType; break;
            case '<Override':
                if(ct[ct2type[y.ContentType]] !== undefined) ct[ct2type[y.ContentType]].push(y.PartName);
                else if(opts.WTF) console.error(y);
                break;
        }
    });
    if(ct.xmlns !== XMLNS.CT) throw new Error("Unknown Namespace: " + ct.xmlns);
    ct.calcchain = ct.calcchains.length > 0 ? ct.calcchains[0] : "";
    ct.sst = ct.strs.length > 0 ? ct.strs[0] : "";
    ct.style = ct.styles.length > 0 ? ct.styles[0] : "";
    ct.defaults = ctext;
    delete ct.calcchains;
    return ct;
}
 
var CTYPE_XML_ROOT = writextag('Types', null, {
    'xmlns': XMLNS.CT,
    'xmlns:xsd': XMLNS.xsd,
    'xmlns:xsi': XMLNS.xsi
});
 
var CTYPE_DEFAULTS = [
    ['xml', 'application/xml'],
    ['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
    ['rels', type2ct.rels[0]]
].map(function(x) {
    return writextag('Default', null, {'Extension':x[0], 'ContentType': x[1]});
});
 
function write_ct(ct, opts) {
    var o = [], v;
    o[o.length] = (XML_HEADER);
    o[o.length] = (CTYPE_XML_ROOT);
    o = o.concat(CTYPE_DEFAULTS);
    var f1 = function(w) {
        if(ct[w] && ct[w].length > 0) {
            v = ct[w][0];
            o[o.length] = (writextag('Override', null, {
                'PartName': (v[0] == '/' ? "":"/") + v,
                'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
            }));
        }
    };
    var f2 = function(w) {
        ct[w].forEach(function(v) {
            o[o.length] = (writextag('Override', null, {
                'PartName': (v[0] == '/' ? "":"/") + v,
                'ContentType': CT_LIST[w][opts.bookType || 'xlsx']
            }));
        });
    };
    var f3 = function(t) {
        (ct[t]||[]).forEach(function(v) {
            o[o.length] = (writextag('Override', null, {
                'PartName': (v[0] == '/' ? "":"/") + v,
                'ContentType': type2ct[t][0]
            }));
        });
    };
    f1('workbooks');
    f2('sheets');
    f3('themes');
    ['strs', 'styles'].forEach(f1);
    ['coreprops', 'extprops', 'custprops'].forEach(f3);
    if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
    return o.join("");
}