diff --git a/bits/40_harb.js b/bits/40_harb.js index 4651ff7..7e71b49 100644 --- a/bits/40_harb.js +++ b/bits/40_harb.js @@ -311,7 +311,7 @@ function sheet_to_dbf(ws/*:Worksheet*/, opts/*:WriteOpts*/) { var cp = +dbf_reverse_map[/*::String(*/current_codepage/*::)*/] || 0x03; h.write_shift(4, 0x00000000 | (cp<<8)); if(dbf_codepage_map[cp] != +o.codepage) { - console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); + if(o.codepage) console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); current_codepage = 1252; } @@ -578,7 +578,7 @@ var SYLK = /*#__PURE__*/(function() { case 'b': o += cell.v ? "TRUE" : "FALSE"; break; case 'e': o += cell.w || cell.v; break; case 'd': o += '"' + (cell.w || cell.v) + '"'; break; - case 's': o += '"' + cell.v.replace(/"/g,"").replace(/;/g, ";;") + '"'; break; + case 's': o += '"' + (cell.v == null ? "" : String(cell.v)).replace(/"/g,"").replace(/;/g, ";;") + '"'; break; } return o; } diff --git a/bits/42_sstxml.js b/bits/42_sstxml.js index df443b0..27f0bae 100644 --- a/bits/42_sstxml.js +++ b/bits/42_sstxml.js @@ -229,6 +229,7 @@ function write_sst_xml(sst/*:SST*/, opts)/*:string*/ { else { sitag += ""; } diff --git a/bits/68_wsbin.js b/bits/68_wsbin.js index 245c97f..c08914c 100644 --- a/bits/68_wsbin.js +++ b/bits/68_wsbin.js @@ -258,9 +258,10 @@ function parse_BrtCellSt(data) { return [cell, value, 'str']; } function write_BrtCellSt(cell, ncell, o) { + var data = cell.v == null ? "" : String(cell.v); if(o == null) o = new_buf(12 + 4 * cell.v.length); write_XLSBCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } function parse_BrtShortSt(data) { @@ -269,9 +270,10 @@ function parse_BrtShortSt(data) { return [cell, value, 'str']; } function write_BrtShortSt(cell, ncell, o) { - if(o == null) o = new_buf(8 + 4 * cell.v.length); + var data = cell.v == null ? "" : String(cell.v); + if(o == null) o = new_buf(8 + 4 * data.length); write_XLSBShortCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } @@ -824,7 +826,7 @@ function write_ws_bin_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:num switch(cell.t) { case 's': case 'str': if(opts.bookSST) { - vv = get_sst_id(opts.Strings, (cell.v/*:any*/), opts.revStrings); + vv = get_sst_id(opts.Strings, (cell.v == null ? "" : String(cell.v)/*:any*/), opts.revStrings); o.t = "s"; o.v = vv; if(last_seen) write_record(ba, 0x0012 /* BrtShortIsst */, write_BrtShortIsst(cell, o)); else write_record(ba, 0x0007 /* BrtCellIsst */, write_BrtCellIsst(cell, o)); diff --git a/bits/78_writebiff.js b/bits/78_writebiff.js index dc21e11..cdcdfed 100644 --- a/bits/78_writebiff.js +++ b/bits/78_writebiff.js @@ -68,7 +68,7 @@ function write_ws_biff2_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return; /* TODO: codepage, sst */ case 's': case 'str': - write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, (cell.v||"").slice(0,255))); + write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v == null ? "" : String(cell.v).slice(0,255))); return; } write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C)); @@ -192,9 +192,9 @@ function write_ws_biff8_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n /* TODO: codepage, sst */ case 's': case 'str': if(opts.bookSST) { - var isst = get_sst_id(opts.Strings, cell.v, opts.revStrings); + var isst = get_sst_id(opts.Strings, cell.v == null ? "" : String(cell.v), opts.revStrings); write_biff_rec(ba, 0x00fd /* LabelSst */, write_LabelSst(R, C, isst, os, opts)); - } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v||"").slice(0,255), os, opts)); + } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v == null ? "" : String(cell.v)).slice(0,255), os, opts)); break; default: write_biff_rec(ba, 0x0201 /* Blank */, write_XLSCell(R, C, os)); diff --git a/bits/83_numbers.js b/bits/83_numbers.js index 12caefb..a7e59a1 100644 --- a/bits/83_numbers.js +++ b/bits/83_numbers.js @@ -169,6 +169,39 @@ function varint_to_i32(buf) { } return i32; } +function varint_to_u64(buf) { + var l = 0, lo = buf[l] & 127, hi = 0; + varint: + if (buf[l++] >= 128) { + lo |= (buf[l] & 127) << 7; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 14; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 21; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 28; + hi = buf[l] >> 4 & 7; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 3; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 10; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 17; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 24; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 31; + } + return [lo >>> 0, hi >>> 0]; +} function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { @@ -705,10 +738,12 @@ function write_new_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[1] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 8; l += 4; break; @@ -735,10 +770,12 @@ function write_old_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[2] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 16; l += 4; break; @@ -772,6 +809,22 @@ function write_TSP_Reference(idx) { [{ type: 0, data: write_varint49(idx) }] ]); } +function numbers_add_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + var orefidx = orefs.indexOf(ref); + if (orefidx == -1) { + orefs.push(ref); + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; + } +} +function numbers_del_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs.filter(function(r) { + return r != ref; + })) }]; +} function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); @@ -779,6 +832,8 @@ function parse_TST_TableDataList(M, root) { var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); + if (!le[1]) + return; var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: @@ -988,7 +1043,7 @@ function parse_TN_DocumentArchive(M, root, opts) { return out; } function parse_numbers_iwa(cfb, opts) { - var _a, _b, _c, _d, _e, _f, _g, _h; + var _a, _b, _c, _d, _e, _f, _g; var M = {}, indices = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) @@ -1018,9 +1073,9 @@ function parse_numbers_iwa(cfb, opts) { }); if (!indices.length) throw new Error("File has no messages"); - if (((_d = (_c = (_b = (_a = M == null ? void 0 : M[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(M[1][0].meta[1][0].data) == 1e4) + if (((_c = (_b = (_a = M == null ? void 0 : M[1]) == null ? void 0 : _a[0].meta) == null ? void 0 : _b[1]) == null ? void 0 : _c[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1e4) throw new Error("Pages documents are not supported"); - var docroot = ((_h = (_g = (_f = (_e = M == null ? void 0 : M[1]) == null ? void 0 : _e[0]) == null ? void 0 : _f.meta) == null ? void 0 : _g[1]) == null ? void 0 : _h[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; + var docroot = ((_g = (_f = (_e = (_d = M == null ? void 0 : M[1]) == null ? void 0 : _d[0]) == null ? void 0 : _e.meta) == null ? void 0 : _f[1]) == null ? void 0 : _g[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; if (!docroot) indices.forEach(function(idx) { M[idx].forEach(function(iwam) { @@ -1037,8 +1092,23 @@ function parse_numbers_iwa(cfb, opts) { throw new Error("Cannot find Document root"); return parse_TN_DocumentArchive(M, docroot, opts); } -function write_tile_row(tri, data, SST, wide) { +function write_TST_TileRowInfo(data, SST, wide) { var _a, _b; + var tri = [ + [], + [{ type: 0, data: write_varint49(0) }], + [{ type: 0, data: write_varint49(0) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(5) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(1) }] + ]; if (!((_a = tri[6]) == null ? void 0 : _a[0]) || !((_b = tri[7]) == null ? void 0 : _b[0])) throw "Mutation only works on post-BNC storages!"; var cnt = 0; @@ -1078,6 +1148,11 @@ function write_tile_row(tri, data, SST, wide) { _celload = write_old_storage({ t: "b", v: data[C] }, SST); break; default: + if (data[C] instanceof Date) { + celload = write_new_storage({ t: "s", v: data[C].toISOString() }, SST); + _celload = write_old_storage({ t: "s", v: data[C].toISOString() }, SST); + break; + } throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); @@ -1097,7 +1172,7 @@ function write_tile_row(tri, data, SST, wide) { tri[6][0].data = u8concat(cell_storage); tri[3][0].data = u8concat(_cell_storage); tri[8] = [{ type: 0, data: write_varint49(wide ? 1 : 0) }]; - return cnt; + return tri; } function write_iwam(type, payload) { return { @@ -1165,19 +1240,20 @@ function write_numbers_iwa(wb, opts) { if (!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README"); var cfb = CFB.read(opts.numbers, { type: "base64" }); - var dependents = build_numbers_deps(cfb); - var cfb_DA = CFB.find(cfb, dependents[1].location); - if (!cfb_DA) - throw "Could not find ".concat(dependents[1].location, " in Numbers template"); - var iwa_DA = parse_iwa_file(decompress_iwa_file(cfb_DA.content)); - var docroot = iwa_DA.find(function(packet) { - return packet.id == 1; - }); + var deps = build_numbers_deps(cfb); + var docroot = numbers_iwa_find(cfb, deps, 1); if (docroot == null) throw "Could not find message ".concat(1, " in Numbers template"); var sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + if (sheetrefs.length > 1) + throw new Error("Template NUMBERS file must have exactly one sheet"); wb.SheetNames.forEach(function(name, idx) { - return write_numbers_ws(cfb, dependents, wb.Sheets[name], name, idx, sheetrefs[idx]); + if (idx >= 1) { + numbers_add_ws(cfb, deps, idx + 1); + docroot = numbers_iwa_find(cfb, deps, 1); + sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + } + write_numbers_ws(cfb, deps, wb.Sheets[name], name, idx, sheetrefs[idx]); }); return cfb; } @@ -1203,9 +1279,364 @@ function numbers_iwa_find(cfb, deps, id) { }); return ainfo; } +function numbers_add_ws(cfb, deps, wsidx) { + var sheetref = -1, newsheetref = -1; + var remap = {}; + numbers_iwa_doit(cfb, deps, 1, function(docroot, arch) { + var doc = parse_shallow(docroot.messages[0].data); + sheetref = parse_TSP_Reference(parse_shallow(docroot.messages[0].data)[1][0].data); + newsheetref = get_unique_msgid({ deps: [1], location: deps[sheetref].location, type: 2 }, deps); + remap[sheetref] = newsheetref; + numbers_add_oref(docroot, newsheetref); + doc[1].push({ type: 2, data: write_TSP_Reference(newsheetref) }); + var sheet = numbers_iwa_find(cfb, deps, sheetref); + sheet.id = newsheetref; + if (deps[1].location == deps[newsheetref].location) + arch.push(sheet); + else + numbers_iwa_doit(cfb, deps, newsheetref, function(_, x) { + return x.push(sheet); + }); + docroot.messages[0].data = write_shallow(doc); + }); + var tiaref = -1; + numbers_iwa_doit(cfb, deps, newsheetref, function(sheetroot, arch) { + var sa = parse_shallow(sheetroot.messages[0].data); + for (var i = 3; i <= 69; ++i) + delete sa[i]; + var drawables = mappa(sa[2], parse_TSP_Reference); + drawables.forEach(function(n) { + return numbers_del_oref(sheetroot, n); + }); + tiaref = get_unique_msgid({ deps: [newsheetref], location: deps[drawables[0]].location, type: deps[drawables[0]].type }, deps); + numbers_add_oref(sheetroot, tiaref); + remap[drawables[0]] = tiaref; + sa[2] = [{ type: 2, data: write_TSP_Reference(tiaref) }]; + var tia = numbers_iwa_find(cfb, deps, drawables[0]); + tia.id = tiaref; + if (deps[drawables[0]].location == deps[newsheetref].location) + arch.push(tia); + else { + var loc2 = deps[newsheetref].location; + loc2 = loc2.replace(/^Root Entry\//, ""); + loc2 = loc2.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + var parentidx = mlist[3].findIndex(function(m) { + var _a, _b; + var mm = parse_shallow(m.data); + if ((_a = mm[3]) == null ? void 0 : _a[0]) + return u8str(mm[3][0].data) == loc2; + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc2) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(tiaref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + numbers_iwa_doit(cfb, deps, tiaref, function(_, x) { + return x.push(tia); + }); + } + sheetroot.messages[0].data = write_shallow(sa); + }); + var tmaref = -1; + numbers_iwa_doit(cfb, deps, tiaref, function(tiaroot, arch) { + var tia = parse_shallow(tiaroot.messages[0].data); + var da = parse_shallow(tia[1][0].data); + for (var i = 3; i <= 69; ++i) + delete da[i]; + var dap = parse_TSP_Reference(da[2][0].data); + da[2][0].data = write_TSP_Reference(remap[dap]); + tia[1][0].data = write_shallow(da); + var oldtmaref = parse_TSP_Reference(tia[2][0].data); + numbers_del_oref(tiaroot, oldtmaref); + tmaref = get_unique_msgid({ deps: [tiaref], location: deps[oldtmaref].location, type: deps[oldtmaref].type }, deps); + numbers_add_oref(tiaroot, tmaref); + remap[oldtmaref] = tmaref; + tia[2][0].data = write_TSP_Reference(tmaref); + var tma = numbers_iwa_find(cfb, deps, oldtmaref); + tma.id = tmaref; + if (deps[tiaref].location == deps[tmaref].location) + arch.push(tma); + else + numbers_iwa_doit(cfb, deps, tmaref, function(_, x) { + return x.push(tma); + }); + tiaroot.messages[0].data = write_shallow(tia); + }); + var loc = deps[tmaref].location; + loc = loc.replace(/^Root Entry\//, ""); + loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, tmaref, function(tmaroot, arch) { + var _a, _b; + var tma = parse_shallow(tmaroot.messages[0].data); + var uuid = u8str(tma[1][0].data), new_uuid = uuid.replace(/-[A-Z0-9]*/, "-".concat(wsidx.toString(16).padStart(4, "0"))); + tma[1][0].data = stru8(new_uuid); + [12, 13, 29, 31, 32, 33, 39, 44, 47, 81, 82, 84].forEach(function(n) { + return delete tma[n]; + }); + if (tma[45]) { + var srrta = parse_shallow(tma[45][0].data); + var ref = parse_TSP_Reference(srrta[1][0].data); + numbers_del_oref(tmaroot, ref); + delete tma[45]; + } + if (tma[70]) { + var hsoa = parse_shallow(tma[70][0].data); + (_a = hsoa[2]) == null ? void 0 : _a.forEach(function(item) { + var hsa = parse_shallow(item.data); + [2, 3].map(function(n) { + return hsa[n][0]; + }).forEach(function(hseadata) { + var hsea = parse_shallow(hseadata.data); + if (!hsea[8]) + return; + var ref2 = parse_TSP_Reference(hsea[8][0].data); + numbers_del_oref(tmaroot, ref2); + }); + }); + delete tma[70]; + } + [ + 46, + 30, + 34, + 35, + 36, + 38, + 48, + 49, + 60, + 61, + 62, + 63, + 64, + 71, + 72, + 73, + 74, + 75, + 85, + 86, + 87, + 88, + 89 + ].forEach(function(n) { + if (!tma[n]) + return; + var ref2 = parse_TSP_Reference(tma[n][0].data); + delete tma[n]; + numbers_del_oref(tmaroot, ref2); + }); + var store = parse_shallow(tma[4][0].data); + { + [2, 4, 5, 6, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22].forEach(function(n) { + var _a2; + if (!((_a2 = store[n]) == null ? void 0 : _a2[0])) + return; + var oldref = parse_TSP_Reference(store[n][0].data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) + arch.push(msg); + else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a3, _b2; + var mm = parse_shallow(m.data); + if ((_a3 = mm[3]) == null ? void 0 : _a3[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + store[n][0].data = write_TSP_Reference(newref); + }); + var row_headers = parse_shallow(store[1][0].data); + { + (_b = row_headers[2]) == null ? void 0 : _b.forEach(function(tspref) { + var oldref = parse_TSP_Reference(tspref.data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) { + arch.push(msg); + } else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tspref.data = write_TSP_Reference(newref); + }); + } + store[1][0].data = write_shallow(row_headers); + var tiles = parse_shallow(store[3][0].data); + { + tiles[1].forEach(function(t) { + var tst = parse_shallow(t.data); + var oldtileref = parse_TSP_Reference(tst[2][0].data); + var newtileref = remap[oldtileref]; + if (!remap[oldtileref]) { + newtileref = get_unique_msgid({ deps: [tmaref], location: "", type: deps[oldtileref].type }, deps); + deps[newtileref].location = "Root Entry/Index/Tables/Tile-".concat(newtileref, ".iwa"); + remap[oldtileref] = newtileref; + var oldtile = numbers_iwa_find(cfb, deps, oldtileref); + oldtile.id = newtileref; + numbers_del_oref(tmaroot, oldtileref); + numbers_add_oref(tmaroot, newtileref); + CFB.utils.cfb_add(cfb, "/Index/Tables/Tile-".concat(newtileref, ".iwa"), compress_iwa_file(write_iwa_file([oldtile]))); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }], + [{ type: 2, data: stru8("Tables/Tile") }], + [{ type: 2, data: stru8("Tables/Tile-".concat(newtileref)) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tst[2][0].data = write_TSP_Reference(newtileref); + t.data = write_shallow(tst); + }); + } + store[3][0].data = write_shallow(tiles); + } + tma[4][0].data = write_shallow(store); + tmaroot.messages[0].data = write_shallow(tma); + }); +} function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { - if (sheetidx >= 1) - return console.error("The Numbers writer currently writes only the first table"); var drawables = []; numbers_iwa_doit(cfb, deps, rootref, function(docroot) { var sheetref = parse_shallow(docroot.messages[0].data); @@ -1223,7 +1654,6 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { - var _a, _b; var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; @@ -1238,15 +1668,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { if (trunc) console.error("Truncating to ".concat(encode_range(range))); var data = sheet_to_json(ws, { range: range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(function(row) { - return row.forEach(function(cell) { - if (typeof cell == "string" && !SST_set.has(cell)) { - SST.push(cell); - SST_set.add(cell); - } - }); - }); + var SST = ["~Sh33tJ5~"]; var loc = deps[tmaref].location; loc = loc.replace(/^Root Entry\//, ""); loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); @@ -1259,9 +1681,9 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { { var row_header_ref = parse_TSP_Reference(parse_shallow(store[1][0].data)[2][0].data); numbers_iwa_doit(cfb, deps, row_header_ref, function(rowhead, _x) { - var _a2; + var _a; var base_bucket = parse_shallow(rowhead.messages[0].data); - if ((_a2 = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a2[0]) + if ((_a = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a[0]) for (var R2 = 0; R2 < data.length; ++R2) { var _bucket = parse_shallow(base_bucket[2][0].data); _bucket[1][0].data = write_varint49(R2); @@ -1281,22 +1703,6 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { } colhead.messages[0].data = write_shallow(base_bucket); }); - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach(function(str, i) { - sstdata[3].push({ type: 2, data: write_shallow([ - [], - [{ type: 0, data: write_varint49(i) }], - [{ type: 0, data: write_varint49(1) }], - [{ type: 2, data: stru8(str) }] - ]) }); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; var tilestore = parse_shallow(store[3][0].data); @@ -1304,25 +1710,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { var tstride = 256; tilestore[2] = [{ type: 0, data: write_varint49(tstride) }]; var tileref = parse_TSP_Reference(parse_shallow(tilestore[1][0].data)[2][0].data); - var save_token = 0; + var save_token = function() { + var metadata = numbers_iwa_find(cfb, deps, 2); + var mlist = parse_shallow(metadata.messages[0].data); + var mlst = mlist[3].filter(function(m) { + return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; + }); + return (mlst == null ? void 0 : mlst.length) ? parse_varint49(parse_shallow(mlst[0].data)[12][0].data) : 0; + }(); { CFB.utils.cfb_del(cfb, deps[tileref].location); numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); - var lst = mlist[3].filter(function(m) { - return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; - }); - if (lst && lst.length > 0) - save_token = parse_varint49(parse_shallow(lst[0].data)[12][0].data); mlist[3] = mlist[3].filter(function(m) { return parse_varint49(parse_shallow(m.data)[1][0].data) != tileref; }); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1335,6 +1743,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); + numbers_del_oref(tmaroot, tileref); } tilestore[1] = []; var ntiles = Math.ceil((range.e.r + 1) / tstride); @@ -1357,22 +1766,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { [{ type: 0, data: write_varint49(USE_WIDE_ROWS ? 1 : 0) }] ]; for (var R = tidx * tstride; R <= Math.min(range.e.r, (tidx + 1) * tstride - 1); ++R) { - var tilerow = [ - [], - [{ type: 0, data: write_varint49(0) }], - [{ type: 0, data: write_varint49(0) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(5) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(1) }] - ]; - write_tile_row(tilerow, data[R], SST, USE_WIDE_ROWS); + var tilerow = write_TST_TileRowInfo(data[R], SST, USE_WIDE_ROWS); tilerow[1][0].data = write_varint49(R - tidx * tstride); tiledata[5].push({ data: write_shallow(tilerow), type: 2 }); } @@ -1406,11 +1800,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { ]) }); mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileid + 1, parse_varint49(mlist[1][0].data))) }]; var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1427,12 +1821,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - var orefs = ((_a = tmaroot.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - var orefidx = orefs.indexOf(newtileid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = newtileid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, newtileid); rbtree[1].push({ type: 2, data: write_shallow([ [], [{ type: 0, data: write_varint49(tidx * tstride) }], @@ -1472,11 +1861,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1493,14 +1882,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - orefs = ((_b = tmaroot.messages[0].meta[5]) == null ? void 0 : _b[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - orefidx = orefs.indexOf(mergeid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = mergeid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, mergeid); } else delete store[13]; + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach(function(str, i) { + if (i == 0) + return; + sstdata[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(i) }], + [{ type: 0, data: write_varint49(1) }], + [{ type: 2, data: stru8(str) }] + ]) }); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); } pb[4][0].data = write_shallow(store); } diff --git a/modules/83_numbers.js b/modules/83_numbers.js index 12caefb..a7e59a1 100644 --- a/modules/83_numbers.js +++ b/modules/83_numbers.js @@ -169,6 +169,39 @@ function varint_to_i32(buf) { } return i32; } +function varint_to_u64(buf) { + var l = 0, lo = buf[l] & 127, hi = 0; + varint: + if (buf[l++] >= 128) { + lo |= (buf[l] & 127) << 7; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 14; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 21; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 28; + hi = buf[l] >> 4 & 7; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 3; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 10; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 17; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 24; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 31; + } + return [lo >>> 0, hi >>> 0]; +} function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { @@ -705,10 +738,12 @@ function write_new_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[1] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 8; l += 4; break; @@ -735,10 +770,12 @@ function write_old_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[2] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 16; l += 4; break; @@ -772,6 +809,22 @@ function write_TSP_Reference(idx) { [{ type: 0, data: write_varint49(idx) }] ]); } +function numbers_add_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + var orefidx = orefs.indexOf(ref); + if (orefidx == -1) { + orefs.push(ref); + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; + } +} +function numbers_del_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs.filter(function(r) { + return r != ref; + })) }]; +} function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); @@ -779,6 +832,8 @@ function parse_TST_TableDataList(M, root) { var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); + if (!le[1]) + return; var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: @@ -988,7 +1043,7 @@ function parse_TN_DocumentArchive(M, root, opts) { return out; } function parse_numbers_iwa(cfb, opts) { - var _a, _b, _c, _d, _e, _f, _g, _h; + var _a, _b, _c, _d, _e, _f, _g; var M = {}, indices = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) @@ -1018,9 +1073,9 @@ function parse_numbers_iwa(cfb, opts) { }); if (!indices.length) throw new Error("File has no messages"); - if (((_d = (_c = (_b = (_a = M == null ? void 0 : M[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(M[1][0].meta[1][0].data) == 1e4) + if (((_c = (_b = (_a = M == null ? void 0 : M[1]) == null ? void 0 : _a[0].meta) == null ? void 0 : _b[1]) == null ? void 0 : _c[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1e4) throw new Error("Pages documents are not supported"); - var docroot = ((_h = (_g = (_f = (_e = M == null ? void 0 : M[1]) == null ? void 0 : _e[0]) == null ? void 0 : _f.meta) == null ? void 0 : _g[1]) == null ? void 0 : _h[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; + var docroot = ((_g = (_f = (_e = (_d = M == null ? void 0 : M[1]) == null ? void 0 : _d[0]) == null ? void 0 : _e.meta) == null ? void 0 : _f[1]) == null ? void 0 : _g[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; if (!docroot) indices.forEach(function(idx) { M[idx].forEach(function(iwam) { @@ -1037,8 +1092,23 @@ function parse_numbers_iwa(cfb, opts) { throw new Error("Cannot find Document root"); return parse_TN_DocumentArchive(M, docroot, opts); } -function write_tile_row(tri, data, SST, wide) { +function write_TST_TileRowInfo(data, SST, wide) { var _a, _b; + var tri = [ + [], + [{ type: 0, data: write_varint49(0) }], + [{ type: 0, data: write_varint49(0) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(5) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(1) }] + ]; if (!((_a = tri[6]) == null ? void 0 : _a[0]) || !((_b = tri[7]) == null ? void 0 : _b[0])) throw "Mutation only works on post-BNC storages!"; var cnt = 0; @@ -1078,6 +1148,11 @@ function write_tile_row(tri, data, SST, wide) { _celload = write_old_storage({ t: "b", v: data[C] }, SST); break; default: + if (data[C] instanceof Date) { + celload = write_new_storage({ t: "s", v: data[C].toISOString() }, SST); + _celload = write_old_storage({ t: "s", v: data[C].toISOString() }, SST); + break; + } throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); @@ -1097,7 +1172,7 @@ function write_tile_row(tri, data, SST, wide) { tri[6][0].data = u8concat(cell_storage); tri[3][0].data = u8concat(_cell_storage); tri[8] = [{ type: 0, data: write_varint49(wide ? 1 : 0) }]; - return cnt; + return tri; } function write_iwam(type, payload) { return { @@ -1165,19 +1240,20 @@ function write_numbers_iwa(wb, opts) { if (!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README"); var cfb = CFB.read(opts.numbers, { type: "base64" }); - var dependents = build_numbers_deps(cfb); - var cfb_DA = CFB.find(cfb, dependents[1].location); - if (!cfb_DA) - throw "Could not find ".concat(dependents[1].location, " in Numbers template"); - var iwa_DA = parse_iwa_file(decompress_iwa_file(cfb_DA.content)); - var docroot = iwa_DA.find(function(packet) { - return packet.id == 1; - }); + var deps = build_numbers_deps(cfb); + var docroot = numbers_iwa_find(cfb, deps, 1); if (docroot == null) throw "Could not find message ".concat(1, " in Numbers template"); var sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + if (sheetrefs.length > 1) + throw new Error("Template NUMBERS file must have exactly one sheet"); wb.SheetNames.forEach(function(name, idx) { - return write_numbers_ws(cfb, dependents, wb.Sheets[name], name, idx, sheetrefs[idx]); + if (idx >= 1) { + numbers_add_ws(cfb, deps, idx + 1); + docroot = numbers_iwa_find(cfb, deps, 1); + sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + } + write_numbers_ws(cfb, deps, wb.Sheets[name], name, idx, sheetrefs[idx]); }); return cfb; } @@ -1203,9 +1279,364 @@ function numbers_iwa_find(cfb, deps, id) { }); return ainfo; } +function numbers_add_ws(cfb, deps, wsidx) { + var sheetref = -1, newsheetref = -1; + var remap = {}; + numbers_iwa_doit(cfb, deps, 1, function(docroot, arch) { + var doc = parse_shallow(docroot.messages[0].data); + sheetref = parse_TSP_Reference(parse_shallow(docroot.messages[0].data)[1][0].data); + newsheetref = get_unique_msgid({ deps: [1], location: deps[sheetref].location, type: 2 }, deps); + remap[sheetref] = newsheetref; + numbers_add_oref(docroot, newsheetref); + doc[1].push({ type: 2, data: write_TSP_Reference(newsheetref) }); + var sheet = numbers_iwa_find(cfb, deps, sheetref); + sheet.id = newsheetref; + if (deps[1].location == deps[newsheetref].location) + arch.push(sheet); + else + numbers_iwa_doit(cfb, deps, newsheetref, function(_, x) { + return x.push(sheet); + }); + docroot.messages[0].data = write_shallow(doc); + }); + var tiaref = -1; + numbers_iwa_doit(cfb, deps, newsheetref, function(sheetroot, arch) { + var sa = parse_shallow(sheetroot.messages[0].data); + for (var i = 3; i <= 69; ++i) + delete sa[i]; + var drawables = mappa(sa[2], parse_TSP_Reference); + drawables.forEach(function(n) { + return numbers_del_oref(sheetroot, n); + }); + tiaref = get_unique_msgid({ deps: [newsheetref], location: deps[drawables[0]].location, type: deps[drawables[0]].type }, deps); + numbers_add_oref(sheetroot, tiaref); + remap[drawables[0]] = tiaref; + sa[2] = [{ type: 2, data: write_TSP_Reference(tiaref) }]; + var tia = numbers_iwa_find(cfb, deps, drawables[0]); + tia.id = tiaref; + if (deps[drawables[0]].location == deps[newsheetref].location) + arch.push(tia); + else { + var loc2 = deps[newsheetref].location; + loc2 = loc2.replace(/^Root Entry\//, ""); + loc2 = loc2.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + var parentidx = mlist[3].findIndex(function(m) { + var _a, _b; + var mm = parse_shallow(m.data); + if ((_a = mm[3]) == null ? void 0 : _a[0]) + return u8str(mm[3][0].data) == loc2; + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc2) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(tiaref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + numbers_iwa_doit(cfb, deps, tiaref, function(_, x) { + return x.push(tia); + }); + } + sheetroot.messages[0].data = write_shallow(sa); + }); + var tmaref = -1; + numbers_iwa_doit(cfb, deps, tiaref, function(tiaroot, arch) { + var tia = parse_shallow(tiaroot.messages[0].data); + var da = parse_shallow(tia[1][0].data); + for (var i = 3; i <= 69; ++i) + delete da[i]; + var dap = parse_TSP_Reference(da[2][0].data); + da[2][0].data = write_TSP_Reference(remap[dap]); + tia[1][0].data = write_shallow(da); + var oldtmaref = parse_TSP_Reference(tia[2][0].data); + numbers_del_oref(tiaroot, oldtmaref); + tmaref = get_unique_msgid({ deps: [tiaref], location: deps[oldtmaref].location, type: deps[oldtmaref].type }, deps); + numbers_add_oref(tiaroot, tmaref); + remap[oldtmaref] = tmaref; + tia[2][0].data = write_TSP_Reference(tmaref); + var tma = numbers_iwa_find(cfb, deps, oldtmaref); + tma.id = tmaref; + if (deps[tiaref].location == deps[tmaref].location) + arch.push(tma); + else + numbers_iwa_doit(cfb, deps, tmaref, function(_, x) { + return x.push(tma); + }); + tiaroot.messages[0].data = write_shallow(tia); + }); + var loc = deps[tmaref].location; + loc = loc.replace(/^Root Entry\//, ""); + loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, tmaref, function(tmaroot, arch) { + var _a, _b; + var tma = parse_shallow(tmaroot.messages[0].data); + var uuid = u8str(tma[1][0].data), new_uuid = uuid.replace(/-[A-Z0-9]*/, "-".concat(wsidx.toString(16).padStart(4, "0"))); + tma[1][0].data = stru8(new_uuid); + [12, 13, 29, 31, 32, 33, 39, 44, 47, 81, 82, 84].forEach(function(n) { + return delete tma[n]; + }); + if (tma[45]) { + var srrta = parse_shallow(tma[45][0].data); + var ref = parse_TSP_Reference(srrta[1][0].data); + numbers_del_oref(tmaroot, ref); + delete tma[45]; + } + if (tma[70]) { + var hsoa = parse_shallow(tma[70][0].data); + (_a = hsoa[2]) == null ? void 0 : _a.forEach(function(item) { + var hsa = parse_shallow(item.data); + [2, 3].map(function(n) { + return hsa[n][0]; + }).forEach(function(hseadata) { + var hsea = parse_shallow(hseadata.data); + if (!hsea[8]) + return; + var ref2 = parse_TSP_Reference(hsea[8][0].data); + numbers_del_oref(tmaroot, ref2); + }); + }); + delete tma[70]; + } + [ + 46, + 30, + 34, + 35, + 36, + 38, + 48, + 49, + 60, + 61, + 62, + 63, + 64, + 71, + 72, + 73, + 74, + 75, + 85, + 86, + 87, + 88, + 89 + ].forEach(function(n) { + if (!tma[n]) + return; + var ref2 = parse_TSP_Reference(tma[n][0].data); + delete tma[n]; + numbers_del_oref(tmaroot, ref2); + }); + var store = parse_shallow(tma[4][0].data); + { + [2, 4, 5, 6, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22].forEach(function(n) { + var _a2; + if (!((_a2 = store[n]) == null ? void 0 : _a2[0])) + return; + var oldref = parse_TSP_Reference(store[n][0].data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) + arch.push(msg); + else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a3, _b2; + var mm = parse_shallow(m.data); + if ((_a3 = mm[3]) == null ? void 0 : _a3[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + store[n][0].data = write_TSP_Reference(newref); + }); + var row_headers = parse_shallow(store[1][0].data); + { + (_b = row_headers[2]) == null ? void 0 : _b.forEach(function(tspref) { + var oldref = parse_TSP_Reference(tspref.data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) { + arch.push(msg); + } else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tspref.data = write_TSP_Reference(newref); + }); + } + store[1][0].data = write_shallow(row_headers); + var tiles = parse_shallow(store[3][0].data); + { + tiles[1].forEach(function(t) { + var tst = parse_shallow(t.data); + var oldtileref = parse_TSP_Reference(tst[2][0].data); + var newtileref = remap[oldtileref]; + if (!remap[oldtileref]) { + newtileref = get_unique_msgid({ deps: [tmaref], location: "", type: deps[oldtileref].type }, deps); + deps[newtileref].location = "Root Entry/Index/Tables/Tile-".concat(newtileref, ".iwa"); + remap[oldtileref] = newtileref; + var oldtile = numbers_iwa_find(cfb, deps, oldtileref); + oldtile.id = newtileref; + numbers_del_oref(tmaroot, oldtileref); + numbers_add_oref(tmaroot, newtileref); + CFB.utils.cfb_add(cfb, "/Index/Tables/Tile-".concat(newtileref, ".iwa"), compress_iwa_file(write_iwa_file([oldtile]))); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }], + [{ type: 2, data: stru8("Tables/Tile") }], + [{ type: 2, data: stru8("Tables/Tile-".concat(newtileref)) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tst[2][0].data = write_TSP_Reference(newtileref); + t.data = write_shallow(tst); + }); + } + store[3][0].data = write_shallow(tiles); + } + tma[4][0].data = write_shallow(store); + tmaroot.messages[0].data = write_shallow(tma); + }); +} function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { - if (sheetidx >= 1) - return console.error("The Numbers writer currently writes only the first table"); var drawables = []; numbers_iwa_doit(cfb, deps, rootref, function(docroot) { var sheetref = parse_shallow(docroot.messages[0].data); @@ -1223,7 +1654,6 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { - var _a, _b; var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; @@ -1238,15 +1668,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { if (trunc) console.error("Truncating to ".concat(encode_range(range))); var data = sheet_to_json(ws, { range: range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(function(row) { - return row.forEach(function(cell) { - if (typeof cell == "string" && !SST_set.has(cell)) { - SST.push(cell); - SST_set.add(cell); - } - }); - }); + var SST = ["~Sh33tJ5~"]; var loc = deps[tmaref].location; loc = loc.replace(/^Root Entry\//, ""); loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); @@ -1259,9 +1681,9 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { { var row_header_ref = parse_TSP_Reference(parse_shallow(store[1][0].data)[2][0].data); numbers_iwa_doit(cfb, deps, row_header_ref, function(rowhead, _x) { - var _a2; + var _a; var base_bucket = parse_shallow(rowhead.messages[0].data); - if ((_a2 = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a2[0]) + if ((_a = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a[0]) for (var R2 = 0; R2 < data.length; ++R2) { var _bucket = parse_shallow(base_bucket[2][0].data); _bucket[1][0].data = write_varint49(R2); @@ -1281,22 +1703,6 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { } colhead.messages[0].data = write_shallow(base_bucket); }); - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach(function(str, i) { - sstdata[3].push({ type: 2, data: write_shallow([ - [], - [{ type: 0, data: write_varint49(i) }], - [{ type: 0, data: write_varint49(1) }], - [{ type: 2, data: stru8(str) }] - ]) }); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; var tilestore = parse_shallow(store[3][0].data); @@ -1304,25 +1710,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { var tstride = 256; tilestore[2] = [{ type: 0, data: write_varint49(tstride) }]; var tileref = parse_TSP_Reference(parse_shallow(tilestore[1][0].data)[2][0].data); - var save_token = 0; + var save_token = function() { + var metadata = numbers_iwa_find(cfb, deps, 2); + var mlist = parse_shallow(metadata.messages[0].data); + var mlst = mlist[3].filter(function(m) { + return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; + }); + return (mlst == null ? void 0 : mlst.length) ? parse_varint49(parse_shallow(mlst[0].data)[12][0].data) : 0; + }(); { CFB.utils.cfb_del(cfb, deps[tileref].location); numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); - var lst = mlist[3].filter(function(m) { - return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; - }); - if (lst && lst.length > 0) - save_token = parse_varint49(parse_shallow(lst[0].data)[12][0].data); mlist[3] = mlist[3].filter(function(m) { return parse_varint49(parse_shallow(m.data)[1][0].data) != tileref; }); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1335,6 +1743,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); + numbers_del_oref(tmaroot, tileref); } tilestore[1] = []; var ntiles = Math.ceil((range.e.r + 1) / tstride); @@ -1357,22 +1766,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { [{ type: 0, data: write_varint49(USE_WIDE_ROWS ? 1 : 0) }] ]; for (var R = tidx * tstride; R <= Math.min(range.e.r, (tidx + 1) * tstride - 1); ++R) { - var tilerow = [ - [], - [{ type: 0, data: write_varint49(0) }], - [{ type: 0, data: write_varint49(0) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(5) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(1) }] - ]; - write_tile_row(tilerow, data[R], SST, USE_WIDE_ROWS); + var tilerow = write_TST_TileRowInfo(data[R], SST, USE_WIDE_ROWS); tilerow[1][0].data = write_varint49(R - tidx * tstride); tiledata[5].push({ data: write_shallow(tilerow), type: 2 }); } @@ -1406,11 +1800,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { ]) }); mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileid + 1, parse_varint49(mlist[1][0].data))) }]; var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1427,12 +1821,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - var orefs = ((_a = tmaroot.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - var orefidx = orefs.indexOf(newtileid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = newtileid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, newtileid); rbtree[1].push({ type: 2, data: write_shallow([ [], [{ type: 0, data: write_varint49(tidx * tstride) }], @@ -1472,11 +1861,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -1493,14 +1882,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - orefs = ((_b = tmaroot.messages[0].meta[5]) == null ? void 0 : _b[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - orefidx = orefs.indexOf(mergeid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = mergeid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, mergeid); } else delete store[13]; + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach(function(str, i) { + if (i == 0) + return; + sstdata[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(i) }], + [{ type: 0, data: write_varint49(1) }], + [{ type: 2, data: stru8(str) }] + ]) }); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); } pb[4][0].data = write_shallow(store); } diff --git a/modules/83_numbers.ts b/modules/83_numbers.ts index 2c2faee..aa50f24 100644 --- a/modules/83_numbers.ts +++ b/modules/83_numbers.ts @@ -573,8 +573,10 @@ function write_new_storage(cell: CellObject, sst: string[]): Uint8Array { case "n": out[1] = 2; writeDecimal128LE(out, l, cell.v as number); flags |= 1; l += 16; break; case "b": out[1] = 6; dv.setFloat64(l, cell.v ? 1 : 0, true); flags |= 2; l += 8; break; case "s": - if(sst.indexOf(cell.v as string) == -1) throw new Error(`Value ${cell.v} missing from SST!`); - out[1] = 3; dv.setUint32(l, sst.indexOf(cell.v as string), true); flags |= 8; l += 4; break; + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if(isst == -1) sst[isst = sst.length] = s; + out[1] = 3; dv.setUint32(l, isst, true); flags |= 8; l += 4; break; default: throw "unsupported cell type " + cell.t; } dv.setUint32(8, flags, true); @@ -588,8 +590,10 @@ function write_old_storage(cell: CellObject, sst: string[]): Uint8Array { case "n": out[2] = 2; dv.setFloat64(l, cell.v as number, true); flags |= 0x20; l += 8; break; case "b": out[2] = 6; dv.setFloat64(l, cell.v ? 1 : 0, true); flags |= 0x20; l += 8; break; case "s": - if(sst.indexOf(cell.v as string) == -1) throw new Error(`Value ${cell.v} missing from SST!`); - out[2] = 3; dv.setUint32(l, sst.indexOf(cell.v as string), true); flags |= 0x10; l += 4; break; + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if(isst == -1) sst[isst = sst.length] = s; + out[2] = 3; dv.setUint32(l, isst, true); flags |= 0x10; l += 4; break; default: throw "unsupported cell type " + cell.t; } dv.setUint32(8, flags, true); @@ -957,7 +961,14 @@ function write_TST_TileRowInfo(data: any[], SST: string[], wide: boolean): Proto celload = write_new_storage({t: "b", v: data[C]}, SST); /*if(!wide)*/ _celload = write_old_storage({t: "b", v: data[C]}, SST); break; - default: throw new Error("Unsupported value " + data[C]); + default: + // TODO: write the actual date code + if(data[C] instanceof Date) { + celload = write_new_storage({t: "s", v: (data[C] as Date).toISOString()}, SST); + /*if(!wide)*/ _celload = write_old_storage({t: "s", v: (data[C] as Date).toISOString()}, SST); + break; + } + throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); last_offset += celload.length; /*if(!wide)*/ { _cell_storage.push(_celload); _last_offset += _celload.length; } @@ -1491,8 +1502,7 @@ function write_numbers_tma(cfb: CFB$Container, deps: Dependents, ws: WorkSheet, /* preprocess data and build up shared string table */ var data = sheet_to_json(ws, { range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(row => row.forEach(cell => { if(typeof cell == "string" && !SST_set.has(cell)) { SST.push(cell); SST_set.add(cell); } })); + var SST = ["~Sh33tJ5~"]; /* identifier for finding the TableModelArchive in the archive */ var loc = deps[tmaref].location; @@ -1536,24 +1546,6 @@ function write_numbers_tma(cfb: CFB$Container, deps: Dependents, ws: WorkSheet, colhead.messages[0].data = write_shallow(base_bucket); }); - /* rebuild shared string table */ - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, (sstroot) => { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach((str, i) => { - if(i == 0) return; // Numbers will assert if index zero - sstdata[3].push({type: 2, data: write_shallow([ [], - [ { type: 0, data: write_varint49(i) } ], - [ { type: 0, data: write_varint49(1) } ], - [ { type: 2, data: stru8(str) } ] - ])}); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); - var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; @@ -1758,6 +1750,24 @@ function write_numbers_tma(cfb: CFB$Container, deps: Dependents, ws: WorkSheet, } else delete store[13]; // TODO: delete references to merge if not needed + /* rebuild shared string table */ + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, (sstroot) => { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach((str, i) => { + if(i == 0) return; // Numbers will assert if index zero + sstdata[3].push({type: 2, data: write_shallow([ [], + [ { type: 0, data: write_varint49(i) } ], + [ { type: 0, data: write_varint49(1) } ], + [ { type: 2, data: stru8(str) } ] + ])}); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); + } pb[4][0].data = write_shallow(store); } diff --git a/test.js b/test.js index 89aa7f4..8cf0fcc 100644 --- a/test.js +++ b/test.js @@ -13,10 +13,8 @@ declare var before:(test:EmptyFunc)=>void; declare var afterEach:(test:EmptyFunc)=>void; declare var cptable: any; */ -var X, XLSX_ZAHL; -var modp = './'; +var X = require("./"), XLSX_ZAHL = require("./dist/xlsx.zahl"); var fs = require('fs'), assert = require('assert'); -describe('source',function(){it('should load',function(){X=require(modp);});}); var DIF_XL = true; var browser = typeof document !== 'undefined'; @@ -398,11 +396,6 @@ function check_comments(wb) { describe('parse options', function() { var html_cell_types = ['s']; - var bef = (function() { - X = require(modp); - }); - if(typeof before != 'undefined') before(bef); - else it('before', bef); describe('cell', function() { it('XLSX should generate HTML by default', function() { var wb = X.read(fs.readFileSync(paths.cstxlsx), {type:TYPE}); @@ -750,7 +743,6 @@ describe('output formats', function() { ["prn", false, true] ]; function RT(T) { - if(!X) X = require(modp); fmts.forEach(function(fmt) { var wb = X.utils.book_new(); X.utils.book_append_sheet(wb, X.utils.aoa_to_sheet([['R',"\u2603"],["\u0BEE",2]]), "Sheet1"); @@ -929,7 +921,6 @@ describe('parse features', function() { describe('comments', function() { if(fs.existsSync(paths.swcxlsx)) it('should have comment as part of cell properties', function(){ - X = require(modp); var sheet = 'Sheet1'; var wb1=X.read(fs.readFileSync(paths.swcxlsx), {type:TYPE}); var wb2=X.read(fs.readFileSync(paths.swcxlsb), {type:TYPE}); @@ -1034,7 +1025,6 @@ describe('parse features', function() { describe('column properties', function() { var wbs = [], wbs_no_slk = []; var bef = (function() { - X = require(modp); wbs = CWPaths.map(function(n) { return X.read(fs.readFileSync(n), {type:TYPE, cellStyles:true}); }); wbs_no_slk = wbs.slice(0, 5); }); @@ -1077,7 +1067,6 @@ describe('parse features', function() { var wbs = [], ols = []; var ol = fs.existsSync(paths.olxls); var bef = (function() { - X = require(modp); wbs = RHPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, cellStyles:true}); }); /* */ if(!ol) return; @@ -1123,7 +1112,6 @@ describe('parse features', function() { describe('merge cells',function() { var wbs=[]; var bef = (function() { - X = require(modp); wbs = MCPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE}); }); }); if(typeof before != 'undefined') before(bef); @@ -1142,7 +1130,6 @@ describe('parse features', function() { describe('should find hyperlinks', function() { var wb1, wb2; var bef = (function() { - X = require(modp); wb1 = HLPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, WTF:true}); }); wb2 = ILPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, WTF:true}); }); }); @@ -1454,7 +1441,6 @@ describe('parse features', function() { // TODO: keep in sync with BookType, support other formats "xlsx"/*, "xlsm" */, "xlsb"/* xls / xla / biff# */, "xlml", "ods", "fods"/*, "csv", "txt", */, "sylk", "html", "dif", "rtf"/*, "prn", "eth"*/, "dbf", "numbers" ].forEach(function(r) { - if(!XLSX_ZAHL) XLSX_ZAHL=require("./dist/xlsx.zahl"); if(r == "numbers" && !can_write_numbers) return; var ws = X.utils.aoa_to_sheet([ ["a", "b", "c"], [1, 2, 3] ]); var wb = X.utils.book_new(); X.utils.book_append_sheet(wb, ws, "Sheet1"); @@ -1483,7 +1469,6 @@ describe('write features', function() { }; var ws; var bef = (function() { - X = require(modp); ws = X.utils.aoa_to_sheet([["a","b","c"],[1,2,3]]); }); if(typeof before != 'undefined') before(bef); @@ -1561,6 +1546,21 @@ describe('write features', function() { assert.equal(Name.Ref, "Sheet1!$A$1:$C$3"); assert.equal(wb2.Workbook.Names.length, wb3.Workbook.Names.length); }); }); + it('should handle non-string values for "s" cells', function() {[ + "xlsx", "xlsb", "xls", "biff5", "biff2", "xlml", "numbers", "ods", "fods", "wk3", "csv", "txt", "sylk", "html", "dif", "dbf", "wk1", "rtf", "prn" + ].forEach(function(fmt) { + var ws = X.utils.aoa_to_sheet([ + ["String", "123"], + ["Number", 123], + ["Boolean", true], + ["Date", new Date()], + ], { cellDates: true }); + ws["B2"].t = ws["B3"].t = ws["B4"].t = "s" + var wb = X.utils.book_new(); + X.utils.book_append_sheet(wb, ws, "Sheet1"); + X.write(wb, {type: TYPE, bookType: fmt, bookSST: false, numbers:XLSX_ZAHL}); + X.write(wb, {type: TYPE, bookType: fmt, bookSST: true, numbers:XLSX_ZAHL}); + }); }); }); function seq(end/*:number*/, start/*:?number*/)/*:Array*/ { @@ -1597,9 +1597,6 @@ function parseDate(str/*:string|Date*/)/*:Date*/ { var fixdate = browser ? parseDate("2014-02-19T14:30:00.000Z") : new Date("2014-02-19T14:30Z"); describe('roundtrip features', function() { - var bef = (function() { X = require(modp); XLSX_ZAHL=require("./dist/xlsx.zahl"); }); - if(typeof before != 'undefined') before(bef); - else it('before', bef); describe('should preserve core properties', function() { [ ['xls', paths.cpxls], ['xlml', paths.cpxml], diff --git a/tests/core.js b/tests/core.js index 5894841..8cf0fcc 100644 --- a/tests/core.js +++ b/tests/core.js @@ -13,10 +13,8 @@ declare var before:(test:EmptyFunc)=>void; declare var afterEach:(test:EmptyFunc)=>void; declare var cptable: any; */ -var X, XLSX_ZAHL = XLSX_ZAHL_PAYLOAD; -var modp = './'; +var X = require("./"), XLSX_ZAHL = require("./dist/xlsx.zahl"); var fs = require('fs'), assert = require('assert'); -describe('source',function(){it('should load',function(){X=require(modp);});}); var DIF_XL = true; var browser = typeof document !== 'undefined'; @@ -27,7 +25,7 @@ var Buffer_from = /*::(*/function(){}/*:: :any)*/; if(typeof Buffer !== 'undefined') { var nbfs = !Buffer.from; - if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } + if(!nbfs) try { Buffer.from("foo", "utf-8"); } catch(e) { nbfs = true; } Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); // $FlowIgnore if(!Buffer.alloc) Buffer.alloc = function(n) { return new Buffer(n); }; @@ -398,11 +396,6 @@ function check_comments(wb) { describe('parse options', function() { var html_cell_types = ['s']; - var bef = (function() { - X = require(modp); - }); - if(typeof before != 'undefined') before(bef); - else it('before', bef); describe('cell', function() { it('XLSX should generate HTML by default', function() { var wb = X.read(fs.readFileSync(paths.cstxlsx), {type:TYPE}); @@ -750,7 +743,6 @@ describe('output formats', function() { ["prn", false, true] ]; function RT(T) { - if(!X) X = require(modp); fmts.forEach(function(fmt) { var wb = X.utils.book_new(); X.utils.book_append_sheet(wb, X.utils.aoa_to_sheet([['R',"\u2603"],["\u0BEE",2]]), "Sheet1"); @@ -929,7 +921,6 @@ describe('parse features', function() { describe('comments', function() { if(fs.existsSync(paths.swcxlsx)) it('should have comment as part of cell properties', function(){ - X = require(modp); var sheet = 'Sheet1'; var wb1=X.read(fs.readFileSync(paths.swcxlsx), {type:TYPE}); var wb2=X.read(fs.readFileSync(paths.swcxlsb), {type:TYPE}); @@ -1034,7 +1025,6 @@ describe('parse features', function() { describe('column properties', function() { var wbs = [], wbs_no_slk = []; var bef = (function() { - X = require(modp); wbs = CWPaths.map(function(n) { return X.read(fs.readFileSync(n), {type:TYPE, cellStyles:true}); }); wbs_no_slk = wbs.slice(0, 5); }); @@ -1077,7 +1067,6 @@ describe('parse features', function() { var wbs = [], ols = []; var ol = fs.existsSync(paths.olxls); var bef = (function() { - X = require(modp); wbs = RHPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, cellStyles:true}); }); /* */ if(!ol) return; @@ -1123,7 +1112,6 @@ describe('parse features', function() { describe('merge cells',function() { var wbs=[]; var bef = (function() { - X = require(modp); wbs = MCPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE}); }); }); if(typeof before != 'undefined') before(bef); @@ -1142,7 +1130,6 @@ describe('parse features', function() { describe('should find hyperlinks', function() { var wb1, wb2; var bef = (function() { - X = require(modp); wb1 = HLPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, WTF:true}); }); wb2 = ILPaths.map(function(p) { return X.read(fs.readFileSync(p), {type:TYPE, WTF:true}); }); }); @@ -1482,7 +1469,6 @@ describe('write features', function() { }; var ws; var bef = (function() { - X = require(modp); ws = X.utils.aoa_to_sheet([["a","b","c"],[1,2,3]]); }); if(typeof before != 'undefined') before(bef); @@ -1560,6 +1546,21 @@ describe('write features', function() { assert.equal(Name.Ref, "Sheet1!$A$1:$C$3"); assert.equal(wb2.Workbook.Names.length, wb3.Workbook.Names.length); }); }); + it('should handle non-string values for "s" cells', function() {[ + "xlsx", "xlsb", "xls", "biff5", "biff2", "xlml", "numbers", "ods", "fods", "wk3", "csv", "txt", "sylk", "html", "dif", "dbf", "wk1", "rtf", "prn" + ].forEach(function(fmt) { + var ws = X.utils.aoa_to_sheet([ + ["String", "123"], + ["Number", 123], + ["Boolean", true], + ["Date", new Date()], + ], { cellDates: true }); + ws["B2"].t = ws["B3"].t = ws["B4"].t = "s" + var wb = X.utils.book_new(); + X.utils.book_append_sheet(wb, ws, "Sheet1"); + X.write(wb, {type: TYPE, bookType: fmt, bookSST: false, numbers:XLSX_ZAHL}); + X.write(wb, {type: TYPE, bookType: fmt, bookSST: true, numbers:XLSX_ZAHL}); + }); }); }); function seq(end/*:number*/, start/*:?number*/)/*:Array*/ { @@ -1596,9 +1597,6 @@ function parseDate(str/*:string|Date*/)/*:Date*/ { var fixdate = browser ? parseDate("2014-02-19T14:30:00.000Z") : new Date("2014-02-19T14:30Z"); describe('roundtrip features', function() { - var bef = (function() { X = require(modp); XLSX_ZAHL=require("./dist/xlsx.zahl"); }); - if(typeof before != 'undefined') before(bef); - else it('before', bef); describe('should preserve core properties', function() { [ ['xls', paths.cpxls], ['xlml', paths.cpxml], @@ -2128,7 +2126,7 @@ function plaintext_test(wb, raw) { var sheet = wb.Sheets[wb.SheetNames[0]]; plaintext_val.forEach(function(x) { var cell = get_cell(sheet, x[0]); - var tcval = x[2+(!!raw ? 1 : 0)]; + var tcval = x[2+(raw ? 1 : 0)]; var type = raw ? 's' : x[1]; if(x.length == 1) { if(cell) { assert.equal(cell.t, 'z'); assert.ok(!cell.v); } return; } assert.equal(cell.v, tcval); assert.equal(cell.t, type); diff --git a/xlsx.flow.js b/xlsx.flow.js index fb0fb78..6d41115 100644 --- a/xlsx.flow.js +++ b/xlsx.flow.js @@ -7958,7 +7958,7 @@ function sheet_to_dbf(ws/*:Worksheet*/, opts/*:WriteOpts*/) { var cp = +dbf_reverse_map[/*::String(*/current_codepage/*::)*/] || 0x03; h.write_shift(4, 0x00000000 | (cp<<8)); if(dbf_codepage_map[cp] != +o.codepage) { - console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); + if(o.codepage) console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); current_codepage = 1252; } @@ -8225,7 +8225,7 @@ var SYLK = /*#__PURE__*/(function() { case 'b': o += cell.v ? "TRUE" : "FALSE"; break; case 'e': o += cell.w || cell.v; break; case 'd': o += '"' + (cell.w || cell.v) + '"'; break; - case 's': o += '"' + cell.v.replace(/"/g,"").replace(/;/g, ";;") + '"'; break; + case 's': o += '"' + (cell.v == null ? "" : String(cell.v)).replace(/"/g,"").replace(/;/g, ";;") + '"'; break; } return o; } @@ -10005,6 +10005,7 @@ function write_sst_xml(sst/*:SST*/, opts)/*:string*/ { else { sitag += ""; } @@ -15543,7 +15544,10 @@ function write_ws_xml_cell(cell/*:Cell*/, ref, ws, opts/*::, idx, wb*/)/*:string var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null; v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : ""); } - if(cell.l) ws['!links'].push([ref, cell.l]); + if(cell.l) { + cell.l.display = escapexml(vv); + ws['!links'].push([ref, cell.l]); + } if(cell.D) o.cm = 1; return writextag('c', v, o); } @@ -15853,6 +15857,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ { } if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1)); if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip); + rel.display = l[1].display; o[o.length] = writextag("hyperlink",null,rel); }); o[o.length] = ""; @@ -16157,9 +16162,10 @@ function parse_BrtCellSt(data) { return [cell, value, 'str']; } function write_BrtCellSt(cell, ncell, o) { + var data = cell.v == null ? "" : String(cell.v); if(o == null) o = new_buf(12 + 4 * cell.v.length); write_XLSBCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } function parse_BrtShortSt(data) { @@ -16168,9 +16174,10 @@ function parse_BrtShortSt(data) { return [cell, value, 'str']; } function write_BrtShortSt(cell, ncell, o) { - if(o == null) o = new_buf(8 + 4 * cell.v.length); + var data = cell.v == null ? "" : String(cell.v); + if(o == null) o = new_buf(8 + 4 * data.length); write_XLSBShortCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } @@ -16723,7 +16730,7 @@ function write_ws_bin_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:num switch(cell.t) { case 's': case 'str': if(opts.bookSST) { - vv = get_sst_id(opts.Strings, (cell.v/*:any*/), opts.revStrings); + vv = get_sst_id(opts.Strings, (cell.v == null ? "" : String(cell.v)/*:any*/), opts.revStrings); o.t = "s"; o.v = vv; if(last_seen) write_record(ba, 0x0012 /* BrtShortIsst */, write_BrtShortIsst(cell, o)); else write_record(ba, 0x0007 /* BrtCellIsst */, write_BrtCellIsst(cell, o)); @@ -21269,7 +21276,7 @@ function write_ws_biff2_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return; /* TODO: codepage, sst */ case 's': case 'str': - write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, (cell.v||"").slice(0,255))); + write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v == null ? "" : String(cell.v).slice(0,255))); return; } write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C)); @@ -21393,9 +21400,9 @@ function write_ws_biff8_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n /* TODO: codepage, sst */ case 's': case 'str': if(opts.bookSST) { - var isst = get_sst_id(opts.Strings, cell.v, opts.revStrings); + var isst = get_sst_id(opts.Strings, cell.v == null ? "" : String(cell.v), opts.revStrings); write_biff_rec(ba, 0x00fd /* LabelSst */, write_LabelSst(R, C, isst, os, opts)); - } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v||"").slice(0,255), os, opts)); + } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v == null ? "" : String(cell.v)).slice(0,255), os, opts)); break; default: write_biff_rec(ba, 0x0201 /* Blank */, write_XLSCell(R, C, os)); @@ -23279,6 +23286,39 @@ function varint_to_i32(buf) { } return i32; } +function varint_to_u64(buf) { + var l = 0, lo = buf[l] & 127, hi = 0; + varint: + if (buf[l++] >= 128) { + lo |= (buf[l] & 127) << 7; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 14; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 21; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 28; + hi = buf[l] >> 4 & 7; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 3; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 10; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 17; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 24; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 31; + } + return [lo >>> 0, hi >>> 0]; +} function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { @@ -23815,10 +23855,12 @@ function write_new_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[1] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 8; l += 4; break; @@ -23845,10 +23887,12 @@ function write_old_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[2] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 16; l += 4; break; @@ -23882,6 +23926,22 @@ function write_TSP_Reference(idx) { [{ type: 0, data: write_varint49(idx) }] ]); } +function numbers_add_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + var orefidx = orefs.indexOf(ref); + if (orefidx == -1) { + orefs.push(ref); + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; + } +} +function numbers_del_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs.filter(function(r) { + return r != ref; + })) }]; +} function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); @@ -23889,6 +23949,8 @@ function parse_TST_TableDataList(M, root) { var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); + if (!le[1]) + return; var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: @@ -24098,7 +24160,7 @@ function parse_TN_DocumentArchive(M, root, opts) { return out; } function parse_numbers_iwa(cfb, opts) { - var _a, _b, _c, _d, _e, _f, _g, _h; + var _a, _b, _c, _d, _e, _f, _g; var M = {}, indices = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) @@ -24128,9 +24190,9 @@ function parse_numbers_iwa(cfb, opts) { }); if (!indices.length) throw new Error("File has no messages"); - if (((_d = (_c = (_b = (_a = M == null ? void 0 : M[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(M[1][0].meta[1][0].data) == 1e4) + if (((_c = (_b = (_a = M == null ? void 0 : M[1]) == null ? void 0 : _a[0].meta) == null ? void 0 : _b[1]) == null ? void 0 : _c[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1e4) throw new Error("Pages documents are not supported"); - var docroot = ((_h = (_g = (_f = (_e = M == null ? void 0 : M[1]) == null ? void 0 : _e[0]) == null ? void 0 : _f.meta) == null ? void 0 : _g[1]) == null ? void 0 : _h[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; + var docroot = ((_g = (_f = (_e = (_d = M == null ? void 0 : M[1]) == null ? void 0 : _d[0]) == null ? void 0 : _e.meta) == null ? void 0 : _f[1]) == null ? void 0 : _g[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; if (!docroot) indices.forEach(function(idx) { M[idx].forEach(function(iwam) { @@ -24147,8 +24209,23 @@ function parse_numbers_iwa(cfb, opts) { throw new Error("Cannot find Document root"); return parse_TN_DocumentArchive(M, docroot, opts); } -function write_tile_row(tri, data, SST, wide) { +function write_TST_TileRowInfo(data, SST, wide) { var _a, _b; + var tri = [ + [], + [{ type: 0, data: write_varint49(0) }], + [{ type: 0, data: write_varint49(0) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(5) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(1) }] + ]; if (!((_a = tri[6]) == null ? void 0 : _a[0]) || !((_b = tri[7]) == null ? void 0 : _b[0])) throw "Mutation only works on post-BNC storages!"; var cnt = 0; @@ -24188,6 +24265,11 @@ function write_tile_row(tri, data, SST, wide) { _celload = write_old_storage({ t: "b", v: data[C] }, SST); break; default: + if (data[C] instanceof Date) { + celload = write_new_storage({ t: "s", v: data[C].toISOString() }, SST); + _celload = write_old_storage({ t: "s", v: data[C].toISOString() }, SST); + break; + } throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); @@ -24207,7 +24289,7 @@ function write_tile_row(tri, data, SST, wide) { tri[6][0].data = u8concat(cell_storage); tri[3][0].data = u8concat(_cell_storage); tri[8] = [{ type: 0, data: write_varint49(wide ? 1 : 0) }]; - return cnt; + return tri; } function write_iwam(type, payload) { return { @@ -24275,19 +24357,20 @@ function write_numbers_iwa(wb, opts) { if (!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README"); var cfb = CFB.read(opts.numbers, { type: "base64" }); - var dependents = build_numbers_deps(cfb); - var cfb_DA = CFB.find(cfb, dependents[1].location); - if (!cfb_DA) - throw "Could not find ".concat(dependents[1].location, " in Numbers template"); - var iwa_DA = parse_iwa_file(decompress_iwa_file(cfb_DA.content)); - var docroot = iwa_DA.find(function(packet) { - return packet.id == 1; - }); + var deps = build_numbers_deps(cfb); + var docroot = numbers_iwa_find(cfb, deps, 1); if (docroot == null) throw "Could not find message ".concat(1, " in Numbers template"); var sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + if (sheetrefs.length > 1) + throw new Error("Template NUMBERS file must have exactly one sheet"); wb.SheetNames.forEach(function(name, idx) { - return write_numbers_ws(cfb, dependents, wb.Sheets[name], name, idx, sheetrefs[idx]); + if (idx >= 1) { + numbers_add_ws(cfb, deps, idx + 1); + docroot = numbers_iwa_find(cfb, deps, 1); + sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + } + write_numbers_ws(cfb, deps, wb.Sheets[name], name, idx, sheetrefs[idx]); }); return cfb; } @@ -24313,9 +24396,364 @@ function numbers_iwa_find(cfb, deps, id) { }); return ainfo; } +function numbers_add_ws(cfb, deps, wsidx) { + var sheetref = -1, newsheetref = -1; + var remap = {}; + numbers_iwa_doit(cfb, deps, 1, function(docroot, arch) { + var doc = parse_shallow(docroot.messages[0].data); + sheetref = parse_TSP_Reference(parse_shallow(docroot.messages[0].data)[1][0].data); + newsheetref = get_unique_msgid({ deps: [1], location: deps[sheetref].location, type: 2 }, deps); + remap[sheetref] = newsheetref; + numbers_add_oref(docroot, newsheetref); + doc[1].push({ type: 2, data: write_TSP_Reference(newsheetref) }); + var sheet = numbers_iwa_find(cfb, deps, sheetref); + sheet.id = newsheetref; + if (deps[1].location == deps[newsheetref].location) + arch.push(sheet); + else + numbers_iwa_doit(cfb, deps, newsheetref, function(_, x) { + return x.push(sheet); + }); + docroot.messages[0].data = write_shallow(doc); + }); + var tiaref = -1; + numbers_iwa_doit(cfb, deps, newsheetref, function(sheetroot, arch) { + var sa = parse_shallow(sheetroot.messages[0].data); + for (var i = 3; i <= 69; ++i) + delete sa[i]; + var drawables = mappa(sa[2], parse_TSP_Reference); + drawables.forEach(function(n) { + return numbers_del_oref(sheetroot, n); + }); + tiaref = get_unique_msgid({ deps: [newsheetref], location: deps[drawables[0]].location, type: deps[drawables[0]].type }, deps); + numbers_add_oref(sheetroot, tiaref); + remap[drawables[0]] = tiaref; + sa[2] = [{ type: 2, data: write_TSP_Reference(tiaref) }]; + var tia = numbers_iwa_find(cfb, deps, drawables[0]); + tia.id = tiaref; + if (deps[drawables[0]].location == deps[newsheetref].location) + arch.push(tia); + else { + var loc2 = deps[newsheetref].location; + loc2 = loc2.replace(/^Root Entry\//, ""); + loc2 = loc2.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + var parentidx = mlist[3].findIndex(function(m) { + var _a, _b; + var mm = parse_shallow(m.data); + if ((_a = mm[3]) == null ? void 0 : _a[0]) + return u8str(mm[3][0].data) == loc2; + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc2) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(tiaref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + numbers_iwa_doit(cfb, deps, tiaref, function(_, x) { + return x.push(tia); + }); + } + sheetroot.messages[0].data = write_shallow(sa); + }); + var tmaref = -1; + numbers_iwa_doit(cfb, deps, tiaref, function(tiaroot, arch) { + var tia = parse_shallow(tiaroot.messages[0].data); + var da = parse_shallow(tia[1][0].data); + for (var i = 3; i <= 69; ++i) + delete da[i]; + var dap = parse_TSP_Reference(da[2][0].data); + da[2][0].data = write_TSP_Reference(remap[dap]); + tia[1][0].data = write_shallow(da); + var oldtmaref = parse_TSP_Reference(tia[2][0].data); + numbers_del_oref(tiaroot, oldtmaref); + tmaref = get_unique_msgid({ deps: [tiaref], location: deps[oldtmaref].location, type: deps[oldtmaref].type }, deps); + numbers_add_oref(tiaroot, tmaref); + remap[oldtmaref] = tmaref; + tia[2][0].data = write_TSP_Reference(tmaref); + var tma = numbers_iwa_find(cfb, deps, oldtmaref); + tma.id = tmaref; + if (deps[tiaref].location == deps[tmaref].location) + arch.push(tma); + else + numbers_iwa_doit(cfb, deps, tmaref, function(_, x) { + return x.push(tma); + }); + tiaroot.messages[0].data = write_shallow(tia); + }); + var loc = deps[tmaref].location; + loc = loc.replace(/^Root Entry\//, ""); + loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, tmaref, function(tmaroot, arch) { + var _a, _b; + var tma = parse_shallow(tmaroot.messages[0].data); + var uuid = u8str(tma[1][0].data), new_uuid = uuid.replace(/-[A-Z0-9]*/, "-".concat(wsidx.toString(16).padStart(4, "0"))); + tma[1][0].data = stru8(new_uuid); + [12, 13, 29, 31, 32, 33, 39, 44, 47, 81, 82, 84].forEach(function(n) { + return delete tma[n]; + }); + if (tma[45]) { + var srrta = parse_shallow(tma[45][0].data); + var ref = parse_TSP_Reference(srrta[1][0].data); + numbers_del_oref(tmaroot, ref); + delete tma[45]; + } + if (tma[70]) { + var hsoa = parse_shallow(tma[70][0].data); + (_a = hsoa[2]) == null ? void 0 : _a.forEach(function(item) { + var hsa = parse_shallow(item.data); + [2, 3].map(function(n) { + return hsa[n][0]; + }).forEach(function(hseadata) { + var hsea = parse_shallow(hseadata.data); + if (!hsea[8]) + return; + var ref2 = parse_TSP_Reference(hsea[8][0].data); + numbers_del_oref(tmaroot, ref2); + }); + }); + delete tma[70]; + } + [ + 46, + 30, + 34, + 35, + 36, + 38, + 48, + 49, + 60, + 61, + 62, + 63, + 64, + 71, + 72, + 73, + 74, + 75, + 85, + 86, + 87, + 88, + 89 + ].forEach(function(n) { + if (!tma[n]) + return; + var ref2 = parse_TSP_Reference(tma[n][0].data); + delete tma[n]; + numbers_del_oref(tmaroot, ref2); + }); + var store = parse_shallow(tma[4][0].data); + { + [2, 4, 5, 6, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22].forEach(function(n) { + var _a2; + if (!((_a2 = store[n]) == null ? void 0 : _a2[0])) + return; + var oldref = parse_TSP_Reference(store[n][0].data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) + arch.push(msg); + else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a3, _b2; + var mm = parse_shallow(m.data); + if ((_a3 = mm[3]) == null ? void 0 : _a3[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + store[n][0].data = write_TSP_Reference(newref); + }); + var row_headers = parse_shallow(store[1][0].data); + { + (_b = row_headers[2]) == null ? void 0 : _b.forEach(function(tspref) { + var oldref = parse_TSP_Reference(tspref.data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) { + arch.push(msg); + } else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tspref.data = write_TSP_Reference(newref); + }); + } + store[1][0].data = write_shallow(row_headers); + var tiles = parse_shallow(store[3][0].data); + { + tiles[1].forEach(function(t) { + var tst = parse_shallow(t.data); + var oldtileref = parse_TSP_Reference(tst[2][0].data); + var newtileref = remap[oldtileref]; + if (!remap[oldtileref]) { + newtileref = get_unique_msgid({ deps: [tmaref], location: "", type: deps[oldtileref].type }, deps); + deps[newtileref].location = "Root Entry/Index/Tables/Tile-".concat(newtileref, ".iwa"); + remap[oldtileref] = newtileref; + var oldtile = numbers_iwa_find(cfb, deps, oldtileref); + oldtile.id = newtileref; + numbers_del_oref(tmaroot, oldtileref); + numbers_add_oref(tmaroot, newtileref); + CFB.utils.cfb_add(cfb, "/Index/Tables/Tile-".concat(newtileref, ".iwa"), compress_iwa_file(write_iwa_file([oldtile]))); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }], + [{ type: 2, data: stru8("Tables/Tile") }], + [{ type: 2, data: stru8("Tables/Tile-".concat(newtileref)) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tst[2][0].data = write_TSP_Reference(newtileref); + t.data = write_shallow(tst); + }); + } + store[3][0].data = write_shallow(tiles); + } + tma[4][0].data = write_shallow(store); + tmaroot.messages[0].data = write_shallow(tma); + }); +} function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { - if (sheetidx >= 1) - return console.error("The Numbers writer currently writes only the first table"); var drawables = []; numbers_iwa_doit(cfb, deps, rootref, function(docroot) { var sheetref = parse_shallow(docroot.messages[0].data); @@ -24333,7 +24771,6 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { - var _a, _b; var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; @@ -24348,15 +24785,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { if (trunc) console.error("Truncating to ".concat(encode_range(range))); var data = sheet_to_json(ws, { range: range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(function(row) { - return row.forEach(function(cell) { - if (typeof cell == "string" && !SST_set.has(cell)) { - SST.push(cell); - SST_set.add(cell); - } - }); - }); + var SST = ["~Sh33tJ5~"]; var loc = deps[tmaref].location; loc = loc.replace(/^Root Entry\//, ""); loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); @@ -24369,9 +24798,9 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { { var row_header_ref = parse_TSP_Reference(parse_shallow(store[1][0].data)[2][0].data); numbers_iwa_doit(cfb, deps, row_header_ref, function(rowhead, _x) { - var _a2; + var _a; var base_bucket = parse_shallow(rowhead.messages[0].data); - if ((_a2 = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a2[0]) + if ((_a = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a[0]) for (var R2 = 0; R2 < data.length; ++R2) { var _bucket = parse_shallow(base_bucket[2][0].data); _bucket[1][0].data = write_varint49(R2); @@ -24391,22 +24820,6 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { } colhead.messages[0].data = write_shallow(base_bucket); }); - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach(function(str, i) { - sstdata[3].push({ type: 2, data: write_shallow([ - [], - [{ type: 0, data: write_varint49(i) }], - [{ type: 0, data: write_varint49(1) }], - [{ type: 2, data: stru8(str) }] - ]) }); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; var tilestore = parse_shallow(store[3][0].data); @@ -24414,25 +24827,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { var tstride = 256; tilestore[2] = [{ type: 0, data: write_varint49(tstride) }]; var tileref = parse_TSP_Reference(parse_shallow(tilestore[1][0].data)[2][0].data); - var save_token = 0; + var save_token = function() { + var metadata = numbers_iwa_find(cfb, deps, 2); + var mlist = parse_shallow(metadata.messages[0].data); + var mlst = mlist[3].filter(function(m) { + return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; + }); + return (mlst == null ? void 0 : mlst.length) ? parse_varint49(parse_shallow(mlst[0].data)[12][0].data) : 0; + }(); { CFB.utils.cfb_del(cfb, deps[tileref].location); numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); - var lst = mlist[3].filter(function(m) { - return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; - }); - if (lst && lst.length > 0) - save_token = parse_varint49(parse_shallow(lst[0].data)[12][0].data); mlist[3] = mlist[3].filter(function(m) { return parse_varint49(parse_shallow(m.data)[1][0].data) != tileref; }); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24445,6 +24860,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); + numbers_del_oref(tmaroot, tileref); } tilestore[1] = []; var ntiles = Math.ceil((range.e.r + 1) / tstride); @@ -24467,22 +24883,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { [{ type: 0, data: write_varint49(USE_WIDE_ROWS ? 1 : 0) }] ]; for (var R = tidx * tstride; R <= Math.min(range.e.r, (tidx + 1) * tstride - 1); ++R) { - var tilerow = [ - [], - [{ type: 0, data: write_varint49(0) }], - [{ type: 0, data: write_varint49(0) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(5) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(1) }] - ]; - write_tile_row(tilerow, data[R], SST, USE_WIDE_ROWS); + var tilerow = write_TST_TileRowInfo(data[R], SST, USE_WIDE_ROWS); tilerow[1][0].data = write_varint49(R - tidx * tstride); tiledata[5].push({ data: write_shallow(tilerow), type: 2 }); } @@ -24516,11 +24917,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { ]) }); mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileid + 1, parse_varint49(mlist[1][0].data))) }]; var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24537,12 +24938,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - var orefs = ((_a = tmaroot.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - var orefidx = orefs.indexOf(newtileid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = newtileid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, newtileid); rbtree[1].push({ type: 2, data: write_shallow([ [], [{ type: 0, data: write_varint49(tidx * tstride) }], @@ -24582,11 +24978,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24603,14 +24999,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - orefs = ((_b = tmaroot.messages[0].meta[5]) == null ? void 0 : _b[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - orefidx = orefs.indexOf(mergeid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = mergeid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, mergeid); } else delete store[13]; + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach(function(str, i) { + if (i == 0) + return; + sstdata[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(i) }], + [{ type: 0, data: write_varint49(1) }], + [{ type: 2, data: stru8(str) }] + ]) }); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); } pb[4][0].data = write_shallow(store); } diff --git a/xlsx.js b/xlsx.js index fd8cf59..ac0394d 100644 --- a/xlsx.js +++ b/xlsx.js @@ -7868,7 +7868,7 @@ function sheet_to_dbf(ws, opts) { var cp = +dbf_reverse_map[current_codepage] || 0x03; h.write_shift(4, 0x00000000 | (cp<<8)); if(dbf_codepage_map[cp] != +o.codepage) { - console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); + if(o.codepage) console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); current_codepage = 1252; } @@ -8135,7 +8135,7 @@ var SYLK = (function() { case 'b': o += cell.v ? "TRUE" : "FALSE"; break; case 'e': o += cell.w || cell.v; break; case 'd': o += '"' + (cell.w || cell.v) + '"'; break; - case 's': o += '"' + cell.v.replace(/"/g,"").replace(/;/g, ";;") + '"'; break; + case 's': o += '"' + (cell.v == null ? "" : String(cell.v)).replace(/"/g,"").replace(/;/g, ";;") + '"'; break; } return o; } @@ -9915,6 +9915,7 @@ function write_sst_xml(sst, opts) { else { sitag += ""; } @@ -15449,7 +15450,10 @@ function write_ws_xml_cell(cell, ref, ws, opts) { var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null; v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : ""); } - if(cell.l) ws['!links'].push([ref, cell.l]); + if(cell.l) { + cell.l.display = escapexml(vv); + ws['!links'].push([ref, cell.l]); + } if(cell.D) o.cm = 1; return writextag('c', v, o); } @@ -15759,6 +15763,7 @@ ws['!links'].forEach(function(l) { } if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1)); if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip); + rel.display = l[1].display; o[o.length] = writextag("hyperlink",null,rel); }); o[o.length] = ""; @@ -16063,9 +16068,10 @@ function parse_BrtCellSt(data) { return [cell, value, 'str']; } function write_BrtCellSt(cell, ncell, o) { + var data = cell.v == null ? "" : String(cell.v); if(o == null) o = new_buf(12 + 4 * cell.v.length); write_XLSBCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } function parse_BrtShortSt(data) { @@ -16074,9 +16080,10 @@ function parse_BrtShortSt(data) { return [cell, value, 'str']; } function write_BrtShortSt(cell, ncell, o) { - if(o == null) o = new_buf(8 + 4 * cell.v.length); + var data = cell.v == null ? "" : String(cell.v); + if(o == null) o = new_buf(8 + 4 * data.length); write_XLSBShortCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } @@ -16628,7 +16635,7 @@ function write_ws_bin_cell(ba, cell, R, C, opts, ws, last_seen) { switch(cell.t) { case 's': case 'str': if(opts.bookSST) { - vv = get_sst_id(opts.Strings, (cell.v), opts.revStrings); + vv = get_sst_id(opts.Strings, (cell.v == null ? "" : String(cell.v)), opts.revStrings); o.t = "s"; o.v = vv; if(last_seen) write_record(ba, 0x0012 /* BrtShortIsst */, write_BrtShortIsst(cell, o)); else write_record(ba, 0x0007 /* BrtCellIsst */, write_BrtCellIsst(cell, o)); @@ -21160,7 +21167,7 @@ function write_ws_biff2_cell(ba, cell, R, C) { case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return; /* TODO: codepage, sst */ case 's': case 'str': - write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, (cell.v||"").slice(0,255))); + write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v == null ? "" : String(cell.v).slice(0,255))); return; } write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C)); @@ -21283,9 +21290,9 @@ function write_ws_biff8_cell(ba, cell, R, C, opts) { /* TODO: codepage, sst */ case 's': case 'str': if(opts.bookSST) { - var isst = get_sst_id(opts.Strings, cell.v, opts.revStrings); + var isst = get_sst_id(opts.Strings, cell.v == null ? "" : String(cell.v), opts.revStrings); write_biff_rec(ba, 0x00fd /* LabelSst */, write_LabelSst(R, C, isst, os, opts)); - } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v||"").slice(0,255), os, opts)); + } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v == null ? "" : String(cell.v)).slice(0,255), os, opts)); break; default: write_biff_rec(ba, 0x0201 /* Blank */, write_XLSCell(R, C, os)); @@ -23169,6 +23176,39 @@ function varint_to_i32(buf) { } return i32; } +function varint_to_u64(buf) { + var l = 0, lo = buf[l] & 127, hi = 0; + varint: + if (buf[l++] >= 128) { + lo |= (buf[l] & 127) << 7; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 14; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 21; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 28; + hi = buf[l] >> 4 & 7; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 3; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 10; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 17; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 24; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 31; + } + return [lo >>> 0, hi >>> 0]; +} function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { @@ -23705,10 +23745,12 @@ function write_new_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[1] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 8; l += 4; break; @@ -23735,10 +23777,12 @@ function write_old_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[2] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 16; l += 4; break; @@ -23772,6 +23816,22 @@ function write_TSP_Reference(idx) { [{ type: 0, data: write_varint49(idx) }] ]); } +function numbers_add_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + var orefidx = orefs.indexOf(ref); + if (orefidx == -1) { + orefs.push(ref); + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; + } +} +function numbers_del_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs.filter(function(r) { + return r != ref; + })) }]; +} function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); @@ -23779,6 +23839,8 @@ function parse_TST_TableDataList(M, root) { var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); + if (!le[1]) + return; var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: @@ -23988,7 +24050,7 @@ function parse_TN_DocumentArchive(M, root, opts) { return out; } function parse_numbers_iwa(cfb, opts) { - var _a, _b, _c, _d, _e, _f, _g, _h; + var _a, _b, _c, _d, _e, _f, _g; var M = {}, indices = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) @@ -24018,9 +24080,9 @@ function parse_numbers_iwa(cfb, opts) { }); if (!indices.length) throw new Error("File has no messages"); - if (((_d = (_c = (_b = (_a = M == null ? void 0 : M[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(M[1][0].meta[1][0].data) == 1e4) + if (((_c = (_b = (_a = M == null ? void 0 : M[1]) == null ? void 0 : _a[0].meta) == null ? void 0 : _b[1]) == null ? void 0 : _c[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1e4) throw new Error("Pages documents are not supported"); - var docroot = ((_h = (_g = (_f = (_e = M == null ? void 0 : M[1]) == null ? void 0 : _e[0]) == null ? void 0 : _f.meta) == null ? void 0 : _g[1]) == null ? void 0 : _h[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; + var docroot = ((_g = (_f = (_e = (_d = M == null ? void 0 : M[1]) == null ? void 0 : _d[0]) == null ? void 0 : _e.meta) == null ? void 0 : _f[1]) == null ? void 0 : _g[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; if (!docroot) indices.forEach(function(idx) { M[idx].forEach(function(iwam) { @@ -24037,8 +24099,23 @@ function parse_numbers_iwa(cfb, opts) { throw new Error("Cannot find Document root"); return parse_TN_DocumentArchive(M, docroot, opts); } -function write_tile_row(tri, data, SST, wide) { +function write_TST_TileRowInfo(data, SST, wide) { var _a, _b; + var tri = [ + [], + [{ type: 0, data: write_varint49(0) }], + [{ type: 0, data: write_varint49(0) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(5) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(1) }] + ]; if (!((_a = tri[6]) == null ? void 0 : _a[0]) || !((_b = tri[7]) == null ? void 0 : _b[0])) throw "Mutation only works on post-BNC storages!"; var cnt = 0; @@ -24078,6 +24155,11 @@ function write_tile_row(tri, data, SST, wide) { _celload = write_old_storage({ t: "b", v: data[C] }, SST); break; default: + if (data[C] instanceof Date) { + celload = write_new_storage({ t: "s", v: data[C].toISOString() }, SST); + _celload = write_old_storage({ t: "s", v: data[C].toISOString() }, SST); + break; + } throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); @@ -24097,7 +24179,7 @@ function write_tile_row(tri, data, SST, wide) { tri[6][0].data = u8concat(cell_storage); tri[3][0].data = u8concat(_cell_storage); tri[8] = [{ type: 0, data: write_varint49(wide ? 1 : 0) }]; - return cnt; + return tri; } function write_iwam(type, payload) { return { @@ -24165,19 +24247,20 @@ function write_numbers_iwa(wb, opts) { if (!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README"); var cfb = CFB.read(opts.numbers, { type: "base64" }); - var dependents = build_numbers_deps(cfb); - var cfb_DA = CFB.find(cfb, dependents[1].location); - if (!cfb_DA) - throw "Could not find ".concat(dependents[1].location, " in Numbers template"); - var iwa_DA = parse_iwa_file(decompress_iwa_file(cfb_DA.content)); - var docroot = iwa_DA.find(function(packet) { - return packet.id == 1; - }); + var deps = build_numbers_deps(cfb); + var docroot = numbers_iwa_find(cfb, deps, 1); if (docroot == null) throw "Could not find message ".concat(1, " in Numbers template"); var sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + if (sheetrefs.length > 1) + throw new Error("Template NUMBERS file must have exactly one sheet"); wb.SheetNames.forEach(function(name, idx) { - return write_numbers_ws(cfb, dependents, wb.Sheets[name], name, idx, sheetrefs[idx]); + if (idx >= 1) { + numbers_add_ws(cfb, deps, idx + 1); + docroot = numbers_iwa_find(cfb, deps, 1); + sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + } + write_numbers_ws(cfb, deps, wb.Sheets[name], name, idx, sheetrefs[idx]); }); return cfb; } @@ -24203,9 +24286,364 @@ function numbers_iwa_find(cfb, deps, id) { }); return ainfo; } +function numbers_add_ws(cfb, deps, wsidx) { + var sheetref = -1, newsheetref = -1; + var remap = {}; + numbers_iwa_doit(cfb, deps, 1, function(docroot, arch) { + var doc = parse_shallow(docroot.messages[0].data); + sheetref = parse_TSP_Reference(parse_shallow(docroot.messages[0].data)[1][0].data); + newsheetref = get_unique_msgid({ deps: [1], location: deps[sheetref].location, type: 2 }, deps); + remap[sheetref] = newsheetref; + numbers_add_oref(docroot, newsheetref); + doc[1].push({ type: 2, data: write_TSP_Reference(newsheetref) }); + var sheet = numbers_iwa_find(cfb, deps, sheetref); + sheet.id = newsheetref; + if (deps[1].location == deps[newsheetref].location) + arch.push(sheet); + else + numbers_iwa_doit(cfb, deps, newsheetref, function(_, x) { + return x.push(sheet); + }); + docroot.messages[0].data = write_shallow(doc); + }); + var tiaref = -1; + numbers_iwa_doit(cfb, deps, newsheetref, function(sheetroot, arch) { + var sa = parse_shallow(sheetroot.messages[0].data); + for (var i = 3; i <= 69; ++i) + delete sa[i]; + var drawables = mappa(sa[2], parse_TSP_Reference); + drawables.forEach(function(n) { + return numbers_del_oref(sheetroot, n); + }); + tiaref = get_unique_msgid({ deps: [newsheetref], location: deps[drawables[0]].location, type: deps[drawables[0]].type }, deps); + numbers_add_oref(sheetroot, tiaref); + remap[drawables[0]] = tiaref; + sa[2] = [{ type: 2, data: write_TSP_Reference(tiaref) }]; + var tia = numbers_iwa_find(cfb, deps, drawables[0]); + tia.id = tiaref; + if (deps[drawables[0]].location == deps[newsheetref].location) + arch.push(tia); + else { + var loc2 = deps[newsheetref].location; + loc2 = loc2.replace(/^Root Entry\//, ""); + loc2 = loc2.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + var parentidx = mlist[3].findIndex(function(m) { + var _a, _b; + var mm = parse_shallow(m.data); + if ((_a = mm[3]) == null ? void 0 : _a[0]) + return u8str(mm[3][0].data) == loc2; + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc2) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(tiaref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + numbers_iwa_doit(cfb, deps, tiaref, function(_, x) { + return x.push(tia); + }); + } + sheetroot.messages[0].data = write_shallow(sa); + }); + var tmaref = -1; + numbers_iwa_doit(cfb, deps, tiaref, function(tiaroot, arch) { + var tia = parse_shallow(tiaroot.messages[0].data); + var da = parse_shallow(tia[1][0].data); + for (var i = 3; i <= 69; ++i) + delete da[i]; + var dap = parse_TSP_Reference(da[2][0].data); + da[2][0].data = write_TSP_Reference(remap[dap]); + tia[1][0].data = write_shallow(da); + var oldtmaref = parse_TSP_Reference(tia[2][0].data); + numbers_del_oref(tiaroot, oldtmaref); + tmaref = get_unique_msgid({ deps: [tiaref], location: deps[oldtmaref].location, type: deps[oldtmaref].type }, deps); + numbers_add_oref(tiaroot, tmaref); + remap[oldtmaref] = tmaref; + tia[2][0].data = write_TSP_Reference(tmaref); + var tma = numbers_iwa_find(cfb, deps, oldtmaref); + tma.id = tmaref; + if (deps[tiaref].location == deps[tmaref].location) + arch.push(tma); + else + numbers_iwa_doit(cfb, deps, tmaref, function(_, x) { + return x.push(tma); + }); + tiaroot.messages[0].data = write_shallow(tia); + }); + var loc = deps[tmaref].location; + loc = loc.replace(/^Root Entry\//, ""); + loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, tmaref, function(tmaroot, arch) { + var _a, _b; + var tma = parse_shallow(tmaroot.messages[0].data); + var uuid = u8str(tma[1][0].data), new_uuid = uuid.replace(/-[A-Z0-9]*/, "-".concat(wsidx.toString(16).padStart(4, "0"))); + tma[1][0].data = stru8(new_uuid); + [12, 13, 29, 31, 32, 33, 39, 44, 47, 81, 82, 84].forEach(function(n) { + return delete tma[n]; + }); + if (tma[45]) { + var srrta = parse_shallow(tma[45][0].data); + var ref = parse_TSP_Reference(srrta[1][0].data); + numbers_del_oref(tmaroot, ref); + delete tma[45]; + } + if (tma[70]) { + var hsoa = parse_shallow(tma[70][0].data); + (_a = hsoa[2]) == null ? void 0 : _a.forEach(function(item) { + var hsa = parse_shallow(item.data); + [2, 3].map(function(n) { + return hsa[n][0]; + }).forEach(function(hseadata) { + var hsea = parse_shallow(hseadata.data); + if (!hsea[8]) + return; + var ref2 = parse_TSP_Reference(hsea[8][0].data); + numbers_del_oref(tmaroot, ref2); + }); + }); + delete tma[70]; + } + [ + 46, + 30, + 34, + 35, + 36, + 38, + 48, + 49, + 60, + 61, + 62, + 63, + 64, + 71, + 72, + 73, + 74, + 75, + 85, + 86, + 87, + 88, + 89 + ].forEach(function(n) { + if (!tma[n]) + return; + var ref2 = parse_TSP_Reference(tma[n][0].data); + delete tma[n]; + numbers_del_oref(tmaroot, ref2); + }); + var store = parse_shallow(tma[4][0].data); + { + [2, 4, 5, 6, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22].forEach(function(n) { + var _a2; + if (!((_a2 = store[n]) == null ? void 0 : _a2[0])) + return; + var oldref = parse_TSP_Reference(store[n][0].data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) + arch.push(msg); + else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a3, _b2; + var mm = parse_shallow(m.data); + if ((_a3 = mm[3]) == null ? void 0 : _a3[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + store[n][0].data = write_TSP_Reference(newref); + }); + var row_headers = parse_shallow(store[1][0].data); + { + (_b = row_headers[2]) == null ? void 0 : _b.forEach(function(tspref) { + var oldref = parse_TSP_Reference(tspref.data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) { + arch.push(msg); + } else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tspref.data = write_TSP_Reference(newref); + }); + } + store[1][0].data = write_shallow(row_headers); + var tiles = parse_shallow(store[3][0].data); + { + tiles[1].forEach(function(t) { + var tst = parse_shallow(t.data); + var oldtileref = parse_TSP_Reference(tst[2][0].data); + var newtileref = remap[oldtileref]; + if (!remap[oldtileref]) { + newtileref = get_unique_msgid({ deps: [tmaref], location: "", type: deps[oldtileref].type }, deps); + deps[newtileref].location = "Root Entry/Index/Tables/Tile-".concat(newtileref, ".iwa"); + remap[oldtileref] = newtileref; + var oldtile = numbers_iwa_find(cfb, deps, oldtileref); + oldtile.id = newtileref; + numbers_del_oref(tmaroot, oldtileref); + numbers_add_oref(tmaroot, newtileref); + CFB.utils.cfb_add(cfb, "/Index/Tables/Tile-".concat(newtileref, ".iwa"), compress_iwa_file(write_iwa_file([oldtile]))); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }], + [{ type: 2, data: stru8("Tables/Tile") }], + [{ type: 2, data: stru8("Tables/Tile-".concat(newtileref)) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tst[2][0].data = write_TSP_Reference(newtileref); + t.data = write_shallow(tst); + }); + } + store[3][0].data = write_shallow(tiles); + } + tma[4][0].data = write_shallow(store); + tmaroot.messages[0].data = write_shallow(tma); + }); +} function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { - if (sheetidx >= 1) - return console.error("The Numbers writer currently writes only the first table"); var drawables = []; numbers_iwa_doit(cfb, deps, rootref, function(docroot) { var sheetref = parse_shallow(docroot.messages[0].data); @@ -24223,7 +24661,6 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { - var _a, _b; var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; @@ -24238,15 +24675,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { if (trunc) console.error("Truncating to ".concat(encode_range(range))); var data = sheet_to_json(ws, { range: range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(function(row) { - return row.forEach(function(cell) { - if (typeof cell == "string" && !SST_set.has(cell)) { - SST.push(cell); - SST_set.add(cell); - } - }); - }); + var SST = ["~Sh33tJ5~"]; var loc = deps[tmaref].location; loc = loc.replace(/^Root Entry\//, ""); loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); @@ -24259,9 +24688,9 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { { var row_header_ref = parse_TSP_Reference(parse_shallow(store[1][0].data)[2][0].data); numbers_iwa_doit(cfb, deps, row_header_ref, function(rowhead, _x) { - var _a2; + var _a; var base_bucket = parse_shallow(rowhead.messages[0].data); - if ((_a2 = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a2[0]) + if ((_a = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a[0]) for (var R2 = 0; R2 < data.length; ++R2) { var _bucket = parse_shallow(base_bucket[2][0].data); _bucket[1][0].data = write_varint49(R2); @@ -24281,22 +24710,6 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { } colhead.messages[0].data = write_shallow(base_bucket); }); - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach(function(str, i) { - sstdata[3].push({ type: 2, data: write_shallow([ - [], - [{ type: 0, data: write_varint49(i) }], - [{ type: 0, data: write_varint49(1) }], - [{ type: 2, data: stru8(str) }] - ]) }); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; var tilestore = parse_shallow(store[3][0].data); @@ -24304,25 +24717,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { var tstride = 256; tilestore[2] = [{ type: 0, data: write_varint49(tstride) }]; var tileref = parse_TSP_Reference(parse_shallow(tilestore[1][0].data)[2][0].data); - var save_token = 0; + var save_token = function() { + var metadata = numbers_iwa_find(cfb, deps, 2); + var mlist = parse_shallow(metadata.messages[0].data); + var mlst = mlist[3].filter(function(m) { + return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; + }); + return (mlst == null ? void 0 : mlst.length) ? parse_varint49(parse_shallow(mlst[0].data)[12][0].data) : 0; + }(); { CFB.utils.cfb_del(cfb, deps[tileref].location); numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); - var lst = mlist[3].filter(function(m) { - return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; - }); - if (lst && lst.length > 0) - save_token = parse_varint49(parse_shallow(lst[0].data)[12][0].data); mlist[3] = mlist[3].filter(function(m) { return parse_varint49(parse_shallow(m.data)[1][0].data) != tileref; }); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24335,6 +24750,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); + numbers_del_oref(tmaroot, tileref); } tilestore[1] = []; var ntiles = Math.ceil((range.e.r + 1) / tstride); @@ -24357,22 +24773,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { [{ type: 0, data: write_varint49(USE_WIDE_ROWS ? 1 : 0) }] ]; for (var R = tidx * tstride; R <= Math.min(range.e.r, (tidx + 1) * tstride - 1); ++R) { - var tilerow = [ - [], - [{ type: 0, data: write_varint49(0) }], - [{ type: 0, data: write_varint49(0) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(5) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(1) }] - ]; - write_tile_row(tilerow, data[R], SST, USE_WIDE_ROWS); + var tilerow = write_TST_TileRowInfo(data[R], SST, USE_WIDE_ROWS); tilerow[1][0].data = write_varint49(R - tidx * tstride); tiledata[5].push({ data: write_shallow(tilerow), type: 2 }); } @@ -24406,11 +24807,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { ]) }); mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileid + 1, parse_varint49(mlist[1][0].data))) }]; var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24427,12 +24828,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - var orefs = ((_a = tmaroot.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - var orefidx = orefs.indexOf(newtileid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = newtileid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, newtileid); rbtree[1].push({ type: 2, data: write_shallow([ [], [{ type: 0, data: write_varint49(tidx * tstride) }], @@ -24472,11 +24868,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24493,14 +24889,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - orefs = ((_b = tmaroot.messages[0].meta[5]) == null ? void 0 : _b[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - orefidx = orefs.indexOf(mergeid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = mergeid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, mergeid); } else delete store[13]; + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach(function(str, i) { + if (i == 0) + return; + sstdata[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(i) }], + [{ type: 0, data: write_varint49(1) }], + [{ type: 2, data: stru8(str) }] + ]) }); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); } pb[4][0].data = write_shallow(store); } diff --git a/xlsx.mjs b/xlsx.mjs index 170c013..dd52fd4 100644 --- a/xlsx.mjs +++ b/xlsx.mjs @@ -7953,7 +7953,7 @@ function sheet_to_dbf(ws/*:Worksheet*/, opts/*:WriteOpts*/) { var cp = +dbf_reverse_map[/*::String(*/current_codepage/*::)*/] || 0x03; h.write_shift(4, 0x00000000 | (cp<<8)); if(dbf_codepage_map[cp] != +o.codepage) { - console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); + if(o.codepage) console.error("DBF Unsupported codepage " + current_codepage + ", using 1252"); current_codepage = 1252; } @@ -8220,7 +8220,7 @@ var SYLK = /*#__PURE__*/(function() { case 'b': o += cell.v ? "TRUE" : "FALSE"; break; case 'e': o += cell.w || cell.v; break; case 'd': o += '"' + (cell.w || cell.v) + '"'; break; - case 's': o += '"' + cell.v.replace(/"/g,"").replace(/;/g, ";;") + '"'; break; + case 's': o += '"' + (cell.v == null ? "" : String(cell.v)).replace(/"/g,"").replace(/;/g, ";;") + '"'; break; } return o; } @@ -10000,6 +10000,7 @@ function write_sst_xml(sst/*:SST*/, opts)/*:string*/ { else { sitag += ""; } @@ -15538,7 +15539,10 @@ function write_ws_xml_cell(cell/*:Cell*/, ref, ws, opts/*::, idx, wb*/)/*:string var ff = cell.F && cell.F.slice(0, ref.length) == ref ? {t:"array", ref:cell.F} : null; v = writextag('f', escapexml(cell.f), ff) + (cell.v != null ? v : ""); } - if(cell.l) ws['!links'].push([ref, cell.l]); + if(cell.l) { + cell.l.display = escapexml(vv); + ws['!links'].push([ref, cell.l]); + } if(cell.D) o.cm = 1; return writextag('c', v, o); } @@ -15848,6 +15852,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ { } if((relc = l[1].Target.indexOf("#")) > -1) rel.location = escapexml(l[1].Target.slice(relc+1)); if(l[1].Tooltip) rel.tooltip = escapexml(l[1].Tooltip); + rel.display = l[1].display; o[o.length] = writextag("hyperlink",null,rel); }); o[o.length] = ""; @@ -16152,9 +16157,10 @@ function parse_BrtCellSt(data) { return [cell, value, 'str']; } function write_BrtCellSt(cell, ncell, o) { + var data = cell.v == null ? "" : String(cell.v); if(o == null) o = new_buf(12 + 4 * cell.v.length); write_XLSBCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } function parse_BrtShortSt(data) { @@ -16163,9 +16169,10 @@ function parse_BrtShortSt(data) { return [cell, value, 'str']; } function write_BrtShortSt(cell, ncell, o) { - if(o == null) o = new_buf(8 + 4 * cell.v.length); + var data = cell.v == null ? "" : String(cell.v); + if(o == null) o = new_buf(8 + 4 * data.length); write_XLSBShortCell(ncell, o); - write_XLWideString(cell.v, o); + write_XLWideString(data, o); return o.length > o.l ? o.slice(0, o.l) : o; } @@ -16718,7 +16725,7 @@ function write_ws_bin_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:num switch(cell.t) { case 's': case 'str': if(opts.bookSST) { - vv = get_sst_id(opts.Strings, (cell.v/*:any*/), opts.revStrings); + vv = get_sst_id(opts.Strings, (cell.v == null ? "" : String(cell.v)/*:any*/), opts.revStrings); o.t = "s"; o.v = vv; if(last_seen) write_record(ba, 0x0012 /* BrtShortIsst */, write_BrtShortIsst(cell, o)); else write_record(ba, 0x0007 /* BrtCellIsst */, write_BrtCellIsst(cell, o)); @@ -21264,7 +21271,7 @@ function write_ws_biff2_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n case 'b': case 'e': write_biff_rec(ba, 0x0005, write_BIFF2BERR(R, C, cell.v, cell.t)); return; /* TODO: codepage, sst */ case 's': case 'str': - write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, (cell.v||"").slice(0,255))); + write_biff_rec(ba, 0x0004, write_BIFF2LABEL(R, C, cell.v == null ? "" : String(cell.v).slice(0,255))); return; } write_biff_rec(ba, 0x0001, write_BIFF2Cell(null, R, C)); @@ -21388,9 +21395,9 @@ function write_ws_biff8_cell(ba/*:BufArray*/, cell/*:Cell*/, R/*:number*/, C/*:n /* TODO: codepage, sst */ case 's': case 'str': if(opts.bookSST) { - var isst = get_sst_id(opts.Strings, cell.v, opts.revStrings); + var isst = get_sst_id(opts.Strings, cell.v == null ? "" : String(cell.v), opts.revStrings); write_biff_rec(ba, 0x00fd /* LabelSst */, write_LabelSst(R, C, isst, os, opts)); - } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v||"").slice(0,255), os, opts)); + } else write_biff_rec(ba, 0x0204 /* Label */, write_Label(R, C, (cell.v == null ? "" : String(cell.v)).slice(0,255), os, opts)); break; default: write_biff_rec(ba, 0x0201 /* Blank */, write_XLSCell(R, C, os)); @@ -23274,6 +23281,39 @@ function varint_to_i32(buf) { } return i32; } +function varint_to_u64(buf) { + var l = 0, lo = buf[l] & 127, hi = 0; + varint: + if (buf[l++] >= 128) { + lo |= (buf[l] & 127) << 7; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 14; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 21; + if (buf[l++] < 128) + break varint; + lo |= (buf[l] & 127) << 28; + hi = buf[l] >> 4 & 7; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 3; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 10; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 17; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 24; + if (buf[l++] < 128) + break varint; + hi |= (buf[l] & 127) << 31; + } + return [lo >>> 0, hi >>> 0]; +} function parse_shallow(buf) { var out = [], ptr = [0]; while (ptr[0] < buf.length) { @@ -23810,10 +23850,12 @@ function write_new_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[1] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 8; l += 4; break; @@ -23840,10 +23882,12 @@ function write_old_storage(cell, sst) { l += 8; break; case "s": - if (sst.indexOf(cell.v) == -1) - throw new Error("Value ".concat(cell.v, " missing from SST!")); + var s = cell.v == null ? "" : String(cell.v); + var isst = sst.indexOf(s); + if (isst == -1) + sst[isst = sst.length] = s; out[2] = 3; - dv.setUint32(l, sst.indexOf(cell.v), true); + dv.setUint32(l, isst, true); flags |= 16; l += 4; break; @@ -23877,6 +23921,22 @@ function write_TSP_Reference(idx) { [{ type: 0, data: write_varint49(idx) }] ]); } +function numbers_add_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + var orefidx = orefs.indexOf(ref); + if (orefidx == -1) { + orefs.push(ref); + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; + } +} +function numbers_del_oref(iwa, ref) { + var _a; + var orefs = ((_a = iwa.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(iwa.messages[0].meta[5][0].data) : []; + iwa.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs.filter(function(r) { + return r != ref; + })) }]; +} function parse_TST_TableDataList(M, root) { var pb = parse_shallow(root.data); var type = varint_to_i32(pb[1][0].data); @@ -23884,6 +23944,8 @@ function parse_TST_TableDataList(M, root) { var data = []; (entries || []).forEach(function(entry) { var le = parse_shallow(entry.data); + if (!le[1]) + return; var key = varint_to_i32(le[1][0].data) >>> 0; switch (type) { case 1: @@ -24093,7 +24155,7 @@ function parse_TN_DocumentArchive(M, root, opts) { return out; } function parse_numbers_iwa(cfb, opts) { - var _a, _b, _c, _d, _e, _f, _g, _h; + var _a, _b, _c, _d, _e, _f, _g; var M = {}, indices = []; cfb.FullPaths.forEach(function(p) { if (p.match(/\.iwpv2/)) @@ -24123,9 +24185,9 @@ function parse_numbers_iwa(cfb, opts) { }); if (!indices.length) throw new Error("File has no messages"); - if (((_d = (_c = (_b = (_a = M == null ? void 0 : M[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(M[1][0].meta[1][0].data) == 1e4) + if (((_c = (_b = (_a = M == null ? void 0 : M[1]) == null ? void 0 : _a[0].meta) == null ? void 0 : _b[1]) == null ? void 0 : _c[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1e4) throw new Error("Pages documents are not supported"); - var docroot = ((_h = (_g = (_f = (_e = M == null ? void 0 : M[1]) == null ? void 0 : _e[0]) == null ? void 0 : _f.meta) == null ? void 0 : _g[1]) == null ? void 0 : _h[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; + var docroot = ((_g = (_f = (_e = (_d = M == null ? void 0 : M[1]) == null ? void 0 : _d[0]) == null ? void 0 : _e.meta) == null ? void 0 : _f[1]) == null ? void 0 : _g[0].data) && varint_to_i32(M[1][0].meta[1][0].data) == 1 && M[1][0]; if (!docroot) indices.forEach(function(idx) { M[idx].forEach(function(iwam) { @@ -24142,8 +24204,23 @@ function parse_numbers_iwa(cfb, opts) { throw new Error("Cannot find Document root"); return parse_TN_DocumentArchive(M, docroot, opts); } -function write_tile_row(tri, data, SST, wide) { +function write_TST_TileRowInfo(data, SST, wide) { var _a, _b; + var tri = [ + [], + [{ type: 0, data: write_varint49(0) }], + [{ type: 0, data: write_varint49(0) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(5) }], + [{ type: 2, data: new Uint8Array([]) }], + [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { + return 255; + })) }], + [{ type: 0, data: write_varint49(1) }] + ]; if (!((_a = tri[6]) == null ? void 0 : _a[0]) || !((_b = tri[7]) == null ? void 0 : _b[0])) throw "Mutation only works on post-BNC storages!"; var cnt = 0; @@ -24183,6 +24260,11 @@ function write_tile_row(tri, data, SST, wide) { _celload = write_old_storage({ t: "b", v: data[C] }, SST); break; default: + if (data[C] instanceof Date) { + celload = write_new_storage({ t: "s", v: data[C].toISOString() }, SST); + _celload = write_old_storage({ t: "s", v: data[C].toISOString() }, SST); + break; + } throw new Error("Unsupported value " + data[C]); } cell_storage.push(celload); @@ -24202,7 +24284,7 @@ function write_tile_row(tri, data, SST, wide) { tri[6][0].data = u8concat(cell_storage); tri[3][0].data = u8concat(_cell_storage); tri[8] = [{ type: 0, data: write_varint49(wide ? 1 : 0) }]; - return cnt; + return tri; } function write_iwam(type, payload) { return { @@ -24270,19 +24352,20 @@ function write_numbers_iwa(wb, opts) { if (!opts || !opts.numbers) throw new Error("Must pass a `numbers` option -- check the README"); var cfb = CFB.read(opts.numbers, { type: "base64" }); - var dependents = build_numbers_deps(cfb); - var cfb_DA = CFB.find(cfb, dependents[1].location); - if (!cfb_DA) - throw "Could not find ".concat(dependents[1].location, " in Numbers template"); - var iwa_DA = parse_iwa_file(decompress_iwa_file(cfb_DA.content)); - var docroot = iwa_DA.find(function(packet) { - return packet.id == 1; - }); + var deps = build_numbers_deps(cfb); + var docroot = numbers_iwa_find(cfb, deps, 1); if (docroot == null) throw "Could not find message ".concat(1, " in Numbers template"); var sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + if (sheetrefs.length > 1) + throw new Error("Template NUMBERS file must have exactly one sheet"); wb.SheetNames.forEach(function(name, idx) { - return write_numbers_ws(cfb, dependents, wb.Sheets[name], name, idx, sheetrefs[idx]); + if (idx >= 1) { + numbers_add_ws(cfb, deps, idx + 1); + docroot = numbers_iwa_find(cfb, deps, 1); + sheetrefs = mappa(parse_shallow(docroot.messages[0].data)[1], parse_TSP_Reference); + } + write_numbers_ws(cfb, deps, wb.Sheets[name], name, idx, sheetrefs[idx]); }); return cfb; } @@ -24308,9 +24391,364 @@ function numbers_iwa_find(cfb, deps, id) { }); return ainfo; } +function numbers_add_ws(cfb, deps, wsidx) { + var sheetref = -1, newsheetref = -1; + var remap = {}; + numbers_iwa_doit(cfb, deps, 1, function(docroot, arch) { + var doc = parse_shallow(docroot.messages[0].data); + sheetref = parse_TSP_Reference(parse_shallow(docroot.messages[0].data)[1][0].data); + newsheetref = get_unique_msgid({ deps: [1], location: deps[sheetref].location, type: 2 }, deps); + remap[sheetref] = newsheetref; + numbers_add_oref(docroot, newsheetref); + doc[1].push({ type: 2, data: write_TSP_Reference(newsheetref) }); + var sheet = numbers_iwa_find(cfb, deps, sheetref); + sheet.id = newsheetref; + if (deps[1].location == deps[newsheetref].location) + arch.push(sheet); + else + numbers_iwa_doit(cfb, deps, newsheetref, function(_, x) { + return x.push(sheet); + }); + docroot.messages[0].data = write_shallow(doc); + }); + var tiaref = -1; + numbers_iwa_doit(cfb, deps, newsheetref, function(sheetroot, arch) { + var sa = parse_shallow(sheetroot.messages[0].data); + for (var i = 3; i <= 69; ++i) + delete sa[i]; + var drawables = mappa(sa[2], parse_TSP_Reference); + drawables.forEach(function(n) { + return numbers_del_oref(sheetroot, n); + }); + tiaref = get_unique_msgid({ deps: [newsheetref], location: deps[drawables[0]].location, type: deps[drawables[0]].type }, deps); + numbers_add_oref(sheetroot, tiaref); + remap[drawables[0]] = tiaref; + sa[2] = [{ type: 2, data: write_TSP_Reference(tiaref) }]; + var tia = numbers_iwa_find(cfb, deps, drawables[0]); + tia.id = tiaref; + if (deps[drawables[0]].location == deps[newsheetref].location) + arch.push(tia); + else { + var loc2 = deps[newsheetref].location; + loc2 = loc2.replace(/^Root Entry\//, ""); + loc2 = loc2.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + var parentidx = mlist[3].findIndex(function(m) { + var _a, _b; + var mm = parse_shallow(m.data); + if ((_a = mm[3]) == null ? void 0 : _a[0]) + return u8str(mm[3][0].data) == loc2; + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc2) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(tiaref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + numbers_iwa_doit(cfb, deps, tiaref, function(_, x) { + return x.push(tia); + }); + } + sheetroot.messages[0].data = write_shallow(sa); + }); + var tmaref = -1; + numbers_iwa_doit(cfb, deps, tiaref, function(tiaroot, arch) { + var tia = parse_shallow(tiaroot.messages[0].data); + var da = parse_shallow(tia[1][0].data); + for (var i = 3; i <= 69; ++i) + delete da[i]; + var dap = parse_TSP_Reference(da[2][0].data); + da[2][0].data = write_TSP_Reference(remap[dap]); + tia[1][0].data = write_shallow(da); + var oldtmaref = parse_TSP_Reference(tia[2][0].data); + numbers_del_oref(tiaroot, oldtmaref); + tmaref = get_unique_msgid({ deps: [tiaref], location: deps[oldtmaref].location, type: deps[oldtmaref].type }, deps); + numbers_add_oref(tiaroot, tmaref); + remap[oldtmaref] = tmaref; + tia[2][0].data = write_TSP_Reference(tmaref); + var tma = numbers_iwa_find(cfb, deps, oldtmaref); + tma.id = tmaref; + if (deps[tiaref].location == deps[tmaref].location) + arch.push(tma); + else + numbers_iwa_doit(cfb, deps, tmaref, function(_, x) { + return x.push(tma); + }); + tiaroot.messages[0].data = write_shallow(tia); + }); + var loc = deps[tmaref].location; + loc = loc.replace(/^Root Entry\//, ""); + loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, tmaref, function(tmaroot, arch) { + var _a, _b; + var tma = parse_shallow(tmaroot.messages[0].data); + var uuid = u8str(tma[1][0].data), new_uuid = uuid.replace(/-[A-Z0-9]*/, "-".concat(wsidx.toString(16).padStart(4, "0"))); + tma[1][0].data = stru8(new_uuid); + [12, 13, 29, 31, 32, 33, 39, 44, 47, 81, 82, 84].forEach(function(n) { + return delete tma[n]; + }); + if (tma[45]) { + var srrta = parse_shallow(tma[45][0].data); + var ref = parse_TSP_Reference(srrta[1][0].data); + numbers_del_oref(tmaroot, ref); + delete tma[45]; + } + if (tma[70]) { + var hsoa = parse_shallow(tma[70][0].data); + (_a = hsoa[2]) == null ? void 0 : _a.forEach(function(item) { + var hsa = parse_shallow(item.data); + [2, 3].map(function(n) { + return hsa[n][0]; + }).forEach(function(hseadata) { + var hsea = parse_shallow(hseadata.data); + if (!hsea[8]) + return; + var ref2 = parse_TSP_Reference(hsea[8][0].data); + numbers_del_oref(tmaroot, ref2); + }); + }); + delete tma[70]; + } + [ + 46, + 30, + 34, + 35, + 36, + 38, + 48, + 49, + 60, + 61, + 62, + 63, + 64, + 71, + 72, + 73, + 74, + 75, + 85, + 86, + 87, + 88, + 89 + ].forEach(function(n) { + if (!tma[n]) + return; + var ref2 = parse_TSP_Reference(tma[n][0].data); + delete tma[n]; + numbers_del_oref(tmaroot, ref2); + }); + var store = parse_shallow(tma[4][0].data); + { + [2, 4, 5, 6, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22].forEach(function(n) { + var _a2; + if (!((_a2 = store[n]) == null ? void 0 : _a2[0])) + return; + var oldref = parse_TSP_Reference(store[n][0].data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) + arch.push(msg); + else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a3, _b2; + var mm = parse_shallow(m.data); + if ((_a3 = mm[3]) == null ? void 0 : _a3[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + store[n][0].data = write_TSP_Reference(newref); + }); + var row_headers = parse_shallow(store[1][0].data); + { + (_b = row_headers[2]) == null ? void 0 : _b.forEach(function(tspref) { + var oldref = parse_TSP_Reference(tspref.data); + var newref = get_unique_msgid({ deps: [tmaref], location: deps[oldref].location, type: deps[oldref].type }, deps); + numbers_del_oref(tmaroot, oldref); + numbers_add_oref(tmaroot, newref); + remap[oldref] = newref; + var msg = numbers_iwa_find(cfb, deps, oldref); + msg.id = newref; + if (deps[oldref].location == deps[tmaref].location) { + arch.push(msg); + } else { + deps[newref].location = deps[oldref].location.replace(oldref.toString(), newref.toString()); + if (deps[newref].location == deps[oldref].location) + deps[newref].location = deps[newref].location.replace(/\.iwa/, "-".concat(newref, ".iwa")); + CFB.utils.cfb_add(cfb, deps[newref].location, compress_iwa_file(write_iwa_file([msg]))); + var newloc = deps[newref].location; + newloc = newloc.replace(/^Root Entry\//, ""); + newloc = newloc.replace(/^Index\//, "").replace(/\.iwa$/, ""); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }], + [{ type: 2, data: stru8(newloc.replace(/-.*$/, "")) }], + [{ type: 2, data: stru8(newloc) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tspref.data = write_TSP_Reference(newref); + }); + } + store[1][0].data = write_shallow(row_headers); + var tiles = parse_shallow(store[3][0].data); + { + tiles[1].forEach(function(t) { + var tst = parse_shallow(t.data); + var oldtileref = parse_TSP_Reference(tst[2][0].data); + var newtileref = remap[oldtileref]; + if (!remap[oldtileref]) { + newtileref = get_unique_msgid({ deps: [tmaref], location: "", type: deps[oldtileref].type }, deps); + deps[newtileref].location = "Root Entry/Index/Tables/Tile-".concat(newtileref, ".iwa"); + remap[oldtileref] = newtileref; + var oldtile = numbers_iwa_find(cfb, deps, oldtileref); + oldtile.id = newtileref; + numbers_del_oref(tmaroot, oldtileref); + numbers_add_oref(tmaroot, newtileref); + CFB.utils.cfb_add(cfb, "/Index/Tables/Tile-".concat(newtileref, ".iwa"), compress_iwa_file(write_iwa_file([oldtile]))); + numbers_iwa_doit(cfb, deps, 2, function(ai) { + var mlist = parse_shallow(ai.messages[0].data); + mlist[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }], + [{ type: 2, data: stru8("Tables/Tile") }], + [{ type: 2, data: stru8("Tables/Tile-".concat(newtileref)) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [{ type: 2, data: new Uint8Array([2, 0, 0]) }], + [], + [], + [], + [], + [{ type: 0, data: write_varint49(0) }], + [], + [{ type: 0, data: write_varint49(0) }] + ]) }); + mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileref + 1, parse_varint49(mlist[1][0].data))) }]; + var parentidx = mlist[3].findIndex(function(m) { + var _a2, _b2; + var mm = parse_shallow(m.data); + if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + return u8str(mm[3][0].data) == loc; + if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + return true; + return false; + }); + var parent = parse_shallow(mlist[3][parentidx].data); + if (!parent[6]) + parent[6] = []; + parent[6].push({ + type: 2, + data: write_shallow([ + [], + [{ type: 0, data: write_varint49(newtileref) }] + ]) + }); + mlist[3][parentidx].data = write_shallow(parent); + ai.messages[0].data = write_shallow(mlist); + }); + } + tst[2][0].data = write_TSP_Reference(newtileref); + t.data = write_shallow(tst); + }); + } + store[3][0].data = write_shallow(tiles); + } + tma[4][0].data = write_shallow(store); + tmaroot.messages[0].data = write_shallow(tma); + }); +} function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { - if (sheetidx >= 1) - return console.error("The Numbers writer currently writes only the first table"); var drawables = []; numbers_iwa_doit(cfb, deps, rootref, function(docroot) { var sheetref = parse_shallow(docroot.messages[0].data); @@ -24328,7 +24766,6 @@ function write_numbers_ws(cfb, deps, ws, wsname, sheetidx, rootref) { } var USE_WIDE_ROWS = true; function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { - var _a, _b; var range = decode_range(ws["!ref"]); range.s.r = range.s.c = 0; var trunc = false; @@ -24343,15 +24780,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { if (trunc) console.error("Truncating to ".concat(encode_range(range))); var data = sheet_to_json(ws, { range: range, header: 1 }); - var SST = ["~Sh33tJ5~"], SST_set = new Set(SST); - data.forEach(function(row) { - return row.forEach(function(cell) { - if (typeof cell == "string" && !SST_set.has(cell)) { - SST.push(cell); - SST_set.add(cell); - } - }); - }); + var SST = ["~Sh33tJ5~"]; var loc = deps[tmaref].location; loc = loc.replace(/^Root Entry\//, ""); loc = loc.replace(/^Index\//, "").replace(/\.iwa$/, ""); @@ -24364,9 +24793,9 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { { var row_header_ref = parse_TSP_Reference(parse_shallow(store[1][0].data)[2][0].data); numbers_iwa_doit(cfb, deps, row_header_ref, function(rowhead, _x) { - var _a2; + var _a; var base_bucket = parse_shallow(rowhead.messages[0].data); - if ((_a2 = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a2[0]) + if ((_a = base_bucket == null ? void 0 : base_bucket[2]) == null ? void 0 : _a[0]) for (var R2 = 0; R2 < data.length; ++R2) { var _bucket = parse_shallow(base_bucket[2][0].data); _bucket[1][0].data = write_varint49(R2); @@ -24386,22 +24815,6 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { } colhead.messages[0].data = write_shallow(base_bucket); }); - var sstref = parse_TSP_Reference(store[4][0].data); - numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { - var sstdata = parse_shallow(sstroot.messages[0].data); - { - sstdata[3] = []; - SST.forEach(function(str, i) { - sstdata[3].push({ type: 2, data: write_shallow([ - [], - [{ type: 0, data: write_varint49(i) }], - [{ type: 0, data: write_varint49(1) }], - [{ type: 2, data: stru8(str) }] - ]) }); - }); - } - sstroot.messages[0].data = write_shallow(sstdata); - }); var rbtree = parse_shallow(store[9][0].data); rbtree[1] = []; var tilestore = parse_shallow(store[3][0].data); @@ -24409,25 +24822,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { var tstride = 256; tilestore[2] = [{ type: 0, data: write_varint49(tstride) }]; var tileref = parse_TSP_Reference(parse_shallow(tilestore[1][0].data)[2][0].data); - var save_token = 0; + var save_token = function() { + var metadata = numbers_iwa_find(cfb, deps, 2); + var mlist = parse_shallow(metadata.messages[0].data); + var mlst = mlist[3].filter(function(m) { + return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; + }); + return (mlst == null ? void 0 : mlst.length) ? parse_varint49(parse_shallow(mlst[0].data)[12][0].data) : 0; + }(); { CFB.utils.cfb_del(cfb, deps[tileref].location); numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); - var lst = mlist[3].filter(function(m) { - return parse_varint49(parse_shallow(m.data)[1][0].data) == tileref; - }); - if (lst && lst.length > 0) - save_token = parse_varint49(parse_shallow(lst[0].data)[12][0].data); mlist[3] = mlist[3].filter(function(m) { return parse_varint49(parse_shallow(m.data)[1][0].data) != tileref; }); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24440,6 +24855,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); + numbers_del_oref(tmaroot, tileref); } tilestore[1] = []; var ntiles = Math.ceil((range.e.r + 1) / tstride); @@ -24462,22 +24878,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { [{ type: 0, data: write_varint49(USE_WIDE_ROWS ? 1 : 0) }] ]; for (var R = tidx * tstride; R <= Math.min(range.e.r, (tidx + 1) * tstride - 1); ++R) { - var tilerow = [ - [], - [{ type: 0, data: write_varint49(0) }], - [{ type: 0, data: write_varint49(0) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(5) }], - [{ type: 2, data: new Uint8Array([]) }], - [{ type: 2, data: new Uint8Array(Array.from({ length: 510 }, function() { - return 255; - })) }], - [{ type: 0, data: write_varint49(1) }] - ]; - write_tile_row(tilerow, data[R], SST, USE_WIDE_ROWS); + var tilerow = write_TST_TileRowInfo(data[R], SST, USE_WIDE_ROWS); tilerow[1][0].data = write_varint49(R - tidx * tstride); tiledata[5].push({ data: write_shallow(tilerow), type: 2 }); } @@ -24511,11 +24912,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { ]) }); mlist[1] = [{ type: 0, data: write_varint49(Math.max(newtileid + 1, parse_varint49(mlist[1][0].data))) }]; var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24532,12 +24933,7 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - var orefs = ((_a = tmaroot.messages[0].meta[5]) == null ? void 0 : _a[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - var orefidx = orefs.indexOf(newtileid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = newtileid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, newtileid); rbtree[1].push({ type: 2, data: write_shallow([ [], [{ type: 0, data: write_varint49(tidx * tstride) }], @@ -24577,11 +24973,11 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { numbers_iwa_doit(cfb, deps, 2, function(ai) { var mlist = parse_shallow(ai.messages[0].data); var parentidx = mlist[3].findIndex(function(m) { - var _a2, _b2; + var _a, _b; var mm = parse_shallow(m.data); - if ((_a2 = mm[3]) == null ? void 0 : _a2[0]) + if ((_a = mm[3]) == null ? void 0 : _a[0]) return u8str(mm[3][0].data) == loc; - if (((_b2 = mm[2]) == null ? void 0 : _b2[0]) && u8str(mm[2][0].data) == loc) + if (((_b = mm[2]) == null ? void 0 : _b[0]) && u8str(mm[2][0].data) == loc) return true; return false; }); @@ -24598,14 +24994,27 @@ function write_numbers_tma(cfb, deps, ws, tmaroot, tmafile, tmaref) { mlist[3][parentidx].data = write_shallow(parent); ai.messages[0].data = write_shallow(mlist); }); - orefs = ((_b = tmaroot.messages[0].meta[5]) == null ? void 0 : _b[0]) ? parse_packed_varints(tmaroot.messages[0].meta[5][0].data) : []; - orefidx = orefs.indexOf(mergeid); - if (orefidx == -1) { - orefs[orefidx = orefs.length] = mergeid; - tmaroot.messages[0].meta[5] = [{ type: 2, data: write_packed_varints(orefs) }]; - } + numbers_add_oref(tmaroot, mergeid); } else delete store[13]; + var sstref = parse_TSP_Reference(store[4][0].data); + numbers_iwa_doit(cfb, deps, sstref, function(sstroot) { + var sstdata = parse_shallow(sstroot.messages[0].data); + { + sstdata[3] = []; + SST.forEach(function(str, i) { + if (i == 0) + return; + sstdata[3].push({ type: 2, data: write_shallow([ + [], + [{ type: 0, data: write_varint49(i) }], + [{ type: 0, data: write_varint49(1) }], + [{ type: 2, data: stru8(str) }] + ]) }); + }); + } + sstroot.messages[0].data = write_shallow(sstdata); + }); } pb[4][0].data = write_shallow(store); }