UNPKG

@protobi/exceljs

Version:

Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge

276 lines (271 loc) 10.9 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var addressRegex = /^[A-Z]+\d+$/; // ========================================================================= // Column Letter to Number conversion var colCache = { _dictionary: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], _l2nFill: 0, _l2n: {}, _n2l: [], _level: function _level(n) { if (n <= 26) { return 1; } if (n <= 26 * 26) { return 2; } return 3; }, _fill: function _fill(level) { var c; var v; var l1; var l2; var l3; var n = 1; if (level >= 4) { throw new Error('Out of bounds. Excel supports columns from 1 to 16384'); } if (this._l2nFill < 1 && level >= 1) { while (n <= 26) { c = this._dictionary[n - 1]; this._n2l[n] = c; this._l2n[c] = n; n++; } this._l2nFill = 1; } if (this._l2nFill < 2 && level >= 2) { n = 27; while (n <= 26 + 26 * 26) { v = n - (26 + 1); l1 = v % 26; l2 = Math.floor(v / 26); c = this._dictionary[l2] + this._dictionary[l1]; this._n2l[n] = c; this._l2n[c] = n; n++; } this._l2nFill = 2; } if (this._l2nFill < 3 && level >= 3) { n = 26 + 26 * 26 + 1; while (n <= 16384) { v = n - (26 * 26 + 26 + 1); l1 = v % 26; l2 = Math.floor(v / 26) % 26; l3 = Math.floor(v / (26 * 26)); c = this._dictionary[l3] + this._dictionary[l2] + this._dictionary[l1]; this._n2l[n] = c; this._l2n[c] = n; n++; } this._l2nFill = 3; } }, l2n: function l2n(l) { if (!this._l2n[l]) { this._fill(l.length); } if (!this._l2n[l]) { throw new Error("Out of bounds. Invalid column letter: ".concat(l)); } return this._l2n[l]; }, n2l: function n2l(n) { if (n < 1 || n > 16384) { throw new Error("".concat(n, " is out of bounds. Excel supports columns from 1 to 16384")); } if (!this._n2l[n]) { this._fill(this._level(n)); } return this._n2l[n]; }, // ========================================================================= // Address processing _hash: {}, // check if value looks like an address validateAddress: function validateAddress(value) { if (!addressRegex.test(value)) { throw new Error("Invalid Address: ".concat(value)); } return true; }, // convert address string into structure decodeAddress: function decodeAddress(value) { var addr = value.length < 5 && this._hash[value]; if (addr) { return addr; } var hasCol = false; var col = ''; var colNumber = 0; var hasRow = false; var row = ''; var rowNumber = 0; for (var i = 0, char; i < value.length; i++) { char = value.charCodeAt(i); // col should before row if (!hasRow && char >= 65 && char <= 90) { // 65 = 'A'.charCodeAt(0) // 90 = 'Z'.charCodeAt(0) hasCol = true; col += value[i]; // colNumber starts from 1 colNumber = colNumber * 26 + char - 64; } else if (char >= 48 && char <= 57) { // 48 = '0'.charCodeAt(0) // 57 = '9'.charCodeAt(0) hasRow = true; row += value[i]; // rowNumber starts from 0 rowNumber = rowNumber * 10 + char - 48; } else if (hasRow && hasCol && char !== 36) { // 36 = '$'.charCodeAt(0) break; } } if (!hasCol) { colNumber = undefined; } else if (colNumber > 16384) { throw new Error("Out of bounds. Invalid column letter: ".concat(col)); } if (!hasRow) { rowNumber = undefined; } // in case $row$col value = col + row; var address = { address: value, col: colNumber, row: rowNumber, $col$row: "$".concat(col, "$").concat(row) }; // mem fix - cache only the tl 100x100 square if (colNumber <= 100 && rowNumber <= 100) { this._hash[value] = address; this._hash[address.$col$row] = address; } return address; }, // convert r,c into structure (if only 1 arg, assume r is address string) getAddress: function getAddress(r, c) { if (c) { var address = this.n2l(c) + r; return this.decodeAddress(address); } return this.decodeAddress(r); }, // convert [address], [tl:br] into address structures decode: function decode(value) { var parts = value.split(':'); if (parts.length === 2) { var tl = this.decodeAddress(parts[0]); var br = this.decodeAddress(parts[1]); var result = { top: Math.min(tl.row, br.row), left: Math.min(tl.col, br.col), bottom: Math.max(tl.row, br.row), right: Math.max(tl.col, br.col) }; // reconstruct tl, br and dimensions result.tl = this.n2l(result.left) + result.top; result.br = this.n2l(result.right) + result.bottom; result.dimensions = "".concat(result.tl, ":").concat(result.br); return result; } return this.decodeAddress(value); }, // convert [sheetName!][$]col[$]row[[$]col[$]row] into address or range structures decodeEx: function decodeEx(value) { var groups = value.match(/(?:(?:(?:'((?:[^']|'')*)')|([^'^ !]*))!)?(.*)/); var sheetName = groups[1] || groups[2]; // Qouted and unqouted groups var reference = groups[3]; // Remaining address var parts = reference.split(':'); if (parts.length > 1) { var tl = this.decodeAddress(parts[0]); var br = this.decodeAddress(parts[1]); var top = Math.min(tl.row, br.row); var left = Math.min(tl.col, br.col); var bottom = Math.max(tl.row, br.row); var right = Math.max(tl.col, br.col); tl = this.n2l(left) + top; br = this.n2l(right) + bottom; return { top: top, left: left, bottom: bottom, right: right, sheetName: sheetName, tl: { address: tl, col: left, row: top, $col$row: "$".concat(this.n2l(left), "$").concat(top), sheetName: sheetName }, br: { address: br, col: right, row: bottom, $col$row: "$".concat(this.n2l(right), "$").concat(bottom), sheetName: sheetName }, dimensions: "".concat(tl, ":").concat(br) }; } if (reference.startsWith('#')) { return sheetName ? { sheetName: sheetName, error: reference } : { error: reference }; } var address = this.decodeAddress(reference); return sheetName ? _objectSpread({ sheetName: sheetName }, address) : address; }, // convert row,col into address string encodeAddress: function encodeAddress(row, col) { return colCache.n2l(col) + row; }, // convert row,col into string address or t,l,b,r into range encode: function encode() { switch (arguments.length) { case 2: return colCache.encodeAddress(arguments[0], arguments[1]); case 4: return "".concat(colCache.encodeAddress(arguments[0], arguments[1]), ":").concat(colCache.encodeAddress(arguments[2], arguments[3])); default: throw new Error('Can only encode with 2 or 4 arguments'); } }, // return true if address is contained within range inRange: function inRange(range, address) { var _range = _slicedToArray(range, 5), left = _range[0], top = _range[1], right = _range[3], bottom = _range[4]; var _address = _slicedToArray(address, 2), col = _address[0], row = _address[1]; return col >= left && col <= right && row >= top && row <= bottom; } }; module.exports = colCache; //# sourceMappingURL=col-cache.js.map