book_append_sheet rolling names

pull/2638/head v0.18.4+deno
SheetJS 9 months ago
parent a5b387716c
commit 2f274dd48c
  1. 1
      .eslintrc
  2. 20
      README.md
  3. 3
      bits/31_rels.js
  4. 11
      bits/51_xlsxmeta.js
  5. 277
      bits/83_numbers.js
  6. 12
      bits/90_utils.js
  7. 20
      docbits/25_manip.md
  8. 11
      misc/51_xlsxmeta.js
  9. 20
      misc/docs/README.md
  10. 7
      modules/.gitignore
  11. 11
      modules/51_xlsxmeta.js
  12. 11
      modules/51_xlsxmeta.ts
  13. 277
      modules/83_numbers.js
  14. 274
      modules/83_numbers.ts
  15. 7
      modules/Makefile
  16. 4
      modules/numbers_to_csv.node.ts
  17. 53
      modules/reframe.node.ts
  18. 88
      modules/src/cell.ts
  19. 105
      modules/src/frame.ts
  20. 40
      modules/src/iwa.ts
  21. 228
      modules/src/numbers.ts
  22. 118
      modules/src/proto.ts
  23. 1
      modules/src/types.ts
  24. 56
      modules/src/util.ts
  25. 2
      package.json
  26. 2
      types/index.d.ts
  27. 303
      xlsx.flow.js
  28. 303
      xlsx.js
  29. 303
      xlsx.mjs

