xls-reader 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +1 -1
- package/README.pt-BR.md +1 -1
- package/dist/index.cjs +643 -618
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -8
- package/dist/{index.d.ts → index.d.mts} +13 -8
- package/dist/index.mjs +772 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +13 -8
- package/dist/index.js +0 -722
- package/dist/index.js.map +0 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
//#region src/errors.ts
|
|
2
|
+
var XlsError = class extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "XlsError";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/biff/record-stream.ts
|
|
11
|
+
function readRecords(stream) {
|
|
12
|
+
const view = new DataView(stream.buffer, stream.byteOffset, stream.byteLength);
|
|
13
|
+
const records = [];
|
|
14
|
+
let pos = 0;
|
|
15
|
+
while (pos + 4 <= stream.length) {
|
|
16
|
+
const type = view.getUint16(pos, true);
|
|
17
|
+
const length = view.getUint16(pos + 2, true);
|
|
18
|
+
if (pos + 4 + length > stream.length) break;
|
|
19
|
+
records.push({
|
|
20
|
+
type,
|
|
21
|
+
data: stream.subarray(pos + 4, pos + 4 + length),
|
|
22
|
+
offset: pos
|
|
23
|
+
});
|
|
24
|
+
pos += 4 + length;
|
|
25
|
+
}
|
|
26
|
+
return records;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/byte-reader.ts
|
|
31
|
+
var ByteReader = class {
|
|
32
|
+
constructor(bytes, start = 0) {
|
|
33
|
+
this.bytes = bytes;
|
|
34
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
35
|
+
this.cursor = start;
|
|
36
|
+
}
|
|
37
|
+
get position() {
|
|
38
|
+
return this.cursor;
|
|
39
|
+
}
|
|
40
|
+
set position(value) {
|
|
41
|
+
this.cursor = value;
|
|
42
|
+
}
|
|
43
|
+
get remaining() {
|
|
44
|
+
return this.bytes.length - this.cursor;
|
|
45
|
+
}
|
|
46
|
+
u8() {
|
|
47
|
+
this.require(1);
|
|
48
|
+
const value = this.view.getUint8(this.cursor);
|
|
49
|
+
this.cursor += 1;
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
u16() {
|
|
53
|
+
this.require(2);
|
|
54
|
+
const value = this.view.getUint16(this.cursor, true);
|
|
55
|
+
this.cursor += 2;
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
u32() {
|
|
59
|
+
this.require(4);
|
|
60
|
+
const value = this.view.getUint32(this.cursor, true);
|
|
61
|
+
this.cursor += 4;
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
f64() {
|
|
65
|
+
this.require(8);
|
|
66
|
+
const value = this.view.getFloat64(this.cursor, true);
|
|
67
|
+
this.cursor += 8;
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
slice(length) {
|
|
71
|
+
this.require(length);
|
|
72
|
+
const view = this.bytes.subarray(this.cursor, this.cursor + length);
|
|
73
|
+
this.cursor += length;
|
|
74
|
+
return view;
|
|
75
|
+
}
|
|
76
|
+
skip(length) {
|
|
77
|
+
this.cursor += length;
|
|
78
|
+
}
|
|
79
|
+
require(length) {
|
|
80
|
+
if (!Number.isInteger(this.cursor) || this.cursor < 0) throw new XlsError(`Invalid read offset ${this.cursor}: corrupt structure`);
|
|
81
|
+
if (this.cursor + length > this.bytes.length) throw new XlsError(`Unexpected end of data: needed ${length} byte(s) at offset ${this.cursor}, but only ${this.remaining} remain`);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/biff/rk-number.ts
|
|
87
|
+
function decodeRk(rk) {
|
|
88
|
+
const dividedBy100 = (rk & 1) !== 0;
|
|
89
|
+
const value = (rk & 2) !== 0 ? rk >> 2 : rkAsDouble(rk);
|
|
90
|
+
return dividedBy100 ? value / 100 : value;
|
|
91
|
+
}
|
|
92
|
+
function rkAsDouble(rk) {
|
|
93
|
+
const view = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
|
|
94
|
+
view.setUint32(4, rk & 4294967292, true);
|
|
95
|
+
return view.getFloat64(0, true);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/biff/unicode-string.ts
|
|
100
|
+
const F_WIDE$1 = 1;
|
|
101
|
+
const F_EXT$1 = 4;
|
|
102
|
+
const F_RICH$1 = 8;
|
|
103
|
+
const BYTES_PER_RUN$1 = 4;
|
|
104
|
+
function readUnicodeString(reader, shortLength) {
|
|
105
|
+
const charCount = shortLength ? reader.u8() : reader.u16();
|
|
106
|
+
const flags = reader.u8();
|
|
107
|
+
const wide = (flags & F_WIDE$1) !== 0;
|
|
108
|
+
const runCount = (flags & F_RICH$1) !== 0 ? reader.u16() : 0;
|
|
109
|
+
const extSize = (flags & F_EXT$1) !== 0 ? reader.u32() : 0;
|
|
110
|
+
let text = "";
|
|
111
|
+
for (let i = 0; i < charCount; i++) text += String.fromCharCode(wide ? reader.u16() : reader.u8());
|
|
112
|
+
reader.skip(runCount * BYTES_PER_RUN$1 + extSize);
|
|
113
|
+
return text;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/biff/cell-records.ts
|
|
118
|
+
function decodeLabelSst(data, ctx) {
|
|
119
|
+
const reader = new ByteReader(data);
|
|
120
|
+
const { row, col } = readHead(reader);
|
|
121
|
+
const index = reader.u32();
|
|
122
|
+
return {
|
|
123
|
+
row,
|
|
124
|
+
col,
|
|
125
|
+
value: ctx.sharedStrings[index] ?? ""
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function decodeLabel(data) {
|
|
129
|
+
const reader = new ByteReader(data);
|
|
130
|
+
const { row, col } = readHead(reader);
|
|
131
|
+
return {
|
|
132
|
+
row,
|
|
133
|
+
col,
|
|
134
|
+
value: readUnicodeString(reader, false)
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function decodeNumber(data, ctx) {
|
|
138
|
+
const reader = new ByteReader(data);
|
|
139
|
+
const { row, col, xf } = readHead(reader);
|
|
140
|
+
return {
|
|
141
|
+
row,
|
|
142
|
+
col,
|
|
143
|
+
value: ctx.numeric(xf, reader.f64())
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function decodeRkCell(data, ctx) {
|
|
147
|
+
const reader = new ByteReader(data);
|
|
148
|
+
const { row, col, xf } = readHead(reader);
|
|
149
|
+
return {
|
|
150
|
+
row,
|
|
151
|
+
col,
|
|
152
|
+
value: ctx.numeric(xf, decodeRk(reader.u32()))
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function decodeBlank(data) {
|
|
156
|
+
const { row, col } = readHead(new ByteReader(data));
|
|
157
|
+
return {
|
|
158
|
+
row,
|
|
159
|
+
col,
|
|
160
|
+
value: null
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function decodeBoolErr(data) {
|
|
164
|
+
const reader = new ByteReader(data);
|
|
165
|
+
const { row, col } = readHead(reader);
|
|
166
|
+
const raw = reader.u8();
|
|
167
|
+
return {
|
|
168
|
+
row,
|
|
169
|
+
col,
|
|
170
|
+
value: reader.u8() !== 0 ? null : raw !== 0
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function decodeMulRk(data, ctx) {
|
|
174
|
+
const reader = new ByteReader(data);
|
|
175
|
+
const row = reader.u16();
|
|
176
|
+
const colFirst = reader.u16();
|
|
177
|
+
const count = (data.length - 6) / 6;
|
|
178
|
+
const cells = [];
|
|
179
|
+
for (let i = 0; i < count; i++) {
|
|
180
|
+
const xf = reader.u16();
|
|
181
|
+
cells.push({
|
|
182
|
+
row,
|
|
183
|
+
col: colFirst + i,
|
|
184
|
+
value: ctx.numeric(xf, decodeRk(reader.u32()))
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return cells;
|
|
188
|
+
}
|
|
189
|
+
function decodeMulBlank(data) {
|
|
190
|
+
const row = new ByteReader(data).u16();
|
|
191
|
+
const colFirst = new ByteReader(data, 2).u16();
|
|
192
|
+
const count = (data.length - 6) / 2;
|
|
193
|
+
const cells = [];
|
|
194
|
+
for (let i = 0; i < count; i++) cells.push({
|
|
195
|
+
row,
|
|
196
|
+
col: colFirst + i,
|
|
197
|
+
value: null
|
|
198
|
+
});
|
|
199
|
+
return cells;
|
|
200
|
+
}
|
|
201
|
+
function decodeFormula(data, ctx) {
|
|
202
|
+
const reader = new ByteReader(data);
|
|
203
|
+
const { row, col, xf } = readHead(reader);
|
|
204
|
+
const result = data.subarray(reader.position, reader.position + 8);
|
|
205
|
+
if (isNonNumericResult(result)) return nonNumericResult(row, col, result);
|
|
206
|
+
return {
|
|
207
|
+
kind: "value",
|
|
208
|
+
cell: {
|
|
209
|
+
row,
|
|
210
|
+
col,
|
|
211
|
+
value: ctx.numeric(xf, reader.f64())
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function isNonNumericResult(result) {
|
|
216
|
+
return result[6] === 255 && result[7] === 255;
|
|
217
|
+
}
|
|
218
|
+
function nonNumericResult(row, col, result) {
|
|
219
|
+
const kind = result[0];
|
|
220
|
+
if (kind === 0) return {
|
|
221
|
+
kind: "pending-string",
|
|
222
|
+
row,
|
|
223
|
+
col
|
|
224
|
+
};
|
|
225
|
+
if (kind === 1) return {
|
|
226
|
+
kind: "value",
|
|
227
|
+
cell: {
|
|
228
|
+
row,
|
|
229
|
+
col,
|
|
230
|
+
value: result[2] !== 0
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
return {
|
|
234
|
+
kind: "value",
|
|
235
|
+
cell: {
|
|
236
|
+
row,
|
|
237
|
+
col,
|
|
238
|
+
value: null
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function readHead(reader) {
|
|
243
|
+
return {
|
|
244
|
+
row: reader.u16(),
|
|
245
|
+
col: reader.u16(),
|
|
246
|
+
xf: reader.u16()
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/biff/record-types.ts
|
|
252
|
+
const RecordType = {
|
|
253
|
+
BOF: 2057,
|
|
254
|
+
EOF: 10,
|
|
255
|
+
BOUNDSHEET8: 133,
|
|
256
|
+
DATEMODE: 34,
|
|
257
|
+
FORMAT: 1054,
|
|
258
|
+
XF: 224,
|
|
259
|
+
SST: 252,
|
|
260
|
+
CONTINUE: 60,
|
|
261
|
+
LABELSST: 253,
|
|
262
|
+
LABEL: 516,
|
|
263
|
+
RSTRING: 214,
|
|
264
|
+
NUMBER: 515,
|
|
265
|
+
RK: 638,
|
|
266
|
+
MULRK: 189,
|
|
267
|
+
BLANK: 513,
|
|
268
|
+
MULBLANK: 190,
|
|
269
|
+
BOOLERR: 517,
|
|
270
|
+
FORMULA: 6,
|
|
271
|
+
STRING: 519
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/biff/sheet-decoder.ts
|
|
276
|
+
const DECODERS = /* @__PURE__ */ new Map([
|
|
277
|
+
[RecordType.LABELSST, (d, c) => [decodeLabelSst(d, c)]],
|
|
278
|
+
[RecordType.LABEL, (d) => [decodeLabel(d)]],
|
|
279
|
+
[RecordType.RSTRING, (d) => [decodeLabel(d)]],
|
|
280
|
+
[RecordType.NUMBER, (d, c) => [decodeNumber(d, c)]],
|
|
281
|
+
[RecordType.RK, (d, c) => [decodeRkCell(d, c)]],
|
|
282
|
+
[RecordType.MULRK, (d, c) => decodeMulRk(d, c)],
|
|
283
|
+
[RecordType.BLANK, (d) => [decodeBlank(d)]],
|
|
284
|
+
[RecordType.MULBLANK, (d) => decodeMulBlank(d)],
|
|
285
|
+
[RecordType.BOOLERR, (d) => [decodeBoolErr(d)]]
|
|
286
|
+
]);
|
|
287
|
+
function decodeSheet(records, boundSheet, offsetToIndex, globals) {
|
|
288
|
+
const ctx = {
|
|
289
|
+
sharedStrings: globals.sharedStrings,
|
|
290
|
+
numeric: globals.numeric
|
|
291
|
+
};
|
|
292
|
+
const cells = collectCells(records, offsetToIndex.get(boundSheet.offset), ctx);
|
|
293
|
+
return {
|
|
294
|
+
name: boundSheet.name,
|
|
295
|
+
rows: toGrid(cells)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function collectCells(records, start, ctx) {
|
|
299
|
+
if (start === void 0) return [];
|
|
300
|
+
const cells = [];
|
|
301
|
+
let pending = null;
|
|
302
|
+
for (let i = start + 1; i < records.length; i++) {
|
|
303
|
+
const record = records[i];
|
|
304
|
+
if (record === void 0 || record.type === RecordType.EOF) break;
|
|
305
|
+
pending = handleRecord(record, cells, pending, ctx);
|
|
306
|
+
}
|
|
307
|
+
return cells;
|
|
308
|
+
}
|
|
309
|
+
function handleRecord(record, cells, pending, ctx) {
|
|
310
|
+
if (record.type === RecordType.STRING && pending) {
|
|
311
|
+
cells.push({
|
|
312
|
+
...pending,
|
|
313
|
+
value: readUnicodeString(new ByteReader(record.data), false)
|
|
314
|
+
});
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
if (record.type === RecordType.FORMULA) return appendFormula(record.data, cells, ctx);
|
|
318
|
+
const decode = DECODERS.get(record.type);
|
|
319
|
+
if (decode) cells.push(...decode(record.data, ctx));
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
function appendFormula(data, cells, ctx) {
|
|
323
|
+
const result = decodeFormula(data, ctx);
|
|
324
|
+
if (result.kind === "value") {
|
|
325
|
+
cells.push(result.cell);
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
row: result.row,
|
|
330
|
+
col: result.col
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const MAX_ROW = 65535;
|
|
334
|
+
const MAX_COL = 255;
|
|
335
|
+
function toGrid(cells) {
|
|
336
|
+
if (cells.length === 0) return [];
|
|
337
|
+
const maxRow = cells.reduce((max, cell) => Math.max(max, cell.row), 0);
|
|
338
|
+
const maxCol = cells.reduce((max, cell) => Math.max(max, cell.col), 0);
|
|
339
|
+
if (maxRow > MAX_ROW || maxCol > MAX_COL) throw new XlsError(`Corrupt sheet: cell position (row ${maxRow}, col ${maxCol}) exceeds the BIFF8 limit of 65536 rows × 256 columns`);
|
|
340
|
+
const rows = Array.from({ length: maxRow + 1 }, () => new Array(maxCol + 1).fill(null));
|
|
341
|
+
for (const cell of cells) {
|
|
342
|
+
const row = rows[cell.row];
|
|
343
|
+
if (row) row[cell.col] = cell.value;
|
|
344
|
+
}
|
|
345
|
+
return rows;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/excel-serial-date.ts
|
|
350
|
+
const MS_PER_DAY = 864e5;
|
|
351
|
+
function excelSerialToDate(serial, date1904) {
|
|
352
|
+
return new Date((date1904 ? Date.UTC(1904, 0, 1) : Date.UTC(1899, 11, 30)) + serial * MS_PER_DAY);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
//#endregion
|
|
356
|
+
//#region src/biff/number-format.ts
|
|
357
|
+
const BUILTIN_DATE_FORMAT_IDS = /* @__PURE__ */ new Set([
|
|
358
|
+
14,
|
|
359
|
+
15,
|
|
360
|
+
16,
|
|
361
|
+
17,
|
|
362
|
+
18,
|
|
363
|
+
19,
|
|
364
|
+
20,
|
|
365
|
+
21,
|
|
366
|
+
22,
|
|
367
|
+
27,
|
|
368
|
+
28,
|
|
369
|
+
29,
|
|
370
|
+
30,
|
|
371
|
+
31,
|
|
372
|
+
32,
|
|
373
|
+
33,
|
|
374
|
+
34,
|
|
375
|
+
35,
|
|
376
|
+
36,
|
|
377
|
+
45,
|
|
378
|
+
46,
|
|
379
|
+
47,
|
|
380
|
+
50,
|
|
381
|
+
51,
|
|
382
|
+
52,
|
|
383
|
+
53,
|
|
384
|
+
54,
|
|
385
|
+
55,
|
|
386
|
+
56,
|
|
387
|
+
57,
|
|
388
|
+
58
|
|
389
|
+
]);
|
|
390
|
+
function isDateFormatIndex(formatIndex, customFormats) {
|
|
391
|
+
if (BUILTIN_DATE_FORMAT_IDS.has(formatIndex)) return true;
|
|
392
|
+
const format = customFormats.get(formatIndex);
|
|
393
|
+
return format !== void 0 && looksLikeDateFormat(format);
|
|
394
|
+
}
|
|
395
|
+
function looksLikeDateFormat(format) {
|
|
396
|
+
const stripped = format.replace(/\\./g, "").replace(/"[^"]*"/g, "").replace(/\[[^\]]*\]/g, "");
|
|
397
|
+
return /[ymdhs]/i.test(stripped);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/biff/continued-reader.ts
|
|
402
|
+
var ContinuedReader = class {
|
|
403
|
+
constructor(chunks) {
|
|
404
|
+
this.chunks = chunks;
|
|
405
|
+
this.chunkIndex = 0;
|
|
406
|
+
this.offset = 0;
|
|
407
|
+
}
|
|
408
|
+
get done() {
|
|
409
|
+
this.settle();
|
|
410
|
+
return this.chunkIndex >= this.chunks.length;
|
|
411
|
+
}
|
|
412
|
+
u8() {
|
|
413
|
+
this.settle();
|
|
414
|
+
return this.rawByte();
|
|
415
|
+
}
|
|
416
|
+
u16() {
|
|
417
|
+
return this.u8() | this.u8() << 8;
|
|
418
|
+
}
|
|
419
|
+
u32() {
|
|
420
|
+
return (this.u16() | this.u16() << 16) >>> 0;
|
|
421
|
+
}
|
|
422
|
+
readChars(count, wide) {
|
|
423
|
+
let text = "";
|
|
424
|
+
let isWide = wide;
|
|
425
|
+
for (let i = 0; i < count; i++) {
|
|
426
|
+
if (this.atChunkEnd()) isWide = (this.crossToNextChunk() & 1) !== 0;
|
|
427
|
+
text += String.fromCharCode(isWide ? this.rawByte() | this.rawByte() << 8 : this.rawByte());
|
|
428
|
+
}
|
|
429
|
+
return text;
|
|
430
|
+
}
|
|
431
|
+
skip(length) {
|
|
432
|
+
let left = length;
|
|
433
|
+
while (left > 0) {
|
|
434
|
+
this.settle();
|
|
435
|
+
const chunk = this.currentChunk();
|
|
436
|
+
const take = Math.min(left, chunk.length - this.offset);
|
|
437
|
+
this.offset += take;
|
|
438
|
+
left -= take;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
settle() {
|
|
442
|
+
while (this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength()) {
|
|
443
|
+
this.chunkIndex += 1;
|
|
444
|
+
this.offset = 0;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
atChunkEnd() {
|
|
448
|
+
return this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength();
|
|
449
|
+
}
|
|
450
|
+
crossToNextChunk() {
|
|
451
|
+
this.chunkIndex += 1;
|
|
452
|
+
this.offset = 0;
|
|
453
|
+
return this.rawByte();
|
|
454
|
+
}
|
|
455
|
+
rawByte() {
|
|
456
|
+
const value = this.currentChunk()[this.offset];
|
|
457
|
+
if (value === void 0) throw new XlsError(`SST cursor past end of chunk ${this.chunkIndex} (offset ${this.offset})`);
|
|
458
|
+
this.offset += 1;
|
|
459
|
+
return value;
|
|
460
|
+
}
|
|
461
|
+
chunkLength() {
|
|
462
|
+
return this.currentChunk().length;
|
|
463
|
+
}
|
|
464
|
+
currentChunk() {
|
|
465
|
+
const chunk = this.chunks[this.chunkIndex];
|
|
466
|
+
if (chunk === void 0) throw new XlsError(`SST reader ran past its ${this.chunks.length} chunk(s)`);
|
|
467
|
+
return chunk;
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
//#endregion
|
|
472
|
+
//#region src/biff/shared-strings.ts
|
|
473
|
+
const F_WIDE = 1;
|
|
474
|
+
const F_EXT = 4;
|
|
475
|
+
const F_RICH = 8;
|
|
476
|
+
const BYTES_PER_RUN = 4;
|
|
477
|
+
function parseSharedStrings(chunks) {
|
|
478
|
+
const reader = new ContinuedReader(chunks);
|
|
479
|
+
reader.u32();
|
|
480
|
+
const unique = reader.u32();
|
|
481
|
+
const strings = [];
|
|
482
|
+
for (let i = 0; i < unique; i++) strings.push(readString(reader));
|
|
483
|
+
return strings;
|
|
484
|
+
}
|
|
485
|
+
function readString(reader) {
|
|
486
|
+
const charCount = reader.u16();
|
|
487
|
+
const flags = reader.u8();
|
|
488
|
+
const runCount = (flags & F_RICH) !== 0 ? reader.u16() : 0;
|
|
489
|
+
const extSize = (flags & F_EXT) !== 0 ? reader.u32() : 0;
|
|
490
|
+
const text = reader.readChars(charCount, (flags & F_WIDE) !== 0);
|
|
491
|
+
reader.skip(runCount * BYTES_PER_RUN + extSize);
|
|
492
|
+
return text;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/biff/workbook-globals.ts
|
|
497
|
+
function parseGlobals(records) {
|
|
498
|
+
let date1904 = false;
|
|
499
|
+
const customFormats = /* @__PURE__ */ new Map();
|
|
500
|
+
const xfFormatIndex = [];
|
|
501
|
+
let sharedStrings = [];
|
|
502
|
+
const boundSheets = [];
|
|
503
|
+
for (let i = 0; i < records.length; i++) {
|
|
504
|
+
const record = records[i];
|
|
505
|
+
if (record === void 0 || record.type === RecordType.EOF) break;
|
|
506
|
+
if (record.type === RecordType.DATEMODE) date1904 = new ByteReader(record.data).u16() === 1;
|
|
507
|
+
else if (record.type === RecordType.FORMAT) addFormat(record.data, customFormats);
|
|
508
|
+
else if (record.type === RecordType.XF) xfFormatIndex.push(new ByteReader(record.data, 2).u16());
|
|
509
|
+
else if (record.type === RecordType.SST) sharedStrings = parseSharedStrings(collectSstChunks(records, i));
|
|
510
|
+
else if (record.type === RecordType.BOUNDSHEET8) boundSheets.push(parseBoundSheet(record.data));
|
|
511
|
+
}
|
|
512
|
+
return {
|
|
513
|
+
sharedStrings,
|
|
514
|
+
boundSheets,
|
|
515
|
+
numeric: makeNumeric(xfFormatIndex, customFormats, date1904)
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
function addFormat(data, into) {
|
|
519
|
+
const reader = new ByteReader(data);
|
|
520
|
+
const formatIndex = reader.u16();
|
|
521
|
+
into.set(formatIndex, readUnicodeString(reader, false));
|
|
522
|
+
}
|
|
523
|
+
function collectSstChunks(records, sstIndex) {
|
|
524
|
+
const first = records[sstIndex];
|
|
525
|
+
const chunks = first ? [first.data] : [];
|
|
526
|
+
for (let j = sstIndex + 1; j < records.length; j++) {
|
|
527
|
+
const record = records[j];
|
|
528
|
+
if (record === void 0 || record.type !== RecordType.CONTINUE) break;
|
|
529
|
+
chunks.push(record.data);
|
|
530
|
+
}
|
|
531
|
+
return chunks;
|
|
532
|
+
}
|
|
533
|
+
function parseBoundSheet(data) {
|
|
534
|
+
const reader = new ByteReader(data);
|
|
535
|
+
const offset = reader.u32();
|
|
536
|
+
reader.u16();
|
|
537
|
+
return {
|
|
538
|
+
name: readUnicodeString(reader, true),
|
|
539
|
+
offset
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function makeNumeric(xfFormatIndex, customFormats, date1904) {
|
|
543
|
+
return (xfIndex, value) => {
|
|
544
|
+
return isDateFormatIndex(xfFormatIndex[xfIndex] ?? 0, customFormats) ? excelSerialToDate(value, date1904) : value;
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/biff/parse-workbook.ts
|
|
550
|
+
function parseWorkbook(stream) {
|
|
551
|
+
const records = readRecords(stream);
|
|
552
|
+
const globals = parseGlobals(records);
|
|
553
|
+
const offsetToIndex = new Map(records.map((record, index) => [record.offset, index]));
|
|
554
|
+
return { sheets: globals.boundSheets.map((sheet) => decodeSheet(records, sheet, offsetToIndex, globals)) };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
//#endregion
|
|
558
|
+
//#region src/cfb/cfb-header.ts
|
|
559
|
+
const SIGNATURE = [
|
|
560
|
+
208,
|
|
561
|
+
207,
|
|
562
|
+
17,
|
|
563
|
+
224,
|
|
564
|
+
161,
|
|
565
|
+
177,
|
|
566
|
+
26,
|
|
567
|
+
225
|
|
568
|
+
];
|
|
569
|
+
function parseCfbHeader(bytes) {
|
|
570
|
+
assertSignature(bytes);
|
|
571
|
+
const reader = new ByteReader(bytes, 30);
|
|
572
|
+
const sectorSize = powerOfTwoSize(reader.u16(), "sector", [9, 12]);
|
|
573
|
+
const miniSectorSize = powerOfTwoSize(reader.u16(), "mini-sector", [6]);
|
|
574
|
+
reader.position = 44;
|
|
575
|
+
reader.u32();
|
|
576
|
+
const firstDirSector = reader.u32();
|
|
577
|
+
reader.u32();
|
|
578
|
+
const miniStreamCutoff = reader.u32();
|
|
579
|
+
const firstMiniFatSector = reader.u32();
|
|
580
|
+
reader.u32();
|
|
581
|
+
return {
|
|
582
|
+
sectorSize,
|
|
583
|
+
miniSectorSize,
|
|
584
|
+
firstDirSector,
|
|
585
|
+
miniStreamCutoff,
|
|
586
|
+
firstMiniFatSector,
|
|
587
|
+
firstDifatSector: reader.u32(),
|
|
588
|
+
numDifatSectors: reader.u32(),
|
|
589
|
+
initialDifat: readInitialDifat(bytes)
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
function powerOfTwoSize(shift, kind, allowed) {
|
|
593
|
+
if (!allowed.includes(shift)) throw new XlsError(`Invalid CFB ${kind} shift ${shift}: expected one of [${allowed.join(", ")}]`);
|
|
594
|
+
return 1 << shift;
|
|
595
|
+
}
|
|
596
|
+
function readInitialDifat(bytes) {
|
|
597
|
+
const reader = new ByteReader(bytes, 76);
|
|
598
|
+
const entries = [];
|
|
599
|
+
for (let i = 0; i < 109; i++) entries.push(reader.u32());
|
|
600
|
+
return entries;
|
|
601
|
+
}
|
|
602
|
+
function assertSignature(bytes) {
|
|
603
|
+
if (bytes.length < 512) throw new XlsError(`File too small to be an OLE2 container: ${bytes.length} bytes (need >= 512)`);
|
|
604
|
+
for (let i = 0; i < SIGNATURE.length; i++) if (bytes[i] !== SIGNATURE[i]) throw new XlsError(`Not an OLE2 / .xls file: expected magic D0CF11E0A1B11AE1, got ${Array.from(bytes.slice(0, 8), (b) => b.toString(16).padStart(2, "0")).join(" ")}`);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
//#endregion
|
|
608
|
+
//#region src/cfb/directory.ts
|
|
609
|
+
const DIRECTORY_ENTRY_SIZE = 128;
|
|
610
|
+
const OBJECT_TYPE_ROOT = 5;
|
|
611
|
+
function parseDirectory(directoryBytes) {
|
|
612
|
+
const entries = [];
|
|
613
|
+
const count = Math.floor(directoryBytes.length / DIRECTORY_ENTRY_SIZE);
|
|
614
|
+
for (let i = 0; i < count; i++) {
|
|
615
|
+
const entry = parseEntry(directoryBytes.subarray(i * DIRECTORY_ENTRY_SIZE));
|
|
616
|
+
if (entry) entries.push(entry);
|
|
617
|
+
}
|
|
618
|
+
return entries;
|
|
619
|
+
}
|
|
620
|
+
function findRoot(entries) {
|
|
621
|
+
return entries.find((entry) => entry.objectType === OBJECT_TYPE_ROOT);
|
|
622
|
+
}
|
|
623
|
+
function parseEntry(entryBytes) {
|
|
624
|
+
const reader = new ByteReader(entryBytes);
|
|
625
|
+
const nameBytes = reader.slice(64);
|
|
626
|
+
const nameLength = reader.u16();
|
|
627
|
+
const objectType = reader.u8();
|
|
628
|
+
if (objectType === 0) return null;
|
|
629
|
+
reader.position = 116;
|
|
630
|
+
const startSector = reader.u32();
|
|
631
|
+
const size = reader.u32();
|
|
632
|
+
return {
|
|
633
|
+
name: decodeName(nameBytes, nameLength),
|
|
634
|
+
objectType,
|
|
635
|
+
startSector,
|
|
636
|
+
size
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
function decodeName(nameBytes, byteLength) {
|
|
640
|
+
const chars = Math.max(0, byteLength / 2 - 1);
|
|
641
|
+
const reader = new ByteReader(nameBytes);
|
|
642
|
+
let name = "";
|
|
643
|
+
for (let i = 0; i < chars; i++) name += String.fromCharCode(reader.u16());
|
|
644
|
+
return name;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
//#endregion
|
|
648
|
+
//#region src/cfb/sector-chains.ts
|
|
649
|
+
const ENDOFCHAIN = 4294967294;
|
|
650
|
+
const FREESECT = 4294967295;
|
|
651
|
+
function sectorOffset(id, sectorSize) {
|
|
652
|
+
return (id + 1) * sectorSize;
|
|
653
|
+
}
|
|
654
|
+
function buildFat(bytes, header) {
|
|
655
|
+
const fatSectorIds = collectFatSectorIds(bytes, header);
|
|
656
|
+
const fat = [];
|
|
657
|
+
const perSector = header.sectorSize / 4;
|
|
658
|
+
for (const id of fatSectorIds) {
|
|
659
|
+
const reader = new ByteReader(bytes, sectorOffset(id, header.sectorSize));
|
|
660
|
+
for (let i = 0; i < perSector; i++) fat.push(reader.u32());
|
|
661
|
+
}
|
|
662
|
+
return fat;
|
|
663
|
+
}
|
|
664
|
+
function collectFatSectorIds(bytes, header) {
|
|
665
|
+
const ids = header.initialDifat.filter((id) => id !== FREESECT && id !== ENDOFCHAIN);
|
|
666
|
+
const perSector = header.sectorSize / 4;
|
|
667
|
+
const seen = /* @__PURE__ */ new Set();
|
|
668
|
+
let difatSector = header.firstDifatSector;
|
|
669
|
+
for (let n = 0; n < header.numDifatSectors && difatSector !== ENDOFCHAIN && !seen.has(difatSector); n++) {
|
|
670
|
+
seen.add(difatSector);
|
|
671
|
+
const reader = new ByteReader(bytes, sectorOffset(difatSector, header.sectorSize));
|
|
672
|
+
for (let i = 0; i < perSector - 1; i++) {
|
|
673
|
+
const id = reader.u32();
|
|
674
|
+
if (id !== FREESECT && id !== ENDOFCHAIN) ids.push(id);
|
|
675
|
+
}
|
|
676
|
+
difatSector = reader.u32();
|
|
677
|
+
}
|
|
678
|
+
return ids;
|
|
679
|
+
}
|
|
680
|
+
function followChain(fat, start) {
|
|
681
|
+
const chain = [];
|
|
682
|
+
const seen = /* @__PURE__ */ new Set();
|
|
683
|
+
let current = start;
|
|
684
|
+
while (current !== ENDOFCHAIN && current < fat.length && !seen.has(current)) {
|
|
685
|
+
seen.add(current);
|
|
686
|
+
chain.push(current);
|
|
687
|
+
current = fat[current] ?? ENDOFCHAIN;
|
|
688
|
+
}
|
|
689
|
+
return chain;
|
|
690
|
+
}
|
|
691
|
+
function readChainBytes(bytes, chain, sectorSize, size) {
|
|
692
|
+
const out = new Uint8Array(chain.length * sectorSize);
|
|
693
|
+
chain.forEach((id, index) => {
|
|
694
|
+
const start = sectorOffset(id, sectorSize);
|
|
695
|
+
out.set(bytes.subarray(start, start + sectorSize), index * sectorSize);
|
|
696
|
+
});
|
|
697
|
+
return out.subarray(0, size);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
//#endregion
|
|
701
|
+
//#region src/cfb/compound-file.ts
|
|
702
|
+
function openCompoundFile(bytes) {
|
|
703
|
+
const header = parseCfbHeader(bytes);
|
|
704
|
+
const fat = buildFat(bytes, header);
|
|
705
|
+
const directory = parseDirectory(directoryBytes(bytes, header, fat));
|
|
706
|
+
const file = {
|
|
707
|
+
bytes,
|
|
708
|
+
header,
|
|
709
|
+
fat,
|
|
710
|
+
directory,
|
|
711
|
+
miniStream: readMiniStreamContainer(bytes, header, fat, directory),
|
|
712
|
+
miniFat: buildMiniFat(bytes, header, fat)
|
|
713
|
+
};
|
|
714
|
+
return { readStream(name) {
|
|
715
|
+
const entry = directory.find((e) => e.name === name);
|
|
716
|
+
if (!entry) return void 0;
|
|
717
|
+
return entry.size < header.miniStreamCutoff ? readMiniStream(file, entry) : readRegularStream(file, entry);
|
|
718
|
+
} };
|
|
719
|
+
}
|
|
720
|
+
function directoryBytes(bytes, header, fat) {
|
|
721
|
+
const chain = followChain(fat, header.firstDirSector);
|
|
722
|
+
return readChainBytes(bytes, chain, header.sectorSize, chain.length * header.sectorSize);
|
|
723
|
+
}
|
|
724
|
+
function readRegularStream(file, entry) {
|
|
725
|
+
const chain = followChain(file.fat, entry.startSector);
|
|
726
|
+
return readChainBytes(file.bytes, chain, file.header.sectorSize, entry.size);
|
|
727
|
+
}
|
|
728
|
+
function readMiniStreamContainer(bytes, header, fat, directory) {
|
|
729
|
+
const root = findRoot(directory);
|
|
730
|
+
if (!root) return /* @__PURE__ */ new Uint8Array(0);
|
|
731
|
+
return readChainBytes(bytes, followChain(fat, root.startSector), header.sectorSize, root.size);
|
|
732
|
+
}
|
|
733
|
+
function buildMiniFat(bytes, header, fat) {
|
|
734
|
+
const chain = followChain(fat, header.firstMiniFatSector);
|
|
735
|
+
return toU32Array(readChainBytes(bytes, chain, header.sectorSize, chain.length * header.sectorSize));
|
|
736
|
+
}
|
|
737
|
+
function readMiniStream(file, entry) {
|
|
738
|
+
const { miniStream, miniFat, header } = file;
|
|
739
|
+
const chain = followChain(miniFat, entry.startSector);
|
|
740
|
+
const out = new Uint8Array(chain.length * header.miniSectorSize);
|
|
741
|
+
chain.forEach((id, index) => {
|
|
742
|
+
const start = id * header.miniSectorSize;
|
|
743
|
+
out.set(miniStream.subarray(start, start + header.miniSectorSize), index * header.miniSectorSize);
|
|
744
|
+
});
|
|
745
|
+
return out.subarray(0, entry.size);
|
|
746
|
+
}
|
|
747
|
+
function toU32Array(raw) {
|
|
748
|
+
const reader = new ByteReader(raw);
|
|
749
|
+
const values = [];
|
|
750
|
+
const count = Math.floor(raw.length / 4);
|
|
751
|
+
for (let i = 0; i < count; i++) values.push(reader.u32());
|
|
752
|
+
return values;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
//#endregion
|
|
756
|
+
//#region src/reader.ts
|
|
757
|
+
function readXls(data) {
|
|
758
|
+
const cfb = openCompoundFile(toBytes(data));
|
|
759
|
+
const stream = cfb.readStream("Workbook") ?? cfb.readStream("Book");
|
|
760
|
+
if (!stream) throw new XlsError("No 'Workbook' or 'Book' stream in the OLE2 file — not a BIFF .xls workbook");
|
|
761
|
+
return parseWorkbook(stream);
|
|
762
|
+
}
|
|
763
|
+
function readFirstSheet(data) {
|
|
764
|
+
return readXls(data).sheets[0];
|
|
765
|
+
}
|
|
766
|
+
function toBytes(data) {
|
|
767
|
+
return data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
//#endregion
|
|
771
|
+
export { XlsError, excelSerialToDate, readFirstSheet, readXls };
|
|
772
|
+
//# sourceMappingURL=index.mjs.map
|