col-cache.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. "use strict";
  2. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  3. 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."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  6. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  9. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  10. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  11. var addressRegex = /^[A-Z]+\d+$/; // =========================================================================
  12. // Column Letter to Number conversion
  13. var colCache = {
  14. _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'],
  15. _l2nFill: 0,
  16. _l2n: {},
  17. _n2l: [],
  18. _level: function _level(n) {
  19. if (n <= 26) {
  20. return 1;
  21. }
  22. if (n <= 26 * 26) {
  23. return 2;
  24. }
  25. return 3;
  26. },
  27. _fill: function _fill(level) {
  28. var c;
  29. var v;
  30. var l1;
  31. var l2;
  32. var l3;
  33. var n = 1;
  34. if (level >= 4) {
  35. throw new Error('Out of bounds. Excel supports columns from 1 to 16384');
  36. }
  37. if (this._l2nFill < 1 && level >= 1) {
  38. while (n <= 26) {
  39. c = this._dictionary[n - 1];
  40. this._n2l[n] = c;
  41. this._l2n[c] = n;
  42. n++;
  43. }
  44. this._l2nFill = 1;
  45. }
  46. if (this._l2nFill < 2 && level >= 2) {
  47. n = 27;
  48. while (n <= 26 + 26 * 26) {
  49. v = n - (26 + 1);
  50. l1 = v % 26;
  51. l2 = Math.floor(v / 26);
  52. c = this._dictionary[l2] + this._dictionary[l1];
  53. this._n2l[n] = c;
  54. this._l2n[c] = n;
  55. n++;
  56. }
  57. this._l2nFill = 2;
  58. }
  59. if (this._l2nFill < 3 && level >= 3) {
  60. n = 26 + 26 * 26 + 1;
  61. while (n <= 16384) {
  62. v = n - (26 * 26 + 26 + 1);
  63. l1 = v % 26;
  64. l2 = Math.floor(v / 26) % 26;
  65. l3 = Math.floor(v / (26 * 26));
  66. c = this._dictionary[l3] + this._dictionary[l2] + this._dictionary[l1];
  67. this._n2l[n] = c;
  68. this._l2n[c] = n;
  69. n++;
  70. }
  71. this._l2nFill = 3;
  72. }
  73. },
  74. l2n: function l2n(l) {
  75. if (!this._l2n[l]) {
  76. this._fill(l.length);
  77. }
  78. if (!this._l2n[l]) {
  79. throw new Error("Out of bounds. Invalid column letter: ".concat(l));
  80. }
  81. return this._l2n[l];
  82. },
  83. n2l: function n2l(n) {
  84. if (n < 1 || n > 16384) {
  85. throw new Error("".concat(n, " is out of bounds. Excel supports columns from 1 to 16384"));
  86. }
  87. if (!this._n2l[n]) {
  88. this._fill(this._level(n));
  89. }
  90. return this._n2l[n];
  91. },
  92. // =========================================================================
  93. // Address processing
  94. _hash: {},
  95. // check if value looks like an address
  96. validateAddress: function validateAddress(value) {
  97. if (!addressRegex.test(value)) {
  98. throw new Error("Invalid Address: ".concat(value));
  99. }
  100. return true;
  101. },
  102. // convert address string into structure
  103. decodeAddress: function decodeAddress(value) {
  104. var addr = value.length < 5 && this._hash[value];
  105. if (addr) {
  106. return addr;
  107. }
  108. var hasCol = false;
  109. var col = '';
  110. var colNumber = 0;
  111. var hasRow = false;
  112. var row = '';
  113. var rowNumber = 0;
  114. for (var i = 0, char; i < value.length; i++) {
  115. char = value.charCodeAt(i); // col should before row
  116. if (!hasRow && char >= 65 && char <= 90) {
  117. // 65 = 'A'.charCodeAt(0)
  118. // 90 = 'Z'.charCodeAt(0)
  119. hasCol = true;
  120. col += value[i]; // colNumber starts from 1
  121. colNumber = colNumber * 26 + char - 64;
  122. } else if (char >= 48 && char <= 57) {
  123. // 48 = '0'.charCodeAt(0)
  124. // 57 = '9'.charCodeAt(0)
  125. hasRow = true;
  126. row += value[i]; // rowNumber starts from 0
  127. rowNumber = rowNumber * 10 + char - 48;
  128. } else if (hasRow && hasCol && char !== 36) {
  129. // 36 = '$'.charCodeAt(0)
  130. break;
  131. }
  132. }
  133. if (!hasCol) {
  134. colNumber = undefined;
  135. } else if (colNumber > 16384) {
  136. throw new Error("Out of bounds. Invalid column letter: ".concat(col));
  137. }
  138. if (!hasRow) {
  139. rowNumber = undefined;
  140. } // in case $row$col
  141. value = col + row;
  142. var address = {
  143. address: value,
  144. col: colNumber,
  145. row: rowNumber,
  146. $col$row: "$".concat(col, "$").concat(row)
  147. }; // mem fix - cache only the tl 100x100 square
  148. if (colNumber <= 100 && rowNumber <= 100) {
  149. this._hash[value] = address;
  150. this._hash[address.$col$row] = address;
  151. }
  152. return address;
  153. },
  154. // convert r,c into structure (if only 1 arg, assume r is address string)
  155. getAddress: function getAddress(r, c) {
  156. if (c) {
  157. var address = this.n2l(c) + r;
  158. return this.decodeAddress(address);
  159. }
  160. return this.decodeAddress(r);
  161. },
  162. // convert [address], [tl:br] into address structures
  163. decode: function decode(value) {
  164. var parts = value.split(':');
  165. if (parts.length === 2) {
  166. var tl = this.decodeAddress(parts[0]);
  167. var br = this.decodeAddress(parts[1]);
  168. var result = {
  169. top: Math.min(tl.row, br.row),
  170. left: Math.min(tl.col, br.col),
  171. bottom: Math.max(tl.row, br.row),
  172. right: Math.max(tl.col, br.col)
  173. }; // reconstruct tl, br and dimensions
  174. result.tl = this.n2l(result.left) + result.top;
  175. result.br = this.n2l(result.right) + result.bottom;
  176. result.dimensions = "".concat(result.tl, ":").concat(result.br);
  177. return result;
  178. }
  179. return this.decodeAddress(value);
  180. },
  181. // convert [sheetName!][$]col[$]row[[$]col[$]row] into address or range structures
  182. decodeEx: function decodeEx(value) {
  183. var groups = value.match(/(?:(?:(?:'((?:[^']|'')*)')|([^'^ !]*))!)?(.*)/);
  184. var sheetName = groups[1] || groups[2]; // Qouted and unqouted groups
  185. var reference = groups[3]; // Remaining address
  186. var parts = reference.split(':');
  187. if (parts.length > 1) {
  188. var tl = this.decodeAddress(parts[0]);
  189. var br = this.decodeAddress(parts[1]);
  190. var top = Math.min(tl.row, br.row);
  191. var left = Math.min(tl.col, br.col);
  192. var bottom = Math.max(tl.row, br.row);
  193. var right = Math.max(tl.col, br.col);
  194. tl = this.n2l(left) + top;
  195. br = this.n2l(right) + bottom;
  196. return {
  197. top: top,
  198. left: left,
  199. bottom: bottom,
  200. right: right,
  201. sheetName: sheetName,
  202. tl: {
  203. address: tl,
  204. col: left,
  205. row: top,
  206. $col$row: "$".concat(this.n2l(left), "$").concat(top),
  207. sheetName: sheetName
  208. },
  209. br: {
  210. address: br,
  211. col: right,
  212. row: bottom,
  213. $col$row: "$".concat(this.n2l(right), "$").concat(bottom),
  214. sheetName: sheetName
  215. },
  216. dimensions: "".concat(tl, ":").concat(br)
  217. };
  218. }
  219. if (reference.startsWith('#')) {
  220. return sheetName ? {
  221. sheetName: sheetName,
  222. error: reference
  223. } : {
  224. error: reference
  225. };
  226. }
  227. var address = this.decodeAddress(reference);
  228. return sheetName ? _objectSpread({
  229. sheetName: sheetName
  230. }, address) : address;
  231. },
  232. // convert row,col into address string
  233. encodeAddress: function encodeAddress(row, col) {
  234. return colCache.n2l(col) + row;
  235. },
  236. // convert row,col into string address or t,l,b,r into range
  237. encode: function encode() {
  238. switch (arguments.length) {
  239. case 2:
  240. return colCache.encodeAddress(arguments[0], arguments[1]);
  241. case 4:
  242. return "".concat(colCache.encodeAddress(arguments[0], arguments[1]), ":").concat(colCache.encodeAddress(arguments[2], arguments[3]));
  243. default:
  244. throw new Error('Can only encode with 2 or 4 arguments');
  245. }
  246. },
  247. // return true if address is contained within range
  248. inRange: function inRange(range, address) {
  249. var _range = _slicedToArray(range, 5),
  250. left = _range[0],
  251. top = _range[1],
  252. right = _range[3],
  253. bottom = _range[4];
  254. var _address = _slicedToArray(address, 2),
  255. col = _address[0],
  256. row = _address[1];
  257. return col >= left && col <= right && row >= top && row <= bottom;
  258. }
  259. };
  260. module.exports = colCache;
  261. //# sourceMappingURL=col-cache.js.map