@ -13,6 +13,7 @@
"no-bitwise": 0,
"no-console": 0,
"no-control-regex": 0,
"no-unused-vars": 1,
"no-empty": 0,
"no-trailing-spaces": 2,
"no-use-before-define": [ 1, {

@ -1388,7 +1388,25 @@ XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);
The `book_append_sheet` utility function appends a worksheet to the workbook.
The third argument specifies the desired worksheet name. Multiple worksheets can
be added to a workbook by calling the function multiple times.
be added to a workbook by calling the function multiple times. If the worksheet
name is already used in the workbook, it will throw an error.
_Append a Worksheet to a Workbook and find a unique name_
```js
var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);
```
If the fourth argument is `true`, the function will start with the specified
worksheet name. If the sheet name exists in the workbook, a new worksheet name
will be chosen by finding the name stem and incrementing the counter:
```js
XLSX.utils.book_append_sheet(workbook, sheetA, "Sheet2", true); // Sheet2
XLSX.utils.book_append_sheet(workbook, sheetB, "Sheet2", true); // Sheet3
XLSX.utils.book_append_sheet(workbook, sheetC, "Sheet2", true); // Sheet4
XLSX.utils.book_append_sheet(workbook, sheetD, "Sheet2", true); // Sheet5
```
_List the Worksheet names in tab order_

@ -76,7 +76,6 @@ function write_rels(rels)/*:string*/ {
return o.join("");
}
var RELS_EXTERN = [RELS.HLINK, RELS.XPATH, RELS.XMISS];
function add_rels(rels, rId/*:number*/, f, type, relobj, targetmode/*:?string*/)/*:number*/ {
if(!relobj) relobj = {};
if(!rels['!id']) rels['!id'] = {};
@ -87,7 +86,7 @@ function add_rels(rels, rId/*:number*/, f, type, relobj, targetmode/*:?string*/)
relobj.Type = type;
relobj.Target = f;
if(targetmode) relobj.TargetMode = targetmode;
else if(RELS_EXTERN.indexOf(relobj.Type) > -1) relobj.TargetMode = "External";
else if([RELS.HLINK, RELS.XPATH, RELS.XMISS].indexOf(relobj.Type) > -1) relobj.TargetMode = "External";
if(rels['!id'][relobj.Id]) throw new Error("Cannot rewrite rId " + rId);
rels['!id'][relobj.Id] = relobj;
rels[('/' + relobj.Target).replace("//","/")] = relobj;

@ -4,6 +4,7 @@ function parse_xlmeta_xml(data, name, opts) {
return out;
var pass = false;
var metatype = 2;
var lastmeta;
data.replace(tagregex, function(x) {
var y = parsexmltag(x);
switch (strip_ns(y[0])) {
@ -21,6 +22,9 @@ function parse_xlmeta_xml(data, name, opts) {
case "</metadataType>":
break;
case "<futureMetadata":
for (var j = 0; j < out.Types.length; ++j)
if (out.Types[j].name == y.name)
lastmeta = out.Types[j];
break;
case "</futureMetadata>":
break;
@ -59,6 +63,13 @@ function parse_xlmeta_xml(data, name, opts) {
case "</ext>":
pass = false;
break;
case "<rvb":
if (!lastmeta)
break;
if (!lastmeta.offsets)
lastmeta.offsets = [];
lastmeta.offsets.push(+y.i);
break;
default:
if (!pass && opts.WTF)
throw new Error("unrecognized " + y[0] + " in metadata");

@ -1,11 +1,11 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var u8_to_dataview = function(array) {
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
};
var u8str = function(u8) {
}
function u8str(u8) {
return typeof TextDecoder != "undefined" ? new TextDecoder().decode(u8) : utf8read(a2s(u8));
};
var u8concat = function(u8a) {
}
function u8concat(u8a) {
var len = u8a.reduce(function(acc, x) {
return acc + x.length;
}, 0);
@ -16,19 +16,19 @@ var u8concat = function(u8a) {
off += u8.length;
});
return out;
};
var popcnt = function(x) {
}
function popcnt(x) {
x -= x >> 1 & 1431655765;
x = (x & 858993459) + (x >> 2 & 858993459);
return (x + (x >> 4) & 252645135) * 16843009 >>> 24;
};
var readDecimal128LE = function(buf, offset) {
}
function readDecimal128LE(buf, offset) {
var exp = (buf[offset + 15] & 127) << 7 | buf[offset + 14] >> 1;
var mantissa = buf[offset + 14] & 1;
for (var j = offset + 13; j >= offset; --j)
mantissa = mantissa * 256 + buf[j];
return (buf[offset + 15] & 128 ? -mantissa : mantissa) * Math.pow(10, exp - 6176);
};
}
function parse_varint49(buf, ptr) {
var l = ptr ? ptr[0] : 0;
var usz = buf[l] & 127;
@ -60,6 +60,43 @@ function parse_varint49(buf, ptr) {
ptr[0] = l;
return usz;
}
function write_varint49(v) {
var usz = new Uint8Array(7);
usz[0] = v & 127;
var L = 1;
sz:
if (v > 127) {
usz[L - 1] |= 128;
usz[L] = v >> 7 & 127;
++L;
if (v <= 16383)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 14 & 127;
++L;
if (v <= 2097151)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 21 & 127;
++L;
if (v <= 268435455)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 256 >>> 21 & 127;
++L;
if (v <= 34359738367)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 65536 >>> 21 & 127;
++L;
if (v <= 4398046511103)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
varint:
@ -228,7 +265,7 @@ function parse_snappy_chunk(type, buf) {
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
}
function deframe(buf) {
function decompress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
@ -242,17 +279,52 @@ function deframe(buf) {
throw new Error("data is not a valid framed stream!");
return u8concat(out);
}
function parse_old_storage(buf, sst, rsst) {
function compress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
var c = Math.min(buf.length - l, 268435455);
var frame = new Uint8Array(4);
out.push(frame);
var usz = write_varint49(c);
var L = usz.length;
out.push(usz);
if (c <= 60) {
L++;
out.push(new Uint8Array([c - 1 << 2]));
} else if (c <= 256) {
L += 2;
out.push(new Uint8Array([240, c - 1 & 255]));
} else if (c <= 65536) {
L += 3;
out.push(new Uint8Array([244, c - 1 & 255, c - 1 >> 8 & 255]));
} else if (c <= 16777216) {
L += 4;
out.push(new Uint8Array([248, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255]));
} else if (c <= 4294967296) {
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));
L += c;
frame[0] = 0;
frame[1] = L & 255;
frame[2] = L >> 8 & 255;
frame[3] = L >> 16 & 255;
l += c;
}
return u8concat(out);
}
function parse_old_storage(buf, sst, rsst, v) {
var dv = u8_to_dataview(buf);
var ctype = buf[buf[0] == 4 ? 1 : 2];
var flags = dv.getUint32(4, true);
var data_offset = 12 + popcnt(flags & 3470) * 4;
var data_offset = (v > 1 ? 12 : 8) + popcnt(flags & (v > 1 ? 3470 : 398)) * 4;
var ridx = -1, sidx = -1, ieee = NaN, dt = new Date(2001, 0, 1);
if (flags & 512) {
ridx = dv.getUint32(data_offset, true);
data_offset += 4;
}
data_offset += popcnt(flags & 12288) * 4;
data_offset += popcnt(flags & (v > 1 ? 12288 : 4096)) * 4;
if (flags & 16) {
sidx = dv.getUint32(data_offset, true);
data_offset += 4;
@ -266,7 +338,7 @@ function parse_old_storage(buf, sst, rsst) {
data_offset += 8;
}
var ret;
switch (ctype) {
switch (buf[2]) {
case 0:
break;
case 2:
@ -306,7 +378,6 @@ function parse_old_storage(buf, sst, rsst) {
}
function parse_storage(buf, sst, rsst) {
var dv = u8_to_dataview(buf);
var ctype = buf[1];
var flags = dv.getUint32(8, true);
var data_offset = 12;
var ridx = -1, sidx = -1, d128 = NaN, ieee = NaN, dt = new Date(2001, 0, 1);
@ -331,7 +402,7 @@ function parse_storage(buf, sst, rsst) {
data_offset += 4;
}
var ret;
switch (ctype) {
switch (buf[1]) {
case 0:
break;
case 2:
@ -357,22 +428,24 @@ function parse_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
}
return ret;
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
case 0:
case 1:
case 2:
case 3:
case 4:
return parse_old_storage(buf, sst, rsst);
return parse_old_storage(buf, sst, rsst, buf[0]);
case 5:
return parse_storage(buf, sst, rsst);
default:
@ -413,47 +486,57 @@ function parse_TST_TableDataList(M, root) {
});
return data;
}
function parse_TST_TileRowInfo(u8) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
function parse_TST_TileRowInfo(u8, type) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
var pb = parse_shallow(u8);
var R = varint_to_i32(pb[1][0].data) >>> 0;
var pre_bnc = (_b = (_a = pb[3]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data;
var pre_bnc_offsets = ((_d = (_c = pb[4]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && u8_to_dataview(pb[4][0].data);
var storage = (_f = (_e = pb[6]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data;
var storage_offsets = ((_h = (_g = pb[7]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data) && u8_to_dataview(pb[7][0].data);
var wide_offsets = ((_j = (_i = pb[8]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && varint_to_i32(pb[8][0].data) > 0 || false;
var cnt = varint_to_i32(pb[2][0].data) >>> 0;
var wide_offsets = ((_b = (_a = pb[8]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data) && varint_to_i32(pb[8][0].data) > 0 || false;
var used_storage_u8, used_storage;
if (((_d = (_c = pb[7]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && type != 0) {
used_storage_u8 = (_f = (_e = pb[7]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data;
used_storage = (_h = (_g = pb[6]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data;
} else if (((_j = (_i = pb[4]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && type != 1) {
used_storage_u8 = (_l = (_k = pb[4]) == null ? void 0 : _k[0]) == null ? void 0 : _l.data;
used_storage = (_n = (_m = pb[3]) == null ? void 0 : _m[0]) == null ? void 0 : _n.data;
} else
throw "NUMBERS Tile missing ".concat(type, " cell storage");
var width = wide_offsets ? 4 : 1;
var cells = [];
var off = 0;
for (var C = 0; C < pre_bnc_offsets.byteLength / 2; ++C) {
if (storage && storage_offsets) {
off = storage_offsets.getUint16(C * 2, true) * width;
if (off < storage.length) {
cells[C] = storage.subarray(off, storage_offsets.getUint16(C * 2 + 2, true) * width);
continue;
}
}
if (pre_bnc && pre_bnc_offsets) {
off = pre_bnc_offsets.getUint16(C * 2, true) * width;
if (off < pre_bnc.length)
cells[C] = pre_bnc.subarray(off, pre_bnc_offsets.getUint16(C * 2 + 2, true) * width);
}
var used_storage_offsets = u8_to_dataview(used_storage_u8);
var offsets = [];
for (var C = 0; C < used_storage_u8.length / 2; ++C) {
var off = used_storage_offsets.getUint16(C * 2, true);
if (off < 65535)
offsets.push([C, off]);
}
if (offsets.length != cnt)
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[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
var _a;
var pb = parse_shallow(root.data);
var ri = mappa(pb[5], parse_TST_TileRowInfo);
return ri.reduce(function(acc, x) {
if (!acc[x.R])
acc[x.R] = [];
x.cells.forEach(function(cell, C) {
if (acc[x.R][C])
throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C));
acc[x.R][C] = cell;
});
return acc;
}, []);
var storage = ((_a = pb == null ? void 0 : pb[7]) == null ? void 0 : _a[0]) ? varint_to_i32(pb[7][0].data) >>> 0 > 0 ? 1 : 0 : -1;
var ri = mappa(pb[5], function(u8) {
return parse_TST_TileRowInfo(u8, storage);
});
return {
nrows: varint_to_i32(pb[4][0].data) >>> 0,
data: ri.reduce(function(acc, x) {
if (!acc[x.R])
acc[x.R] = [];
x.cells.forEach(function(cell, C) {
if (acc[x.R][C])
throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C));
acc[x.R][C] = cell;
});
return acc;
}, [])
};
}
function parse_TST_TableModelArchive(M, root, ws) {
var _a;
@ -466,33 +549,28 @@ 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 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]) : [];
{
var tile = parse_shallow(store[3][0].data);
var tiles = [];
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_TSP_Reference(tl[2][0].data)][0];
var mtype = varint_to_i32(ref.meta[1][0].data);
if (mtype != 6002)
throw new Error("6001 unexpected reference to ".concat(mtype));
tiles.push({ id: varint_to_i32(tl[1][0].data), ref: parse_TST_Tile(M, ref) });
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]) : [];
var tile = parse_shallow(store[3][0].data);
var _R = 0;
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_TSP_Reference(tl[2][0].data)][0];
var mtype = varint_to_i32(ref.meta[1][0].data);
if (mtype != 6002)
throw new Error("6001 unexpected reference to ".concat(mtype));
var _tile = parse_TST_Tile(M, ref);
_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;
});
tiles.forEach(function(tile2) {
tile2.ref.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
});
});
});
}
}
});
_R += _tile.nrows;
});
}
function parse_TST_TableInfoArchive(M, root) {
var pb = parse_shallow(root.data);
@ -530,8 +608,8 @@ function parse_TN_DocumentArchive(M, root) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
root2.sheets.forEach(function(sheet) {
book_append_sheet(out, sheet, root2.name);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
}
});
@ -541,7 +619,8 @@ function parse_TN_DocumentArchive(M, root) {
return out;
}
function parse_numbers_iwa(cfb) {
var out = [];
var _a, _b, _c, _d;
var out = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
if (p.match(/\.iwpv2/))
throw new Error("Unsupported password protection");
@ -551,7 +630,7 @@ function parse_numbers_iwa(cfb) {
return;
var o;
try {
o = deframe(s.content);
o = decompress_iwa_file(s.content);
} catch (e) {
return console.log("?? " + s.content.length + " " + (e.message || e));
}
@ -562,23 +641,25 @@ function parse_numbers_iwa(cfb) {
return console.log("## " + (e.message || e));
}
packets.forEach(function(packet) {
out[+packet.id] = packet.messages;
out[packet.id] = packet.messages;
indices.push(packet.id);
});
});
if (!out.length)
if (!indices.length)
throw new Error("File has no messages");
var docroot;
out.forEach(function(iwams) {
iwams.forEach(function(iwam) {
var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0;
if (mtype == 1) {
if (!docroot)
docroot = iwam;
else
throw new Error("Document has multiple roots");
}
var docroot = ((_d = (_c = (_b = (_a = out == null ? void 0 : out[1]) == null ? void 0 : _a[0]) == null ? void 0 : _b.meta) == null ? void 0 : _c[1]) == null ? void 0 : _d[0].data) && varint_to_i32(out[1][0].meta[1][0].data) == 1 && out[1][0];
if (!docroot)
indices.forEach(function(idx) {
out[idx].forEach(function(iwam) {
var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0;
if (mtype == 1) {
if (!docroot)
docroot = iwam;
else
throw new Error("Document has multiple roots");
}
});
});
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(out, docroot);

@ -287,14 +287,22 @@ function book_new()/*:Workbook*/ {
}
/* add a worksheet to the end of a given workbook */
function book_append_sheet(wb/*:Workbook*/, ws/*:Worksheet*/, name/*:?string*/) {
if(!name) for(var i = 1; i <= 0xFFFF; ++i, name = undefined) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
function book_append_sheet(wb/*:Workbook*/, ws/*:Worksheet*/, name/*:?string*/, roll/*:?boolean*/)/*:string*/ {
var i = 1;
if(!name) for(; i <= 0xFFFF; ++i, name = undefined) if(wb.SheetNames.indexOf(name = "Sheet" + i) == -1) break;
if(!name || wb.SheetNames.length >= 0xFFFF) throw new Error("Too many worksheets");
if(roll && wb.SheetNames.indexOf(name) >= 0) {
var m = name.match(/(^.*?)(\d+)$/);
i = m && +m[2] || 0;
var root = m && m[1] || name;
for(++i; i <= 0xFFFF; ++i) if(wb.SheetNames.indexOf(name = root + i) == -1) break;
}
check_ws_name(name);
if(wb.SheetNames.indexOf(name) >= 0) throw new Error("Worksheet with name |" + name + "| already exists!");
wb.SheetNames.push(name);
wb.Sheets[name] = ws;
return name;
}
/* set sheet visibility (visible/hidden/very hidden) */

@ -16,7 +16,25 @@ XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);
The `book_append_sheet` utility function appends a worksheet to the workbook.
The third argument specifies the desired worksheet name. Multiple worksheets can
be added to a workbook by calling the function multiple times.
be added to a workbook by calling the function multiple times. If the worksheet
name is already used in the workbook, it will throw an error.
_Append a Worksheet to a Workbook and find a unique name_
```js
var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);
```
If the fourth argument is `true`, the function will start with the specified
worksheet name. If the sheet name exists in the workbook, a new worksheet name
will be chosen by finding the name stem and incrementing the counter:
```js
XLSX.utils.book_append_sheet(workbook, sheetA, "Sheet2", true); // Sheet2
XLSX.utils.book_append_sheet(workbook, sheetB, "Sheet2", true); // Sheet3
XLSX.utils.book_append_sheet(workbook, sheetC, "Sheet2", true); // Sheet4
XLSX.utils.book_append_sheet(workbook, sheetD, "Sheet2", true); // Sheet5
```
_List the Worksheet names in tab order_

@ -4,6 +4,7 @@ function parse_xlmeta_xml(data, name, opts) {
return out;
var pass = false;
var metatype = 2;
var lastmeta;
data.replace(tagregex, function(x) {
var y = parsexmltag(x);
switch (strip_ns(y[0])) {
@ -21,6 +22,9 @@ function parse_xlmeta_xml(data, name, opts) {
case "</metadataType>":
break;
case "<futureMetadata":
for (var j = 0; j < out.Types.length; ++j)
if (out.Types[j].name == y.name)
lastmeta = out.Types[j];
break;
case "</futureMetadata>":
break;
@ -59,6 +63,13 @@ function parse_xlmeta_xml(data, name, opts) {
case "</ext>":
pass = false;
break;
case "<rvb":
if (!lastmeta)
break;
if (!lastmeta.offsets)
lastmeta.offsets = [];
lastmeta.offsets.push(+y.i);
break;
default:
if (!pass && opts.WTF)
throw new Error("unrecognized " + y[0] + " in metadata");

@ -1313,7 +1313,25 @@ XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);
The `book_append_sheet` utility function appends a worksheet to the workbook.
The third argument specifies the desired worksheet name. Multiple worksheets can
be added to a workbook by calling the function multiple times.
be added to a workbook by calling the function multiple times. If the worksheet
name is already used in the workbook, it will throw an error.
_Append a Worksheet to a Workbook and find a unique name_
```js
var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);
```
If the fourth argument is `true`, the function will start with the specified
worksheet name. If the sheet name exists in the workbook, a new worksheet name
will be chosen by finding the name stem and incrementing the counter:
```js
XLSX.utils.book_append_sheet(workbook, sheetA, "Sheet2", true); // Sheet2
XLSX.utils.book_append_sheet(workbook, sheetB, "Sheet2", true); // Sheet3
XLSX.utils.book_append_sheet(workbook, sheetC, "Sheet2", true); // Sheet4
XLSX.utils.book_append_sheet(workbook, sheetD, "Sheet2", true); // Sheet5
```
_List the Worksheet names in tab order_

@ -1,2 +1,7 @@
test_files
numbers_to_csv.node.js
*.node.js
*.[Pp][Rr][Oo][Tt][Oo]
*.[Ii][Ww][Aa]
*.[Jj][Pp][Gg]
*.[Pp][Ll][Ii][Ss][Tt]
DocumentIdentifier

@ -4,6 +4,7 @@ function parse_xlmeta_xml(data, name, opts) {
return out;
var pass = false;
var metatype = 2;
var lastmeta;
data.replace(tagregex, function(x) {
var y = parsexmltag(x);
switch (strip_ns(y[0])) {
@ -21,6 +22,9 @@ function parse_xlmeta_xml(data, name, opts) {
case "</metadataType>":
break;
case "<futureMetadata":
for (var j = 0; j < out.Types.length; ++j)
if (out.Types[j].name == y.name)
lastmeta = out.Types[j];
break;
case "</futureMetadata>":
break;
@ -59,6 +63,13 @@ function parse_xlmeta_xml(data, name, opts) {
case "</ext>":
pass = false;
break;
case "<rvb":
if (!lastmeta)
break;
if (!lastmeta.offsets)
lastmeta.offsets = [];
lastmeta.offsets.push(+y.i);
break;
default:
if (!pass && opts.WTF)
throw new Error("unrecognized " + y[0] + " in metadata");

@ -6,6 +6,7 @@ function parse_xlmeta_xml(data: string, name: string, opts?: ParseXLMetaOptions)
if(!data) return out;
var pass = false;
var metatype: 0 | 1 | 2 = 2;
var lastmeta: XLMDT;
data.replace(tagregex, (x: string/*, idx: number*/) => {
var y: any = parsexmltag(x);
@ -25,7 +26,9 @@ function parse_xlmeta_xml(data: string, name: string, opts?: ParseXLMetaOptions)
case '</metadataType>': break;
/* 18.9.4 */
case '<futureMetadata': break;
case '<futureMetadata':
for(var j = 0; j < out.Types.length; ++j) if(out.Types[j].name == y.name) lastmeta = out.Types[j];
break;
case '</futureMetadata>': break;
/* 18.9.1 */
@ -54,6 +57,12 @@ function parse_xlmeta_xml(data: string, name: string, opts?: ParseXLMetaOptions)
case '<ext': pass=true; break; //TODO: check with versions of excel
case '</ext>': pass=false; break;
case '<rvb':
if(!lastmeta) break;
if(!lastmeta.offsets) lastmeta.offsets = [];
lastmeta.offsets.push(+y.i);
break;
default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in metadata');
}
return x;

@ -1,11 +1,11 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
var u8_to_dataview = function(array) {
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
};
var u8str = function(u8) {
}
function u8str(u8) {
return typeof TextDecoder != "undefined" ? new TextDecoder().decode(u8) : utf8read(a2s(u8));
};
var u8concat = function(u8a) {
}
function u8concat(u8a) {
var len = u8a.reduce(function(acc, x) {
return acc + x.length;
}, 0);
@ -16,19 +16,19 @@ var u8concat = function(u8a) {
off += u8.length;
});
return out;
};
var popcnt = function(x) {
}
function popcnt(x) {
x -= x >> 1 & 1431655765;
x = (x & 858993459) + (x >> 2 & 858993459);
return (x + (x >> 4) & 252645135) * 16843009 >>> 24;
};
var readDecimal128LE = function(buf, offset) {
}
function readDecimal128LE(buf, offset) {
var exp = (buf[offset + 15] & 127) << 7 | buf[offset + 14] >> 1;
var mantissa = buf[offset + 14] & 1;
for (var j = offset + 13; j >= offset; --j)
mantissa = mantissa * 256 + buf[j];
return (buf[offset + 15] & 128 ? -mantissa : mantissa) * Math.pow(10, exp - 6176);
};
}
function parse_varint49(buf, ptr) {
var l = ptr ? ptr[0] : 0;
var usz = buf[l] & 127;
@ -60,6 +60,43 @@ function parse_varint49(buf, ptr) {
ptr[0] = l;
return usz;
}
function write_varint49(v) {
var usz = new Uint8Array(7);
usz[0] = v & 127;
var L = 1;
sz:
if (v > 127) {
usz[L - 1] |= 128;
usz[L] = v >> 7 & 127;
++L;
if (v <= 16383)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 14 & 127;
++L;
if (v <= 2097151)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 21 & 127;
++L;
if (v <= 268435455)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 256 >>> 21 & 127;
++L;
if (v <= 34359738367)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 65536 >>> 21 & 127;
++L;
if (v <= 4398046511103)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
varint:
@ -228,7 +265,7 @@ function parse_snappy_chunk(type, buf) {
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
}
function deframe(buf) {
function decompress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
@ -242,17 +279,52 @@ function deframe(buf) {
throw new Error("data is not a valid framed stream!");
return u8concat(out);
}
function parse_old_storage(buf, sst, rsst) {
function compress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
var c = Math.min(buf.length - l, 268435455);
var frame = new Uint8Array(4);
out.push(frame);
var usz = write_varint49(c);
var L = usz.length;
out.push(usz);
if (c <= 60) {
L++;
out.push(new Uint8Array([c - 1 << 2]));
} else if (c <= 256) {
L += 2;
out.push(new Uint8Array([240, c - 1 & 255]));
} else if (c <= 65536) {
L += 3;
out.push(new Uint8Array([244, c - 1 & 255, c - 1 >> 8 & 255]));
} else if (c <= 16777216) {
L += 4;
out.push(new Uint8Array([248, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255]));
} else if (c <= 4294967296) {
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));
L += c;
frame[0] = 0;
frame[1] = L & 255;
frame[2] = L >> 8 & 255;
frame[3] = L >> 16 & 255;
l += c;
}
return u8concat(out);
}
function parse_old_storage(buf, sst, rsst, v) {
var dv = u8_to_dataview(buf);
var ctype = buf[buf[0] == 4 ? 1 : 2];
var flags = dv.getUint32(4, true);
var data_offset = 12 + popcnt(flags & 3470) * 4;
var data_offset = (v > 1 ? 12 : 8) + popcnt(flags & (v > 1 ? 3470 : 398)) * 4;
var ridx = -1, sidx = -1, ieee = NaN, dt = new Date(2001, 0, 1);
if (flags & 512) {
ridx = dv.getUint32(data_offset, true);
data_offset += 4;
}
data_offset += popcnt(flags & 12288) * 4;
data_offset += popcnt(flags & (v > 1 ? 12288 : 4096)) * 4;
if (flags & 16) {
sidx = dv.getUint32(data_offset, true);
data_offset += 4;
@ -266,7 +338,7 @@ function parse_old_storage(buf, sst, rsst) {
data_offset += 8;
}
var ret;
switch (ctype) {
switch (buf[2]) {
case 0:
break;
case 2:
@ -306,7 +378,6 @@ function parse_old_storage(buf, sst, rsst) {
}
function parse_storage(buf, sst, rsst) {
var dv = u8_to_dataview(buf);
var ctype = buf[1];
var flags = dv.getUint32(8, true);
var data_offset = 12;
var ridx = -1, sidx = -1, d128 = NaN, ieee = NaN, dt = new Date(2001, 0, 1);
@ -331,7 +402,7 @@ function parse_storage(buf, sst, rsst) {
data_offset += 4;
}
var ret;
switch (ctype) {
switch (buf[1]) {
case 0:
break;
case 2:
@ -357,22 +428,24 @@ function parse_storage(buf, sst, rsst) {
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
throw new Error("Unsupported cell type ".concat(ctype, " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
}
return ret;
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
case 0:
case 1:
case 2:
case 3:
case 4:
return parse_old_storage(buf, sst, rsst);
return parse_old_storage(buf, sst, rsst, buf[0]);
case 5:
return parse_storage(buf, sst, rsst);
default:
@ -413,47 +486,57 @@ function parse_TST_TableDataList(M, root) {
});
return data;
}
function parse_TST_TileRowInfo(u8) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
function parse_TST_TileRowInfo(u8, type) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
var pb = parse_shallow(u8);
var R = varint_to_i32(pb[1][0].data) >>> 0;
var pre_bnc = (_b = (_a = pb[3]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data;
var pre_bnc_offsets = ((_d = (_c = pb[4]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && u8_to_dataview(pb[4][0].data);
var storage = (_f = (_e = pb[6]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data;
var storage_offsets = ((_h = (_g = pb[7]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data) && u8_to_dataview(pb[7][0].data);
var wide_offsets = ((_j = (_i = pb[8]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && varint_to_i32(pb[8][0].data) > 0 || false;
var cnt = varint_to_i32(pb[2][0].data) >>> 0;
var wide_offsets = ((_b = (_a = pb[8]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data) && varint_to_i32(pb[8][0].data) > 0 || false;
var used_storage_u8, used_storage;
if (((_d = (_c = pb[7]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && type != 0) {
used_storage_u8 = (_f = (_e = pb[7]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data;
used_storage = (_h = (_g = pb[6]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data;
} else if (((_j = (_i = pb[4]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && type != 1) {
used_storage_u8 = (_l = (_k = pb[4]) == null ? void 0 : _k[0]) == null ? void 0 : _l.data;
used_storage = (_n = (_m = pb[3]) == null ? void 0 : _m[0]) == null ? void 0 : _n.data;
} else
throw "NUMBERS Tile missing ".concat(type, " cell storage");
var width = wide_offsets ? 4 : 1;
var cells = [];
var off = 0;
for (var C = 0; C < pre_bnc_offsets.byteLength / 2; ++C) {
if (storage && storage_offsets) {
off = storage_offsets.getUint16(C * 2, true) * width;
if (off < storage.length) {
cells[C] = storage.subarray(off, storage_offsets.getUint16(C * 2 + 2, true) * width);
continue;
}
}
if (pre_bnc && pre_bnc_offsets) {
off = pre_bnc_offsets.getUint16(C * 2, true) * width;
if (off < pre_bnc.length)
cells[C] = pre_bnc.subarray(off, pre_bnc_offsets.getUint16(C * 2 + 2, true) * width);
}
var used_storage_offsets = u8_to_dataview(used_storage_u8);
var offsets = [];
for (var C = 0; C < used_storage_u8.length / 2; ++C) {
var off = used_storage_offsets.getUint16(C * 2, true);
if (off < 65535)
offsets.push([C, off]);
}
if (offsets.length != cnt)
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[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
var _a;
var pb = parse_shallow(root.data);
var ri = mappa(pb[5], parse_TST_TileRowInfo);
return ri.reduce(function(acc, x) {
if (!acc[x.R])
acc[x.R] = [];
x.cells.forEach(function(cell, C) {
if (acc[x.R][C])
throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C));
acc[x.R][C] = cell;
});
return acc;
}, []);
var storage = ((_a = pb == null ? void 0 : pb[7]) == null ? void 0 : _a[0]) ? varint_to_i32(pb[7][0].data) >>> 0 > 0 ? 1 : 0 : -1;
var ri = mappa(pb[5], function(u8) {
return parse_TST_TileRowInfo(u8, storage);
});
return {
nrows: varint_to_i32(pb[4][0].data) >>> 0,
data: ri.reduce(function(acc, x) {
if (!acc[x.R])
acc[x.R] = [];
x.cells.forEach(function(cell, C) {
if (acc[x.R][C])
throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C));
acc[x.R][C] = cell;
});
return acc;
}, [])
};
}
function parse_TST_TableModelArchive(M, root, ws) {
var _a;
@ -466,33 +549,28 @@ 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 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]) : [];
{
var tile = parse_shallow(store[3][0].data);
var tiles = [];
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_TSP_Reference(tl[2][0].data)][0];
var mtype = varint_to_i32(ref.meta[1][0].data);
if (mtype != 6002)
throw new Error("6001 unexpected reference to ".concat(mtype));
tiles.push({ id: varint_to_i32(tl[1][0].data), ref: parse_TST_Tile(M, ref) });
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]) : [];
var tile = parse_shallow(store[3][0].data);
var _R = 0;
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_TSP_Reference(tl[2][0].data)][0];
var mtype = varint_to_i32(ref.meta[1][0].data);
if (mtype != 6002)
throw new Error("6001 unexpected reference to ".concat(mtype));
var _tile = parse_TST_Tile(M, ref);
_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;
});
tiles.forEach(function(tile2) {
tile2.ref.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
});
});
});
}
}
});
_R += _tile.nrows;
});
}
function parse_TST_TableInfoArchive(M, root) {
var pb = parse_shallow(root.data);
@ -530,8 +608,8 @@ function parse_TN_DocumentArchive(M, root) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
root2.sheets.forEach(function(sheet) {
book_append_sheet(out, sheet, root2.name);
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
}
});
@ -541,7 +619,8 @@ function parse_TN_DocumentArchive(M, root) {
return out;
}
function parse_numbers_iwa(cfb) {
var out = [];
var _a, _b, _c, _d;
var out = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
if (p.match(/\.iwpv2/))
throw new Error("Unsupported password protection");
@ -551,7 +630,7 @@ function parse_numbers_iwa(cfb) {
return;
var o;
try {
o = deframe(s.content);
o = decompress_iwa_file(s.content);
} catch (e) {
return console.log("?? " + s.content.length + " " + (e.message || e));
}
@ -562,23 +641,25 @@ function parse_numbers_iwa(cfb) {
return console.log("## " + (e.message || e));
}
packets.forEach(function(packet) {
out[+packet.id] = packet.messages;
out[packet.id] = packet.messages;
indices.push(packet.id);
});
});
if (!out.length)
if (!indices.length)
throw new Error("File has no messages");
var docroot;
out.forEach(function(iwams) {
iwams.forEach(function(iwam) {
var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0;
if (mtype == 1) {
if (!docroot)
docroot = iwam;
else
throw new Error("Document has multiple roots");
}
var docroot = ((_d = (_c = (_b = (_a = out == null ? void 0 : out[1]) == null ? void 0 : _a[0]) == null ? void 0 : _b.meta) == null ? void 0 : _c[1]) == null ? void 0 : _d[0].data) && varint_to_i32(out[1][0].meta[1][0].data) == 1 && out[1][0];
if (!docroot)
indices.forEach(function(idx) {
out[idx].forEach(function(iwam) {
var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0;
if (mtype == 1) {