numbers parser prefer subarray

This commit is contained in:
SheetJS 2022-07-13 03:52:29 -04:00
parent ef6d3086ac
commit dbc30ef188
8 changed files with 493 additions and 277 deletions

View File

@ -193,7 +193,7 @@ ctest: ## Build browser test fixtures
.PHONY: ctestserv
ctestserv: ## Start a test server on port 8000
@cd tests && python -mSimpleHTTPServer
@cd tests && python -mSimpleHTTPServer || python3 -mhttp.server || npx -y http-server -p 8000 .
## Code Checking

View File

@ -1,4 +1,7 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if (typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined")
subarray = "slice";
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
}
@ -117,7 +120,7 @@ function write_varint49(v) {
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
@ -153,22 +156,22 @@ function parse_shallow(buf) {
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
res = buf[subarray](l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
@ -210,7 +213,7 @@ function parse_iwa_file(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));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
@ -221,7 +224,7 @@ function parse_iwa_file(buf) {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -281,7 +284,7 @@ function parse_snappy_chunk(type, buf) {
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
chunks.push(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
@ -300,35 +303,57 @@ function parse_snappy_chunk(type, buf) {
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;
}
var j = chunks.length - 1, off = offset;
while (j >= 0 && off >= chunks[j].length) {
off -= chunks[j].length;
--j;
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if (j < 0) {
if (off == 0)
off = chunks[j = 0].length;
else
throw new Error("Invalid offset beyond length");
}
if (length < off)
chunks.push(chunks[j][subarray](-off, -off + length));
else {
if (off > 0) {
chunks.push(chunks[j][subarray](-off));
length -= off;
}
++j;
while (length >= chunks[j].length) {
chunks.push(chunks[j]);
length -= chunks[j].length;
++j;
}
if (length)
chunks.push(chunks[j][subarray](0, length));
}
if (chunks.length > 100)
chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
if (chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0) != usz)
throw new Error("Unexpected length: ".concat(chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0), " != ").concat(usz));
return chunks;
}
function decompress_iwa_file(buf) {
if (Array.isArray(buf))
buf = new Uint8Array(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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if (l !== buf.length)
@ -361,7 +386,7 @@ function compress_iwa_file(buf) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
out.push(buf[subarray](l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
@ -420,11 +445,11 @@ function parse_old_storage(buf, sst, rsst, v) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
return ret;
}
@ -480,14 +505,14 @@ function parse_new_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
return ret;
}
@ -519,7 +544,7 @@ function write_new_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function write_old_storage(cell, sst) {
var out = new Uint8Array(32), dv = u8_to_dataview(out), l = 12, flags = 0;
@ -549,7 +574,7 @@ function write_old_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
@ -631,9 +656,9 @@ function parse_TST_TileRowInfo(u8, type) {
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C + 1][1] * width);
if (offsets.length >= 1)
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
@ -674,6 +699,7 @@ function parse_TST_TableModelArchive(M, root, ws) {
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
@ -688,10 +714,17 @@ function parse_TST_TableModelArchive(M, root, ws) {
var _tile = parse_TST_Tile(M, ref2);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
if (res) {
if (dense) {
if (!ws[_R + R])
ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({ r: _R + R, c: C });
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -714,9 +747,14 @@ function parse_TST_TableModelArchive(M, root, ws) {
});
}
}
function parse_TST_TableInfoArchive(M, root) {
function parse_TST_TableInfoArchive(M, root, opts) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var out;
if (!(opts == null ? void 0 : opts.dense))
out = { "!ref": "A1" };
else
out = [];
out["!ref"] = "A1";
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
@ -724,7 +762,7 @@ function parse_TST_TableInfoArchive(M, root) {
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
function parse_TN_SheetArchive(M, root, opts) {
var _a;
var pb = parse_shallow(root.data);
var out = {
@ -736,12 +774,12 @@ function parse_TN_SheetArchive(M, root) {
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));
out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
function parse_TN_DocumentArchive(M, root, opts) {
var _a;
var out = book_new();
var pb = parse_shallow(root.data);
@ -752,7 +790,7 @@ function parse_TN_DocumentArchive(M, root) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
var root2 = parse_TN_SheetArchive(M, m, opts);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
@ -764,7 +802,7 @@ function parse_TN_DocumentArchive(M, root) {
out.bookType = "numbers";
return out;
}
function parse_numbers_iwa(cfb) {
function parse_numbers_iwa(cfb, opts) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var M = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
@ -810,7 +848,7 @@ function parse_numbers_iwa(cfb) {
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
function write_tile_row(tri, data, SST, wide) {
var _a, _b;

View File

@ -69,10 +69,10 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
if(safegetzipfile(zip, 'Index/Document.iwa')) {
if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support');
if(typeof parse_numbers_iwa != "undefined") {
if(zip.FileIndex) return parse_numbers_iwa(zip);
if(zip.FileIndex) return parse_numbers_iwa(zip, opts);
var _zip = CFB.utils.cfb_new();
zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); });
return parse_numbers_iwa(_zip);
return parse_numbers_iwa(_zip, opts);
}
throw new Error('Unsupported NUMBERS file');
}

View File

@ -1,4 +1,7 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if (typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined")
subarray = "slice";
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
}
@ -117,7 +120,7 @@ function write_varint49(v) {
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
@ -153,22 +156,22 @@ function parse_shallow(buf) {
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
res = buf[subarray](l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
@ -210,7 +213,7 @@ function parse_iwa_file(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));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
@ -221,7 +224,7 @@ function parse_iwa_file(buf) {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -281,7 +284,7 @@ function parse_snappy_chunk(type, buf) {
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
chunks.push(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
@ -300,35 +303,57 @@ function parse_snappy_chunk(type, buf) {
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;
}
var j = chunks.length - 1, off = offset;
while (j >= 0 && off >= chunks[j].length) {
off -= chunks[j].length;
--j;
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if (j < 0) {
if (off == 0)
off = chunks[j = 0].length;
else
throw new Error("Invalid offset beyond length");
}
if (length < off)
chunks.push(chunks[j][subarray](-off, -off + length));
else {
if (off > 0) {
chunks.push(chunks[j][subarray](-off));
length -= off;
}
++j;
while (length >= chunks[j].length) {
chunks.push(chunks[j]);
length -= chunks[j].length;
++j;
}
if (length)
chunks.push(chunks[j][subarray](0, length));
}
if (chunks.length > 100)
chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
if (chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0) != usz)
throw new Error("Unexpected length: ".concat(chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0), " != ").concat(usz));
return chunks;
}
function decompress_iwa_file(buf) {
if (Array.isArray(buf))
buf = new Uint8Array(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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if (l !== buf.length)
@ -361,7 +386,7 @@ function compress_iwa_file(buf) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
out.push(buf[subarray](l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
@ -420,11 +445,11 @@ function parse_old_storage(buf, sst, rsst, v) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
return ret;
}
@ -480,14 +505,14 @@ function parse_new_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
return ret;
}
@ -519,7 +544,7 @@ function write_new_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function write_old_storage(cell, sst) {
var out = new Uint8Array(32), dv = u8_to_dataview(out), l = 12, flags = 0;
@ -549,7 +574,7 @@ function write_old_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
@ -631,9 +656,9 @@ function parse_TST_TileRowInfo(u8, type) {
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C + 1][1] * width);
if (offsets.length >= 1)
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
@ -674,6 +699,7 @@ function parse_TST_TableModelArchive(M, root, ws) {
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
@ -688,10 +714,17 @@ function parse_TST_TableModelArchive(M, root, ws) {
var _tile = parse_TST_Tile(M, ref2);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
if (res) {
if (dense) {
if (!ws[_R + R])
ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({ r: _R + R, c: C });
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -714,9 +747,14 @@ function parse_TST_TableModelArchive(M, root, ws) {
});
}
}
function parse_TST_TableInfoArchive(M, root) {
function parse_TST_TableInfoArchive(M, root, opts) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var out;
if (!(opts == null ? void 0 : opts.dense))
out = { "!ref": "A1" };
else
out = [];
out["!ref"] = "A1";
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
@ -724,7 +762,7 @@ function parse_TST_TableInfoArchive(M, root) {
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
function parse_TN_SheetArchive(M, root, opts) {
var _a;
var pb = parse_shallow(root.data);
var out = {
@ -736,12 +774,12 @@ function parse_TN_SheetArchive(M, root) {
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));
out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
function parse_TN_DocumentArchive(M, root, opts) {
var _a;
var out = book_new();
var pb = parse_shallow(root.data);
@ -752,7 +790,7 @@ function parse_TN_DocumentArchive(M, root) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
var root2 = parse_TN_SheetArchive(M, m, opts);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
@ -764,7 +802,7 @@ function parse_TN_DocumentArchive(M, root) {
out.bookType = "numbers";
return out;
}
function parse_numbers_iwa(cfb) {
function parse_numbers_iwa(cfb, opts) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var M = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
@ -810,7 +848,7 @@ function parse_numbers_iwa(cfb) {
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
function write_tile_row(tri, data, SST, wide) {
var _a, _b;

View File

@ -3,7 +3,7 @@
/* these are type imports and do not show up in the generated JS */
import { CFB$Container, CFB$Entry } from 'cfb';
import { WorkBook, WorkSheet, Range, CellObject } from '../';
import { WorkBook, WorkSheet, Range, CellObject, ParsingOptions, WritingOptions } from '../';
import type { utils } from "../";
declare var encode_cell: typeof utils.encode_cell;
@ -17,6 +17,9 @@ declare var CFB: typeof _CFB;
//<<import { utils } from "../../";
//<<const { encode_cell, encode_range, book_new, book_append_sheet } = utils;
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if(typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined") subarray = "slice";
function u8_to_dataview(array: Uint8Array): DataView { return new DataView(array.buffer, array.byteOffset, array.byteLength); }
//<<export { u8_to_dataview };
@ -104,7 +107,7 @@ function write_varint49(v: number): Uint8Array {
if(v <= 0x3FFFFFFFFFF) break sz;
usz[L-1] |= 0x80; usz[L] = ((v/0x1000000) >>> 21) & 0x7F; ++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
//<<export { parse_varint49, write_varint49 };
@ -141,11 +144,11 @@ function parse_shallow(buf: Uint8Array): ProtoMessage {
case 0: {
var l = ptr[0];
while(buf[ptr[0]++] >= 0x80);
res = buf.slice(l, ptr[0]);
res = buf[subarray](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 5: len = 4; res = buf[subarray](ptr[0], ptr[0] + len); ptr[0] += len; break;
case 1: len = 8; res = buf[subarray](ptr[0], ptr[0] + len); ptr[0] += len; break;
case 2: len = parse_varint49(buf, ptr); res = buf[subarray](ptr[0], ptr[0] + len); ptr[0] += len; break;
case 3: // Start group
case 4: // End group
default: throw new Error(`PB Type ${type} for Field ${num} at offset ${off}`);
@ -194,7 +197,7 @@ function parse_iwa_file(buf: Uint8Array): IWAArchiveInfo[] {
while(ptr[0] < buf.length) {
/* .TSP.ArchiveInfo */
var len = parse_varint49(buf, ptr);
var ai = parse_shallow(buf.slice(ptr[0], ptr[0] + len));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res: IWAArchiveInfo = {
@ -207,7 +210,7 @@ function parse_iwa_file(buf: Uint8Array): IWAArchiveInfo[] {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -243,7 +246,7 @@ function write_iwa_file(ias: IWAArchiveInfo[]): Uint8Array {
//<<export { IWAMessage, IWAArchiveInfo, parse_iwa_file, write_iwa_file };
/** Decompress a snappy chunk */
function parse_snappy_chunk(type: number, buf: Uint8Array): Uint8Array {
function parse_snappy_chunk(type: number, buf: Uint8Array): Uint8Array[] {
if(type != 0) throw new Error(`Unexpected Snappy chunk type ${type}`);
var ptr: Ptr = [0];
@ -263,7 +266,7 @@ function parse_snappy_chunk(type: number, buf: Uint8Array): Uint8Array {
len >>>=0; len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len)); ptr[0] += len; continue;
chunks.push(buf[subarray](ptr[0], ptr[0] + len)); ptr[0] += len; continue;
} else {
var offset = 0, length = 0;
if(tag == 1) {
@ -275,32 +278,38 @@ function parse_snappy_chunk(type: number, buf: Uint8Array): Uint8Array {
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;
}
var j = chunks.length - 1, off = offset;
while(j >=0 && off >= chunks[j].length) { off -= chunks[j].length; --j; }
if(j < 0) {
if(off == 0) off = chunks[(j = 0)].length;
else throw new Error("Invalid offset beyond length");
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if(length < off) chunks.push(chunks[j][subarray](-off, -off + length));
else {
if(off > 0) { chunks.push(chunks[j][subarray](-off)); length -= off; } ++j;
while(length >= chunks[j].length) { chunks.push(chunks[j]); length -= chunks[j].length; ++j; }
if(length) chunks.push(chunks[j][subarray](0, length));
}
if(chunks.length > 100) chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if(o.length != usz) throw new Error(`Unexpected length: ${o.length} != ${usz}`);
return o;
if(chunks.reduce((acc, u8) => acc + u8.length, 0) != usz) throw new Error(`Unexpected length: ${chunks.reduce((acc, u8) => acc + u8.length, 0)} != ${usz}`);
return chunks;
//var o = u8concat(chunks);
//if(o.length != usz) throw new Error(`Unexpected length: ${o.length} != ${usz}`);
//return o;
}
/** Decompress IWA file */
function decompress_iwa_file(buf: Uint8Array): Uint8Array {
if(Array.isArray(buf)) buf = new Uint8Array(buf);
var out: Uint8Array[] = [];
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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if(l !== buf.length) throw new Error("data is not a valid framed stream!");
@ -325,7 +334,7 @@ function compress_iwa_file(buf: Uint8Array): Uint8Array {
else if(c <= 0x1000000) { L += 4; out.push(new Uint8Array([0xF8, (c-1) & 0xFF, ((c-1) >> 8) & 0xFF, ((c-1) >> 16) & 0xFF])); }
else if(c <= 0x100000000) { L += 5; out.push(new Uint8Array([0xFC, (c-1) & 0xFF, ((c-1) >> 8) & 0xFF, ((c-1) >> 16) & 0xFF, ((c-1) >>> 24) & 0xFF])); }
out.push(buf.slice(l, l + c)); L += c;
out.push(buf[subarray](l, l + c)); L += c;
frame[0] = 0;
frame[1] = L & 0xFF; frame[2] = (L >> 8) & 0xFF; frame[3] = (L >> 16) & 0xFF;
@ -361,9 +370,9 @@ function parse_old_storage(buf: Uint8Array, sst: string[], rsst: string[], v: 0|
case 8: ret = { t: "e", v: 0}; break; // "formula error" TODO: enumerate and map errors to csf equivalents
case 9: { // "rich text"
if(ridx > -1) ret = { t: "s", v: rsst[ridx] };
else throw new Error(`Unsupported cell type ${buf.slice(0,4)}`);
else throw new Error(`Unsupported cell type ${buf[subarray](0,4)}`);
} break;
default: throw new Error(`Unsupported cell type ${buf.slice(0,4)}`);
default: throw new Error(`Unsupported cell type ${buf[subarray](0,4)}`);
}
/* TODO: Some fields appear after the cell data */
@ -397,10 +406,10 @@ function parse_new_storage(buf: Uint8Array, sst: string[], rsst: string[]): Cell
case 8: ret = { t: "e", v: 0}; break; // "formula error" TODO: enumerate and map errors to csf equivalents
case 9: { // "rich text"
if(ridx > -1) ret = { t: "s", v: rsst[ridx] };
else throw new Error(`Unsupported cell type ${buf[1]} : ${flags & 0x1F} : ${buf.slice(0,4)}`);
else throw new Error(`Unsupported cell type ${buf[1]} : ${flags & 0x1F} : ${buf[subarray](0,4)}`);
} break;
case 10: ret = { t: "n", v: d128 }; break; // currency
default: throw new Error(`Unsupported cell type ${buf[1]} : ${flags & 0x1F} : ${buf.slice(0,4)}`);
default: throw new Error(`Unsupported cell type ${buf[1]} : ${flags & 0x1F} : ${buf[subarray](0,4)}`);
}
/* TODO: All styling fields appear after the cell data */
@ -420,7 +429,7 @@ function write_new_storage(cell: CellObject, sst: string[]): Uint8Array {
default: throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
/** Write a cell "old storage" (version 3) */
function write_old_storage(cell: CellObject, sst: string[]): Uint8Array {
@ -435,7 +444,7 @@ function write_old_storage(cell: CellObject, sst: string[]): Uint8Array {
default: throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
//<<export { write_new_storage, write_old_storage };
function parse_cell_storage(buf: Uint8Array, sst: string[], rsst: string[]): CellObject | void {
@ -532,8 +541,8 @@ function parse_TST_TileRowInfo(u8: Uint8Array, type: TileStorageType): TileRowIn
if(offsets.length != cnt) throw `Expected ${cnt} cells, found ${offsets.length}`;
var cells: Uint8Array[] = [];
for(C = 0; C < offsets.length - 1; ++C) cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C+1][1] * width);
if(offsets.length >= 1) cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
for(C = 0; C < offsets.length - 1; ++C) cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C+1][1] * width);
if(offsets.length >= 1) cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R, cells };
}
@ -571,7 +580,7 @@ function parse_TST_TableModelArchive(M: MessageSpace, root: IWAMessage, ws: Work
range.e.c = (varint_to_i32(pb[7][0].data) >>> 0) - 1;
if(range.e.c < 0) throw new Error(`Invalid col varint ${pb[7][0].data}`);
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
// .TST.DataStore
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
@ -590,9 +599,16 @@ function parse_TST_TableModelArchive(M: MessageSpace, root: IWAMessage, ws: Work
var _tile = parse_TST_Tile(M, ref);
_tile.data.forEach((row, R) => {
row.forEach((buf, C) => {
var addr = encode_cell({r:_R + R,c:C});
var res = parse_cell_storage(buf, sst, rsst);
if(res) ws[addr] = res;
if(res) {
if(dense) {
if(!ws[_R + R]) ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({r:_R + R,c:C});
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -617,9 +633,13 @@ function parse_TST_TableModelArchive(M: MessageSpace, root: IWAMessage, ws: Work
}
/** Parse .TST.TableInfoArchive (6000) */
function parse_TST_TableInfoArchive(M: MessageSpace, root: IWAMessage): WorkSheet {
function parse_TST_TableInfoArchive(M: MessageSpace, root: IWAMessage, opts?: ParsingOptions): WorkSheet {
var pb = parse_shallow(root.data);
var out: WorkSheet = { "!ref": "A1" };
// ESBuild #2375
var out: WorkSheet;
if(!opts?.dense) out = ({ "!ref": "A1" });
else out = ([] as any);
out["!ref"] = "A1";
var tableref = M[parse_TSP_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 ${mtype}`);
@ -632,7 +652,7 @@ interface NSheet {
sheets: WorkSheet[];
}
/** Parse .TN.SheetArchive (2) */
function parse_TN_SheetArchive(M: MessageSpace, root: IWAMessage): NSheet {
function parse_TN_SheetArchive(M: MessageSpace, root: IWAMessage, opts?: ParsingOptions): NSheet {
var pb = parse_shallow(root.data);
var out: NSheet = {
name: (pb[1]?.[0] ? u8str(pb[1][0].data) : ""),
@ -642,14 +662,14 @@ function parse_TN_SheetArchive(M: MessageSpace, root: IWAMessage): NSheet {
shapeoffs.forEach((off) => {
M[off].forEach((m: IWAMessage) => {
var mtype = varint_to_i32(m.meta[1][0].data);
if(mtype == 6000) out.sheets.push(parse_TST_TableInfoArchive(M, m));
if(mtype == 6000) out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
/** Parse .TN.DocumentArchive */
function parse_TN_DocumentArchive(M: MessageSpace, root: IWAMessage): WorkBook {
function parse_TN_DocumentArchive(M: MessageSpace, root: IWAMessage, opts?: ParsingOptions): WorkBook {
var out = book_new();
var pb = parse_shallow(root.data);
if(pb[2]?.[0]) throw new Error("Keynote presentations are not supported");
@ -667,7 +687,7 @@ function parse_TN_DocumentArchive(M: MessageSpace, root: IWAMessage): WorkBook {
M[off].forEach((m: IWAMessage) => {
var mtype = varint_to_i32(m.meta[1][0].data);
if(mtype == 2) {
var root = parse_TN_SheetArchive(M, m);
var root = parse_TN_SheetArchive(M, m, opts);
root.sheets.forEach((sheet, idx) => { book_append_sheet(out, sheet, idx == 0 ? root.name : root.name + "_" + idx, true); });
}
});
@ -678,7 +698,7 @@ function parse_TN_DocumentArchive(M: MessageSpace, root: IWAMessage): WorkBook {
}
/** Parse NUMBERS file */
function parse_numbers_iwa(cfb: CFB$Container): WorkBook {
function parse_numbers_iwa(cfb: CFB$Container, opts?: ParsingOptions ): WorkBook {
var M: MessageSpace = {}, indices: number[] = [];
cfb.FullPaths.forEach(p => { if(p.match(/\.iwpv2/)) throw new Error(`Unsupported password protection`); });
@ -706,7 +726,7 @@ function parse_numbers_iwa(cfb: CFB$Container): WorkBook {
});
});
if(!docroot) throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
//<<export { parse_numbers_iwa };
@ -784,7 +804,7 @@ function write_iwam(type: number, payload: Uint8Array): IWAMessage {
var USE_WIDE_ROWS = true;
/** Write NUMBERS workbook */
function write_numbers_iwa(wb: WorkBook, opts: any): CFB$Container {
function write_numbers_iwa(wb: WorkBook, opts?: WritingOptions): CFB$Container {
if(!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README");
/* TODO: support multiple worksheets, larger ranges, more data types, etc */

View File

@ -4156,8 +4156,11 @@ function buf_array()/*:BufArray*/ {
var endbuf = function ba_endbuf() {
if(!curbuf) return;
if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
if(curbuf.length > 0) bufs.push(curbuf);
// workaround for new Buffer(3).slice(0,0) bug in bun 0.1.3
if(curbuf.l) {
if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
if(curbuf.length > 0) bufs.push(curbuf);
}
curbuf = null;
};
@ -23063,6 +23066,9 @@ function write_ods(wb/*:any*/, opts/*:any*/) {
}
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if (typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined")
subarray = "slice";
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
}
@ -23181,7 +23187,7 @@ function write_varint49(v) {
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
@ -23217,22 +23223,22 @@ function parse_shallow(buf) {
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
res = buf[subarray](l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
@ -23274,7 +23280,7 @@ function parse_iwa_file(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));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
@ -23285,7 +23291,7 @@ function parse_iwa_file(buf) {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -23345,7 +23351,7 @@ function parse_snappy_chunk(type, buf) {
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
chunks.push(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
@ -23364,35 +23370,57 @@ function parse_snappy_chunk(type, buf) {
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;
}
var j = chunks.length - 1, off = offset;
while (j >= 0 && off >= chunks[j].length) {
off -= chunks[j].length;
--j;
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if (j < 0) {
if (off == 0)
off = chunks[j = 0].length;
else
throw new Error("Invalid offset beyond length");
}
if (length < off)
chunks.push(chunks[j][subarray](-off, -off + length));
else {
if (off > 0) {
chunks.push(chunks[j][subarray](-off));
length -= off;
}
++j;
while (length >= chunks[j].length) {
chunks.push(chunks[j]);
length -= chunks[j].length;
++j;
}
if (length)
chunks.push(chunks[j][subarray](0, length));
}
if (chunks.length > 100)
chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
if (chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0) != usz)
throw new Error("Unexpected length: ".concat(chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0), " != ").concat(usz));
return chunks;
}
function decompress_iwa_file(buf) {
if (Array.isArray(buf))
buf = new Uint8Array(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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if (l !== buf.length)
@ -23425,7 +23453,7 @@ function compress_iwa_file(buf) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
out.push(buf[subarray](l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
@ -23484,11 +23512,11 @@ function parse_old_storage(buf, sst, rsst, v) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23544,14 +23572,14 @@ function parse_new_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23583,7 +23611,7 @@ function write_new_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function write_old_storage(cell, sst) {
var out = new Uint8Array(32), dv = u8_to_dataview(out), l = 12, flags = 0;
@ -23613,7 +23641,7 @@ function write_old_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
@ -23695,9 +23723,9 @@ function parse_TST_TileRowInfo(u8, type) {
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C + 1][1] * width);
if (offsets.length >= 1)
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
@ -23738,6 +23766,7 @@ function parse_TST_TableModelArchive(M, root, ws) {
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
@ -23752,10 +23781,17 @@ function parse_TST_TableModelArchive(M, root, ws) {
var _tile = parse_TST_Tile(M, ref2);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
if (res) {
if (dense) {
if (!ws[_R + R])
ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({ r: _R + R, c: C });
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -23778,9 +23814,14 @@ function parse_TST_TableModelArchive(M, root, ws) {
});
}
}
function parse_TST_TableInfoArchive(M, root) {
function parse_TST_TableInfoArchive(M, root, opts) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var out;
if (!(opts == null ? void 0 : opts.dense))
out = { "!ref": "A1" };
else
out = [];
out["!ref"] = "A1";
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
@ -23788,7 +23829,7 @@ function parse_TST_TableInfoArchive(M, root) {
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
function parse_TN_SheetArchive(M, root, opts) {
var _a;
var pb = parse_shallow(root.data);
var out = {
@ -23800,12 +23841,12 @@ function parse_TN_SheetArchive(M, root) {
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));
out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
function parse_TN_DocumentArchive(M, root, opts) {
var _a;
var out = book_new();
var pb = parse_shallow(root.data);
@ -23816,7 +23857,7 @@ function parse_TN_DocumentArchive(M, root) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
var root2 = parse_TN_SheetArchive(M, m, opts);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
@ -23828,7 +23869,7 @@ function parse_TN_DocumentArchive(M, root) {
out.bookType = "numbers";
return out;
}
function parse_numbers_iwa(cfb) {
function parse_numbers_iwa(cfb, opts) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var M = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
@ -23874,7 +23915,7 @@ function parse_numbers_iwa(cfb) {
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
function write_tile_row(tri, data, SST, wide) {
var _a, _b;
@ -24388,10 +24429,10 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
if(safegetzipfile(zip, 'Index/Document.iwa')) {
if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support');
if(typeof parse_numbers_iwa != "undefined") {
if(zip.FileIndex) return parse_numbers_iwa(zip);
if(zip.FileIndex) return parse_numbers_iwa(zip, opts);
var _zip = CFB.utils.cfb_new();
zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); });
return parse_numbers_iwa(_zip);
return parse_numbers_iwa(_zip, opts);
}
throw new Error('Unsupported NUMBERS file');
}

137
xlsx.js generated
View File

@ -4076,8 +4076,11 @@ function buf_array() {
var endbuf = function ba_endbuf() {
if(!curbuf) return;
if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
if(curbuf.length > 0) bufs.push(curbuf);
// workaround for new Buffer(3).slice(0,0) bug in bun 0.1.3
if(curbuf.l) {
if(curbuf.length > curbuf.l) { curbuf = curbuf.slice(0, curbuf.l); curbuf.l = curbuf.length; }
if(curbuf.length > 0) bufs.push(curbuf);
}
curbuf = null;
};
@ -22953,6 +22956,9 @@ function write_ods(wb, opts) {
}
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if (typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined")
subarray = "slice";
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
}
@ -23071,7 +23077,7 @@ function write_varint49(v) {
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
@ -23107,22 +23113,22 @@ function parse_shallow(buf) {
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
res = buf[subarray](l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
@ -23164,7 +23170,7 @@ function parse_iwa_file(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));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
@ -23175,7 +23181,7 @@ function parse_iwa_file(buf) {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -23235,7 +23241,7 @@ function parse_snappy_chunk(type, buf) {
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
chunks.push(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
@ -23254,35 +23260,57 @@ function parse_snappy_chunk(type, buf) {
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;
}
var j = chunks.length - 1, off = offset;
while (j >= 0 && off >= chunks[j].length) {
off -= chunks[j].length;
--j;
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if (j < 0) {
if (off == 0)
off = chunks[j = 0].length;
else
throw new Error("Invalid offset beyond length");
}
if (length < off)
chunks.push(chunks[j][subarray](-off, -off + length));
else {
if (off > 0) {
chunks.push(chunks[j][subarray](-off));
length -= off;
}
++j;
while (length >= chunks[j].length) {
chunks.push(chunks[j]);
length -= chunks[j].length;
++j;
}
if (length)
chunks.push(chunks[j][subarray](0, length));
}
if (chunks.length > 100)
chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
if (chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0) != usz)
throw new Error("Unexpected length: ".concat(chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0), " != ").concat(usz));
return chunks;
}
function decompress_iwa_file(buf) {
if (Array.isArray(buf))
buf = new Uint8Array(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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if (l !== buf.length)
@ -23315,7 +23343,7 @@ function compress_iwa_file(buf) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
out.push(buf[subarray](l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
@ -23374,11 +23402,11 @@ function parse_old_storage(buf, sst, rsst, v) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23434,14 +23462,14 @@ function parse_new_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23473,7 +23501,7 @@ function write_new_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function write_old_storage(cell, sst) {
var out = new Uint8Array(32), dv = u8_to_dataview(out), l = 12, flags = 0;
@ -23503,7 +23531,7 @@ function write_old_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
@ -23585,9 +23613,9 @@ function parse_TST_TileRowInfo(u8, type) {
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C + 1][1] * width);
if (offsets.length >= 1)
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
@ -23628,6 +23656,7 @@ function parse_TST_TableModelArchive(M, root, ws) {
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
@ -23642,10 +23671,17 @@ function parse_TST_TableModelArchive(M, root, ws) {
var _tile = parse_TST_Tile(M, ref2);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
if (res) {
if (dense) {
if (!ws[_R + R])
ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({ r: _R + R, c: C });
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -23668,9 +23704,14 @@ function parse_TST_TableModelArchive(M, root, ws) {
});
}
}
function parse_TST_TableInfoArchive(M, root) {
function parse_TST_TableInfoArchive(M, root, opts) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var out;
if (!(opts == null ? void 0 : opts.dense))
out = { "!ref": "A1" };
else
out = [];
out["!ref"] = "A1";
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
@ -23678,7 +23719,7 @@ function parse_TST_TableInfoArchive(M, root) {
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
function parse_TN_SheetArchive(M, root, opts) {
var _a;
var pb = parse_shallow(root.data);
var out = {
@ -23690,12 +23731,12 @@ function parse_TN_SheetArchive(M, root) {
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));
out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
function parse_TN_DocumentArchive(M, root, opts) {
var _a;
var out = book_new();
var pb = parse_shallow(root.data);
@ -23706,7 +23747,7 @@ function parse_TN_DocumentArchive(M, root) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
var root2 = parse_TN_SheetArchive(M, m, opts);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
@ -23718,7 +23759,7 @@ function parse_TN_DocumentArchive(M, root) {
out.bookType = "numbers";
return out;
}
function parse_numbers_iwa(cfb) {
function parse_numbers_iwa(cfb, opts) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var M = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
@ -23764,7 +23805,7 @@ function parse_numbers_iwa(cfb) {
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
function write_tile_row(tri, data, SST, wide) {
var _a, _b;
@ -24278,10 +24319,10 @@ function parse_zip(zip, opts) {
if(safegetzipfile(zip, 'Index/Document.iwa')) {
if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support');
if(typeof parse_numbers_iwa != "undefined") {
if(zip.FileIndex) return parse_numbers_iwa(zip);
if(zip.FileIndex) return parse_numbers_iwa(zip, opts);
var _zip = CFB.utils.cfb_new();
zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); });
return parse_numbers_iwa(_zip);
return parse_numbers_iwa(_zip, opts);
}
throw new Error('Unsupported NUMBERS file');
}

130
xlsx.mjs generated
View File

@ -23061,6 +23061,9 @@ function write_ods(wb/*:any*/, opts/*:any*/) {
}
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var subarray = typeof Uint8Array !== "undefined" && typeof Uint8Array.prototype.subarray != "undefined" ? "subarray" : "slice";
if (typeof Buffer !== "undefined" && typeof Buffer.prototype.subarray == "undefined")
subarray = "slice";
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
}
@ -23179,7 +23182,7 @@ function write_varint49(v) {
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
return usz[subarray](0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
@ -23215,22 +23218,22 @@ function parse_shallow(buf) {
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
res = buf[subarray](l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
res = buf[subarray](ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
@ -23272,7 +23275,7 @@ function parse_iwa_file(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));
var ai = parse_shallow(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
@ -23283,7 +23286,7 @@ function parse_iwa_file(buf) {
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
data: buf[subarray](ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
@ -23343,7 +23346,7 @@ function parse_snappy_chunk(type, buf) {
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
chunks.push(buf[subarray](ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
@ -23362,35 +23365,57 @@ function parse_snappy_chunk(type, buf) {
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;
}
var j = chunks.length - 1, off = offset;
while (j >= 0 && off >= chunks[j].length) {
off -= chunks[j].length;
--j;
}
chunks.push(chunks[0].slice(-offset, -offset + length));
if (j < 0) {
if (off == 0)
off = chunks[j = 0].length;
else
throw new Error("Invalid offset beyond length");
}
if (length < off)
chunks.push(chunks[j][subarray](-off, -off + length));
else {
if (off > 0) {
chunks.push(chunks[j][subarray](-off));
length -= off;
}
++j;
while (length >= chunks[j].length) {
chunks.push(chunks[j]);
length -= chunks[j].length;
++j;
}
if (length)
chunks.push(chunks[j][subarray](0, length));
}
if (chunks.length > 100)
chunks = [u8concat(chunks)];
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
if (chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0) != usz)
throw new Error("Unexpected length: ".concat(chunks.reduce(function(acc, u8) {
return acc + u8.length;
}, 0), " != ").concat(usz));
return chunks;
}
function decompress_iwa_file(buf) {
if (Array.isArray(buf))
buf = new Uint8Array(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)));
out.push.apply(out, parse_snappy_chunk(t, buf[subarray](l, l + len)));
l += len;
}
if (l !== buf.length)
@ -23423,7 +23448,7 @@ function compress_iwa_file(buf) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
out.push(buf[subarray](l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
@ -23482,11 +23507,11 @@ function parse_old_storage(buf, sst, rsst, v) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23542,14 +23567,14 @@ function parse_new_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf[subarray](0, 4)));
}
return ret;
}
@ -23581,7 +23606,7 @@ function write_new_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(8, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function write_old_storage(cell, sst) {
var out = new Uint8Array(32), dv = u8_to_dataview(out), l = 12, flags = 0;
@ -23611,7 +23636,7 @@ function write_old_storage(cell, sst) {
throw "unsupported cell type " + cell.t;
}
dv.setUint32(4, flags, true);
return out.slice(0, l);
return out[subarray](0, l);
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
@ -23693,9 +23718,9 @@ function parse_TST_TileRowInfo(u8, type) {
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[C][0]] = used_storage[subarray](offsets[C][1] * width, offsets[C + 1][1] * width);
if (offsets.length >= 1)
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage[subarray](offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
@ -23736,6 +23761,7 @@ function parse_TST_TableModelArchive(M, root, ws) {
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
var dense = Array.isArray(ws);
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
@ -23750,10 +23776,17 @@ function parse_TST_TableModelArchive(M, root, ws) {
var _tile = parse_TST_Tile(M, ref2);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
if (res) {
if (dense) {
if (!ws[_R + R])
ws[_R + R] = [];
ws[_R + R][C] = res;
} else {
var addr = encode_cell({ r: _R + R, c: C });
ws[addr] = res;
}
}
});
});
_R += _tile.nrows;
@ -23776,9 +23809,14 @@ function parse_TST_TableModelArchive(M, root, ws) {
});
}
}
function parse_TST_TableInfoArchive(M, root) {
function parse_TST_TableInfoArchive(M, root, opts) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var out;
if (!(opts == null ? void 0 : opts.dense))
out = { "!ref": "A1" };
else
out = [];
out["!ref"] = "A1";
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
@ -23786,7 +23824,7 @@ function parse_TST_TableInfoArchive(M, root) {
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
function parse_TN_SheetArchive(M, root, opts) {
var _a;
var pb = parse_shallow(root.data);
var out = {
@ -23798,12 +23836,12 @@ function parse_TN_SheetArchive(M, root) {
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));
out.sheets.push(parse_TST_TableInfoArchive(M, m, opts));
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
function parse_TN_DocumentArchive(M, root, opts) {
var _a;
var out = book_new();
var pb = parse_shallow(root.data);
@ -23814,7 +23852,7 @@ function parse_TN_DocumentArchive(M, root) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
var root2 = parse_TN_SheetArchive(M, m, opts);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
@ -23826,7 +23864,7 @@ function parse_TN_DocumentArchive(M, root) {
out.bookType = "numbers";
return out;
}
function parse_numbers_iwa(cfb) {
function parse_numbers_iwa(cfb, opts) {
var _a, _b, _c, _d, _e, _f, _g, _h;
var M = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
@ -23872,7 +23910,7 @@ function parse_numbers_iwa(cfb) {
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(M, docroot);
return parse_TN_DocumentArchive(M, docroot, opts);
}
function write_tile_row(tri, data, SST, wide) {
var _a, _b;
@ -24386,10 +24424,10 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
if(safegetzipfile(zip, 'Index/Document.iwa')) {
if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support');
if(typeof parse_numbers_iwa != "undefined") {
if(zip.FileIndex) return parse_numbers_iwa(zip);
if(zip.FileIndex) return parse_numbers_iwa(zip, opts);
var _zip = CFB.utils.cfb_new();
zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); });
return parse_numbers_iwa(_zip);
return parse_numbers_iwa(_zip, opts);
}
throw new Error('Unsupported NUMBERS file');
}