deno.land / x / sheetjs@v0.18.3 / modules / 83_numbers.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
var NUMBERS = !Object.defineProperty ? (void 0) :(function() { var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __markAsModule = function(target) { return __defProp(target, "__esModule", { value: true }); }; var __export = function(target, all) { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __reExport = function(target, module, copyDefault, desc) { if (module && typeof module === "object" || typeof module === "function") for (var keys = __getOwnPropNames(module), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) __defProp(target, key, { get: function(k) { return module[k]; }.bind(null, key), enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable }); } return target; }; var __toCommonJS = /* @__PURE__ */ function(cache) { return function(module, temp) { return cache && cache.get(module) || (temp = __reExport(__markAsModule({}), module, 1), cache && cache.set(module, temp), temp); }; }(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
// 83_numbers.ts var numbers_exports = {}; __export(numbers_exports, { parse_numbers: function() { return numbers_default; } });
// src/util.ts var u8_to_dataview = function(array) { return new DataView(array.buffer, array.byteOffset, array.byteLength); }; var u8str = function(u8) { return typeof TextDecoder != "undefined" ? new TextDecoder().decode(u8) : utf8read(a2s(u8)); }; var u8concat = function(u8a) { var len = u8a.reduce(function(acc, x) { return acc + x.length; }, 0); var out = new Uint8Array(len); var off = 0; u8a.forEach(function(u8) { out.set(u8, off); off += u8.length; }); return out; }; var popcnt = function(x) { x -= x >> 1 & 1431655765; x = (x & 858993459) + (x >> 2 & 858993459); return (x + (x >> 4) & 252645135) * 16843009 >>> 24; }; var readDecimal128LE = function(buf, offset) { var exp = (buf[offset + 15] & 127) << 7 | buf[offset + 14] >> 1; var mantissa = buf[offset + 14] & 1; for (var j = offset + 13; j >= offset; --j) mantissa = mantissa * 256 + buf[j]; return (buf[offset + 15] & 128 ? -mantissa : mantissa) * Math.pow(10, exp - 6176); };
// src/proto.ts function parse_varint49(buf, ptr) { var l = ptr ? ptr[0] : 0; var usz = buf[l] & 127; varint: if (buf[l++] >= 128) { usz |= (buf[l] & 127) << 7; if (buf[l++] < 128) break varint; usz |= (buf[l] & 127) << 14; if (buf[l++] < 128) break varint; usz |= (buf[l] & 127) << 21; if (buf[l++] < 128) break varint; usz += (buf[l] & 127) * Math.pow(2, 28); ++l; if (buf[l++] < 128) break varint; usz += (buf[l] & 127) * Math.pow(2, 35); ++l; if (buf[l++] < 128) break varint; usz += (buf[l] & 127) * Math.pow(2, 42); ++l; if (buf[l++] < 128) break varint; } if (ptr) ptr[0] = l; return usz; } function varint_to_i32(buf) { var l = 0, i32 = buf[l] & 127; varint: if (buf[l++] >= 128) { i32 |= (buf[l] & 127) << 7; if (buf[l++] < 128) break varint; i32 |= (buf[l] & 127) << 14; if (buf[l++] < 128) break varint; i32 |= (buf[l] & 127) << 21; if (buf[l++] < 128) break varint; i32 |= (buf[l] & 127) << 28; } return i32; } function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { var off = ptr[0]; var num = parse_varint49(buf, ptr); var type = num & 7; num = Math.floor(num / 8); var len = 0; var res; if (num == 0) break; switch (type) { case 0: { var l = ptr[0]; while (buf[ptr[0]++] >= 128) ; res = buf.slice(l, ptr[0]); } break; case 5: len = 4; res = buf.slice(ptr[0], ptr[0] + len); ptr[0] += len; break; case 1: len = 8; res = buf.slice(ptr[0], ptr[0] + len); ptr[0] += len; break; case 2: len = parse_varint49(buf, ptr); res = buf.slice(ptr[0], ptr[0] + len); ptr[0] += len; break; case 3: case 4: default: throw new Error("PB Type ".concat(type, " for Field ").concat(num, " at offset ").concat(off)); } var v = { offset: off, data: res, type: type }; if (out[num] == null) out[num] = [v]; else out[num].push(v); } return out; } function mappa(data, cb) { if (!data) return []; return data.map(function(d) { var _a; try { return cb(d.data); } catch (e) { var m = (_a = e.message) == null ? void 0 : _a.match(/at offset (\d+)/); if (m) e.message = e.message.replace(/at offset (\d+)/, "at offset " + (+m[1] + d.offset)); throw e; } }); }
// src/frame.ts function deframe(buf) { var out = []; var l = 0; while (l < buf.length) { var t = buf[l++]; var len = buf[l] | buf[l + 1] << 8 | buf[l + 2] << 16; l += 3; out.push(parse_snappy_chunk(t, buf.slice(l, l + len))); l += len; } if (l !== buf.length) throw new Error("data is not a valid framed stream!"); return u8concat(out); } function parse_snappy_chunk(type, buf) { if (type != 0) throw new Error("Unexpected Snappy chunk type ".concat(type)); var ptr = [0]; var usz = parse_varint49(buf, ptr); var chunks = []; while (ptr[0] < buf.length) { var tag = buf[ptr[0]] & 3; if (tag == 0) { var len = buf[ptr[0]++] >> 2; if (len < 60) ++len; else { var c = len - 59; len = buf[ptr[0]]; if (c > 1) len |= buf[ptr[0] + 1] << 8; if (c > 2) len |= buf[ptr[0] + 2] << 16; if (c > 3) len |= buf[ptr[0] + 3] << 24; len >>>= 0; len++; ptr[0] += c; } chunks.push(buf.slice(ptr[0], ptr[0] + len)); ptr[0] += len; continue; } else { var offset = 0, length = 0; if (tag == 1) { length = (buf[ptr[0]] >> 2 & 7) + 4; offset = (buf[ptr[0]++] & 224) << 3; offset |= buf[ptr[0]++]; } else { length = (buf[ptr[0]++] >> 2) + 1; if (tag == 2) { offset = buf[ptr[0]] | buf[ptr[0] + 1] << 8; ptr[0] += 2; } else { offset = (buf[ptr[0]] | buf[ptr[0] + 1] << 8 | buf[ptr[0] + 2] << 16 | buf[ptr[0] + 3] << 24) >>> 0; ptr[0] += 4; } } chunks = [u8concat(chunks)]; if (offset == 0) throw new Error("Invalid offset 0"); if (offset > chunks[0].length) throw new Error("Invalid offset beyond length"); if (length >= offset) { chunks.push(chunks[0].slice(-offset)); length -= offset; while (length >= chunks[chunks.length - 1].length) { chunks.push(chunks[chunks.length - 1]); length -= chunks[chunks.length - 1].length; } } chunks.push(chunks[0].slice(-offset, -offset + length)); } } var o = u8concat(chunks); if (o.length != usz) throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz)); return o; }
// src/iwa.ts function parse_iwa(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { var len = parse_varint49(buf, ptr); var ai = parse_shallow(buf.slice(ptr[0], ptr[0] + len)); ptr[0] += len; var res = { id: varint_to_i32(ai[1][0].data), messages: [] }; ai[2].forEach(function(b) { var mi = parse_shallow(b.data); var fl = varint_to_i32(mi[3][0].data); res.messages.push({ meta: mi, data: buf.slice(ptr[0], ptr[0] + fl) }); ptr[0] += fl; }); out.push(res); } return out; }
// src/cell.ts function parse_old_storage(buf, sst, rsst) { var dv = u8_to_dataview(buf); var ctype = buf[buf[0] == 4 ? 1 : 2]; var flags = dv.getUint32(4, true); var data_offset = 12 + popcnt(flags & 3470) * 4; var ridx = -1, sidx = -1, ieee = NaN, dt = new Date(2001, 0, 1); if (flags & 512) { ridx = dv.getUint32(data_offset, true); data_offset += 4; } data_offset += popcnt(flags & 12288) * 4; if (flags & 16) { sidx = dv.getUint32(data_offset, true); data_offset += 4; } if (flags & 32) { ieee = dv.getFloat64(data_offset, true); data_offset += 8; } if (flags & 64) { dt.setTime(dt.getTime() + dv.getFloat64(data_offset, true) * 1e3); data_offset += 8; } var ret; switch (ctype) { case 0: break; case 2: ret = { t: "n", v: ieee }; break; case 3: ret = { t: "s", v: sst[sidx] }; break; case 5: ret = { t: "d", v: dt }; break; case 6: ret = { t: "b", v: ieee > 0 }; break; case 7: ret = { t: "n", v: ieee }; break; case 8: ret = { t: "e", v: 0 }; break; case 9: { if (ridx > -1) ret = { t: "s", v: rsst[ridx] }; else if (sidx > -1) ret = { t: "s", v: sst[sidx] }; else if (!isNaN(ieee)) ret = { t: "n", v: ieee }; else throw new Error("Unsupported cell type ".concat(buf.slice(0, 4))); } break; default: throw new Error("Unsupported cell type ".concat(buf.slice(0, 4))); } return ret; } function parse_storage(buf, sst, rsst) { var dv = u8_to_dataview(buf); var ctype = buf[1]; var flags = dv.getUint32(8, true); var data_offset = 12; var ridx = -1, sidx = -1, d128 = NaN, ieee = NaN, dt = new Date(2001, 0, 1); if (flags & 1) { d128 = readDecimal128LE(buf, data_offset); data_offset += 16; } if (flags & 2) { ieee = dv.getFloat64(data_offset, true); data_offset += 8; } if (flags & 4) { dt.setTime(dt.getTime() + dv.getFloat64(data_offset, true) * 1e3); data_offset += 8; } if (flags & 8) { sidx = dv.getUint32(data_offset, true); data_offset += 4; } if (flags & 16) { ridx = dv.getUint32(data_offset, true); data_offset += 4; } var ret; switch (ctype) { case 0: break; case 2: ret = { t: "n", v: d128 }; break; case 3: ret = { t: "s", v: sst[sidx] }; break; case 5: ret = { t: "d", v: dt }; break; case 6: ret = { t: "b", v: ieee > 0 }; break; case 7: ret = { t: "n", v: ieee }; break; case 8: ret = { t: "e", v: 0 }; break; case 9: { if (ridx > -1) ret = { t: "s", v: rsst[ridx] }; else throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4))); } break; case 10: ret = { t: "n", v: d128 }; break; default: throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4))); } return ret; } function parse(buf, sst, rsst) { switch (buf[0]) { case 3: case 4: return parse_old_storage(buf, sst, rsst); case 5: return parse_storage(buf, sst, rsst); default: throw new Error("Unsupported payload version ".concat(buf[0])); } }
// src/numbers.ts var encode_col = function(C) { var s = ""; for (++C; C; C = Math.floor((C - 1) / 26)) s = String.fromCharCode((C - 1) % 26 + 65) + s; return s; }; var encode_cell = function(c) { return "".concat(encode_col(c.c)).concat(c.r + 1); }; var encode_range = function(r) { return encode_cell(r.s) + ":" + encode_cell(r.e); }; var book_new = function() { return { Sheets: {}, SheetNames: [] }; }; var book_append_sheet = function(wb, ws, name) { if (!name) for (var i = 1; i < 9999; ++i) { if (wb.SheetNames.indexOf(name = "Sheet ".concat(i)) == -1) break; } else if (wb.SheetNames.indexOf(name) > -1) for (var i = 1; i < 9999; ++i) { if (wb.SheetNames.indexOf("".concat(name, "_").concat(i)) == -1) { name = "".concat(name, "_").concat(i); break; } } wb.SheetNames.push(name); wb.Sheets[name] = ws; }; function parse_numbers(cfb) { var out = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) throw new Error("Unsupported password protection"); }); cfb.FileIndex.forEach(function(s) { if (!s.name.match(/\.iwa$/)) return; var o; try { o = deframe(s.content); } catch (e) { return console.log("?? " + s.content.length + " " + (e.message || e)); } var packets; try { packets = parse_iwa(o); } catch (e) { return console.log("## " + (e.message || e)); } packets.forEach(function(packet) { out[+packet.id] = packet.messages; }); }); if (!out.length) throw new Error("File has no messages"); var docroot; out.forEach(function(iwams) { iwams.forEach(function(iwam) { var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0; if (mtype == 1) { if (!docroot) docroot = iwam; else throw new Error("Document has multiple roots"); } }); }); if (!docroot) throw new Error("Cannot find Document root"); return parse_docroot(out, docroot); } var numbers_default = parse_numbers; function parse_Reference(buf) { var pb = parse_shallow(buf); return parse_varint49(pb[1][0].data); } function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); var entries = pb[3]; var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: data[key] = u8str(le[3][0].data); break; case 8: { var rt = M[parse_Reference(le[9][0].data)][0]; var rtp = parse_shallow(rt.data); var rtpref = M[parse_Reference(rtp[1][0].data)][0]; var mtype = varint_to_i32(rtpref.meta[1][0].data); if (mtype != 2001) throw new Error("2000 unexpected reference to ".concat(mtype)); var tswpsa = parse_shallow(rtpref.data); data[key] = tswpsa[3].map(function(x) { return u8str(x.data); }).join(""); } break; } }); return data; } function parse_TST_TileRowInfo(u8) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j; var pb = parse_shallow(u8); var R = varint_to_i32(pb[1][0].data) >>> 0; var pre_bnc = (_b = (_a = pb[3]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data; var pre_bnc_offsets = ((_d = (_c = pb[4]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && u8_to_dataview(pb[4][0].data); var storage = (_f = (_e = pb[6]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data; var storage_offsets = ((_h = (_g = pb[7]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data) && u8_to_dataview(pb[7][0].data); var wide_offsets = ((_j = (_i = pb[8]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && varint_to_i32(pb[8][0].data) > 0 || false; var width = wide_offsets ? 4 : 1; var cells = []; var off = 0; for (var C = 0; C < pre_bnc_offsets.byteLength / 2; ++C) { if (storage && storage_offsets) { off = storage_offsets.getUint16(C * 2, true) * width; if (off < storage.length) { cells[C] = storage.subarray(off, storage_offsets.getUint16(C * 2 + 2, true) * width); continue; } } if (pre_bnc && pre_bnc_offsets) { off = pre_bnc_offsets.getUint16(C * 2, true) * width; if (off < pre_bnc.length) cells[C] = pre_bnc.subarray(off, pre_bnc_offsets.getUint16(C * 2 + 2, true) * width); } } return { R: R, cells: cells }; } function parse_TST_Tile(M, root) { var pb = parse_shallow(root.data); var ri = mappa(pb[5], parse_TST_TileRowInfo); return ri.reduce(function(acc, x) { if (!acc[x.R]) acc[x.R] = []; x.cells.forEach(function(cell, C) { if (acc[x.R][C]) throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C)); acc[x.R][C] = cell; }); return acc; }, []); } function parse_TST_TableModelArchive(M, root, ws) { var _a; var pb = parse_shallow(root.data); var range = { s: { r: 0, c: 0 }, e: { r: 0, c: 0 } }; range.e.r = (varint_to_i32(pb[6][0].data) >>> 0) - 1; if (range.e.r < 0) throw new Error("Invalid row varint ".concat(pb[6][0].data)); range.e.c = (varint_to_i32(pb[7][0].data) >>> 0) - 1; if (range.e.c < 0) throw new Error("Invalid col varint ".concat(pb[7][0].data)); ws["!ref"] = encode_range(range); { var store = parse_shallow(pb[4][0].data); var sst = parse_TST_TableDataList(M, M[parse_Reference(store[4][0].data)][0]); var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_Reference(store[17][0].data)][0]) : []; { var tile = parse_shallow(store[3][0].data); var tiles = []; tile[1].forEach(function(t) { var tl = parse_shallow(t.data); var ref = M[parse_Reference(tl[2][0].data)][0]; var mtype = varint_to_i32(ref.meta[1][0].data); if (mtype != 6002) throw new Error("6001 unexpected reference to ".concat(mtype)); tiles.push({ id: varint_to_i32(tl[1][0].data), ref: parse_TST_Tile(M, ref) }); }); tiles.forEach(function(tile2) { tile2.ref.forEach(function(row, R) { row.forEach(function(buf, C) { var addr = encode_cell({ r: R, c: C }); var res = parse(buf, sst, rsst); if (res) ws[addr] = res; }); }); }); } } } function parse_TST_TableInfoArchive(M, root) { var pb = parse_shallow(root.data); var out = { "!ref": "A1" }; var tableref = M[parse_Reference(pb[2][0].data)]; var mtype = varint_to_i32(tableref[0].meta[1][0].data); if (mtype != 6001) throw new Error("6000 unexpected reference to ".concat(mtype)); parse_TST_TableModelArchive(M, tableref[0], out); return out; } function parse_sheetroot(M, root) { var _a; var pb = parse_shallow(root.data); var out = { name: ((_a = pb[1]) == null ? void 0 : _a[0]) ? u8str(pb[1][0].data) : "", sheets: [] }; var shapeoffs = mappa(pb[2], parse_Reference); shapeoffs.forEach(function(off) { M[off].forEach(function(m) { var mtype = varint_to_i32(m.meta[1][0].data); if (mtype == 6e3) out.sheets.push(parse_TST_TableInfoArchive(M, m)); }); }); return out; } function parse_docroot(M, root) { var out = book_new(); var pb = parse_shallow(root.data); var sheetoffs = mappa(pb[1], parse_Reference); sheetoffs.forEach(function(off) { M[off].forEach(function(m) { var mtype = varint_to_i32(m.meta[1][0].data); if (mtype == 2) { var root2 = parse_sheetroot(M, m); root2.sheets.forEach(function(sheet) { book_append_sheet(out, sheet, root2.name); }); } }); }); if (out.SheetNames.length == 0) throw new Error("Empty NUMBERS file"); return out; } return __toCommonJS(numbers_exports);})();/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
sheetjs

Version Info

Tagged at
2 years ago