deno.land / x / sheetjs@v0.18.3 / bits / 85_parsezip.js

85_parsezip.js
نووسراو ببینە
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
function get_sheet_type(n/*:string*/)/*:string*/ { if(RELS.WS.indexOf(n) > -1) return "sheet"; if(RELS.CS && n == RELS.CS) return "chart"; if(RELS.DS && n == RELS.DS) return "dialog"; if(RELS.MS && n == RELS.MS) return "macro"; return (n && n.length) ? n : "sheet";}function safe_parse_wbrels(wbrels, sheets) { if(!wbrels) return 0; try { wbrels = sheets.map(function pwbr(w) { if(!w.id) w.id = w.strRelID; return [w.name, wbrels['!id'][w.id].Target, get_sheet_type(wbrels['!id'][w.id].Type)]; }); } catch(e) { return null; } return !wbrels || wbrels.length === 0 ? null : wbrels;}
function safe_parse_sheet(zip, path/*:string*/, relsPath/*:string*/, sheet, idx/*:number*/, sheetRels, sheets, stype/*:string*/, opts, wb, themes, styles) { try { sheetRels[sheet]=parse_rels(getzipstr(zip, relsPath, true), path); var data = getzipdata(zip, path); var _ws; switch(stype) { case 'sheet': _ws = parse_ws(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break; case 'chart': _ws = parse_cs(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); if(!_ws || !_ws['!drawel']) break; var dfile = resolve_path(_ws['!drawel'].Target, path); var drelsp = get_rels_path(dfile); var draw = parse_drawing(getzipstr(zip, dfile, true), parse_rels(getzipstr(zip, drelsp, true), dfile)); var chartp = resolve_path(draw, dfile); var crelsp = get_rels_path(chartp); _ws = parse_chart(getzipstr(zip, chartp, true), chartp, opts, parse_rels(getzipstr(zip, crelsp, true), chartp), wb, _ws); break; case 'macro': _ws = parse_ms(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break; case 'dialog': _ws = parse_ds(data, path, idx, opts, sheetRels[sheet], wb, themes, styles); break; default: throw new Error("Unrecognized sheet type " + stype); } sheets[sheet] = _ws;
/* scan rels for comments */ var comments = []; if(sheetRels && sheetRels[sheet]) keys(sheetRels[sheet]).forEach(function(n) { if(sheetRels[sheet][n].Type == RELS.CMNT) { var dfile = resolve_path(sheetRels[sheet][n].Target, path); comments = parse_cmnt(getzipdata(zip, dfile, true), dfile, opts); if(!comments || !comments.length) return; sheet_insert_comments(_ws, comments); } }); } catch(e) { if(opts.WTF) throw e; }}
function strip_front_slash(x/*:string*/)/*:string*/ { return x.charAt(0) == '/' ? x.slice(1) : x; }
function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ { make_ssf(SSF); opts = opts || {}; fix_read_opts(opts);
/* OpenDocument Part 3 Section 2.2.1 OpenDocument Package */ if(safegetzipfile(zip, 'META-INF/manifest.xml')) return parse_ods(zip, opts); /* UOC */ if(safegetzipfile(zip, 'objectdata.xml')) return parse_ods(zip, opts); /* Numbers */ if(safegetzipfile(zip, 'Index/Document.iwa')) { if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support'); if(typeof NUMBERS != "undefined") { if(zip.FileIndex) return NUMBERS.parse_numbers(zip); var _zip = CFB.utils.cfb_new(); zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); }); return NUMBERS.parse_numbers(_zip); } throw new Error('Unsupported NUMBERS file'); } if(!safegetzipfile(zip, '[Content_Types].xml')) { if(safegetzipfile(zip, 'index.xml.gz')) throw new Error('Unsupported NUMBERS 08 file'); if(safegetzipfile(zip, 'index.xml')) throw new Error('Unsupported NUMBERS 09 file'); throw new Error('Unsupported ZIP file'); }
var entries = zipentries(zip); var dir = parse_ct((getzipstr(zip, '[Content_Types].xml')/*:?any*/)); var xlsb = false; var sheets, binname; if(dir.workbooks.length === 0) { binname = "xl/workbook.xml"; if(getzipdata(zip,binname, true)) dir.workbooks.push(binname); } if(dir.workbooks.length === 0) { binname = "xl/workbook.bin"; if(!getzipdata(zip,binname,true)) throw new Error("Could not find workbook"); dir.workbooks.push(binname); xlsb = true; } if(dir.workbooks[0].slice(-3) == "bin") xlsb = true;
var themes = ({}/*:any*/); var styles = ({}/*:any*/); if(!opts.bookSheets && !opts.bookProps) { strs = []; if(dir.sst) try { strs=parse_sst(getzipdata(zip, strip_front_slash(dir.sst)), dir.sst, opts); } catch(e) { if(opts.WTF) throw e; }
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipstr(zip, dir.themes[0].replace(/^\//,''), true)||"",dir.themes[0], opts);
if(dir.style) styles = parse_sty(getzipdata(zip, strip_front_slash(dir.style)), dir.style, themes, opts); }
/*var externbooks = */dir.links.map(function(link) { try { var rels = parse_rels(getzipstr(zip, get_rels_path(strip_front_slash(link))), link); return parse_xlink(getzipdata(zip, strip_front_slash(link)), rels, link, opts); } catch(e) {} });
var wb = parse_wb(getzipdata(zip, strip_front_slash(dir.workbooks[0])), dir.workbooks[0], opts);
var props = {}, propdata = "";
if(dir.coreprops.length) { propdata = getzipdata(zip, strip_front_slash(dir.coreprops[0]), true); if(propdata) props = parse_core_props(propdata); if(dir.extprops.length !== 0) { propdata = getzipdata(zip, strip_front_slash(dir.extprops[0]), true); if(propdata) parse_ext_props(propdata, props, opts); } }
var custprops = {}; if(!opts.bookSheets || opts.bookProps) { if (dir.custprops.length !== 0) { propdata = getzipstr(zip, strip_front_slash(dir.custprops[0]), true); if(propdata) custprops = parse_cust_props(propdata, opts); } }
var out = ({}/*:any*/); if(opts.bookSheets || opts.bookProps) { if(wb.Sheets) sheets = wb.Sheets.map(function pluck(x){ return x.name; }); else if(props.Worksheets && props.SheetNames.length > 0) sheets=props.SheetNames; if(opts.bookProps) { out.Props = props; out.Custprops = custprops; } if(opts.bookSheets && typeof sheets !== 'undefined') out.SheetNames = sheets; if(opts.bookSheets ? out.SheetNames : opts.bookProps) return out; } sheets = {};
var deps = {}; if(opts.bookDeps && dir.calcchain) deps=parse_cc(getzipdata(zip, strip_front_slash(dir.calcchain)),dir.calcchain,opts);
var i=0; var sheetRels = ({}/*:any*/); var path, relsPath;
{ var wbsheets = wb.Sheets; props.Worksheets = wbsheets.length; props.SheetNames = []; for(var j = 0; j != wbsheets.length; ++j) { props.SheetNames[j] = wbsheets[j].name; } }
var wbext = xlsb ? "bin" : "xml"; var wbrelsi = dir.workbooks[0].lastIndexOf("/"); var wbrelsfile = (dir.workbooks[0].slice(0, wbrelsi+1) + "_rels/" + dir.workbooks[0].slice(wbrelsi+1) + ".rels").replace(/^\//,""); if(!safegetzipfile(zip, wbrelsfile)) wbrelsfile = 'xl/_rels/workbook.' + wbext + '.rels'; var wbrels = parse_rels(getzipstr(zip, wbrelsfile, true), wbrelsfile.replace(/_rels.*/, "s5s"));
if((dir.metadata || []).length >= 1) { /* TODO: MDX and other types of metadata */ opts.xlmeta = parse_xlmeta(getzipdata(zip, strip_front_slash(dir.metadata[0])),dir.metadata[0],opts); }
if(wbrels) wbrels = safe_parse_wbrels(wbrels, wb.Sheets);

/* Numbers iOS hack */ var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0; wsloop: for(i = 0; i != props.Worksheets; ++i) { var stype = "sheet"; if(wbrels && wbrels[i]) { path = 'xl/' + (wbrels[i][1]).replace(/[\/]?xl\//, ""); if(!safegetzipfile(zip, path)) path = wbrels[i][1]; if(!safegetzipfile(zip, path)) path = wbrelsfile.replace(/_rels\/.*$/,"") + wbrels[i][1]; stype = wbrels[i][2]; } else { path = 'xl/worksheets/sheet'+(i+1-nmode)+"." + wbext; path = path.replace(/sheet0\./,"sheet."); } relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels"); if(opts && opts.sheets != null) switch(typeof opts.sheets) { case "number": if(i != opts.sheets) continue wsloop; break; case "string": if(props.SheetNames[i].toLowerCase() != opts.sheets.toLowerCase()) continue wsloop; break; default: if(Array.isArray && Array.isArray(opts.sheets)) { var snjseen = false; for(var snj = 0; snj != opts.sheets.length; ++snj) { if(typeof opts.sheets[snj] == "number" && opts.sheets[snj] == i) snjseen=1; if(typeof opts.sheets[snj] == "string" && opts.sheets[snj].toLowerCase() == props.SheetNames[i].toLowerCase()) snjseen = 1; } if(!snjseen) continue wsloop; } } safe_parse_sheet(zip, path, relsPath, props.SheetNames[i], i, sheetRels, sheets, stype, opts, wb, themes, styles); }
out = ({ Directory: dir, Workbook: wb, Props: props, Custprops: custprops, Deps: deps, Sheets: sheets, SheetNames: props.SheetNames, Strings: strs, Styles: styles, Themes: themes, SSF: SSF.get_table() }/*:any*/); if(opts && opts.bookFiles) { if(zip.files) { out.keys = entries; out.files = zip.files; } else { out.keys = []; out.files = {}; zip.FullPaths.forEach(function(p, idx) { p = p.replace(/^Root Entry[\/]/, ""); out.keys.push(p); out.files[p] = zip.FileIndex[idx]; }); } } if(opts && opts.bookVBA) { if(dir.vba.length > 0) out.vbaraw = getzipdata(zip,strip_front_slash(dir.vba[0]),true); else if(dir.defaults && dir.defaults.bin === CT_VBA) out.vbaraw = getzipdata(zip, 'xl/vbaProject.bin',true); } return out;}
/* [MS-OFFCRYPTO] 2.1.1 */function parse_xlsxcfb(cfb, _opts/*:?ParseOpts*/)/*:Workbook*/ { var opts = _opts || {}; var f = 'Workbook', data = CFB.find(cfb, f); try { f = '/!DataSpaces/Version'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f); /*var version = */parse_DataSpaceVersionInfo(data.content);
/* 2.3.4.1 */ f = '/!DataSpaces/DataSpaceMap'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f); var dsm = parse_DataSpaceMap(data.content); if(dsm.length !== 1 || dsm[0].comps.length !== 1 || dsm[0].comps[0].t !== 0 || dsm[0].name !== "StrongEncryptionDataSpace" || dsm[0].comps[0].v !== "EncryptedPackage") throw new Error("ECMA-376 Encrypted file bad " + f);
/* 2.3.4.2 */ f = '/!DataSpaces/DataSpaceInfo/StrongEncryptionDataSpace'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f); var seds = parse_DataSpaceDefinition(data.content); if(seds.length != 1 || seds[0] != "StrongEncryptionTransform") throw new Error("ECMA-376 Encrypted file bad " + f);
/* 2.3.4.3 */ f = '/!DataSpaces/TransformInfo/StrongEncryptionTransform/!Primary'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f); /*var hdr = */parse_Primary(data.content); } catch(e) {}
f = '/EncryptionInfo'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f); var einfo = parse_EncryptionInfo(data.content);
/* 2.3.4.4 */ f = '/EncryptedPackage'; data = CFB.find(cfb, f); if(!data || !data.content) throw new Error("ECMA-376 Encrypted file missing " + f);
/*global decrypt_agile *//*:: declare var decrypt_agile:any; */ if(einfo[0] == 0x04 && typeof decrypt_agile !== 'undefined') return decrypt_agile(einfo[1], data.content, opts.password || "", opts);/*global decrypt_std76 *//*:: declare var decrypt_std76:any; */ if(einfo[0] == 0x02 && typeof decrypt_std76 !== 'undefined') return decrypt_std76(einfo[1], data.content, opts.password || "", opts); throw new Error("File is password-protected");}

sheetjs

Version Info

Tagged at
2 years ago