range.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. var colCache = require('../utils/col-cache'); // used by worksheet to calculate sheet dimensions
  6. var Range = /*#__PURE__*/function () {
  7. function Range() {
  8. _classCallCheck(this, Range);
  9. this.decode(arguments);
  10. }
  11. _createClass(Range, [{
  12. key: "setTLBR",
  13. value: function setTLBR(t, l, b, r, s) {
  14. if (arguments.length < 4) {
  15. // setTLBR(tl, br, s)
  16. var tl = colCache.decodeAddress(t);
  17. var br = colCache.decodeAddress(l);
  18. this.model = {
  19. top: Math.min(tl.row, br.row),
  20. left: Math.min(tl.col, br.col),
  21. bottom: Math.max(tl.row, br.row),
  22. right: Math.max(tl.col, br.col),
  23. sheetName: b
  24. };
  25. this.setTLBR(tl.row, tl.col, br.row, br.col, s);
  26. } else {
  27. // setTLBR(t, l, b, r, s)
  28. this.model = {
  29. top: Math.min(t, b),
  30. left: Math.min(l, r),
  31. bottom: Math.max(t, b),
  32. right: Math.max(l, r),
  33. sheetName: s
  34. };
  35. }
  36. }
  37. }, {
  38. key: "decode",
  39. value: function decode(argv) {
  40. switch (argv.length) {
  41. case 5:
  42. // [t,l,b,r,s]
  43. this.setTLBR(argv[0], argv[1], argv[2], argv[3], argv[4]);
  44. break;
  45. case 4:
  46. // [t,l,b,r]
  47. this.setTLBR(argv[0], argv[1], argv[2], argv[3]);
  48. break;
  49. case 3:
  50. // [tl,br,s]
  51. this.setTLBR(argv[0], argv[1], argv[2]);
  52. break;
  53. case 2:
  54. // [tl,br]
  55. this.setTLBR(argv[0], argv[1]);
  56. break;
  57. case 1:
  58. {
  59. var value = argv[0];
  60. if (value instanceof Range) {
  61. // copy constructor
  62. this.model = {
  63. top: value.model.top,
  64. left: value.model.left,
  65. bottom: value.model.bottom,
  66. right: value.model.right,
  67. sheetName: value.sheetName
  68. };
  69. } else if (value instanceof Array) {
  70. // an arguments array
  71. this.decode(value);
  72. } else if (value.top && value.left && value.bottom && value.right) {
  73. // a model
  74. this.model = {
  75. top: value.top,
  76. left: value.left,
  77. bottom: value.bottom,
  78. right: value.right,
  79. sheetName: value.sheetName
  80. };
  81. } else {
  82. // [sheetName!]tl:br
  83. var tlbr = colCache.decodeEx(value);
  84. if (tlbr.top) {
  85. this.model = {
  86. top: tlbr.top,
  87. left: tlbr.left,
  88. bottom: tlbr.bottom,
  89. right: tlbr.right,
  90. sheetName: tlbr.sheetName
  91. };
  92. } else {
  93. this.model = {
  94. top: tlbr.row,
  95. left: tlbr.col,
  96. bottom: tlbr.row,
  97. right: tlbr.col,
  98. sheetName: tlbr.sheetName
  99. };
  100. }
  101. }
  102. break;
  103. }
  104. case 0:
  105. this.model = {
  106. top: 0,
  107. left: 0,
  108. bottom: 0,
  109. right: 0
  110. };
  111. break;
  112. default:
  113. throw new Error("Invalid number of arguments to _getDimensions() - ".concat(argv.length));
  114. }
  115. }
  116. }, {
  117. key: "expand",
  118. value: function expand(top, left, bottom, right) {
  119. if (!this.model.top || top < this.top) this.top = top;
  120. if (!this.model.left || left < this.left) this.left = left;
  121. if (!this.model.bottom || bottom > this.bottom) this.bottom = bottom;
  122. if (!this.model.right || right > this.right) this.right = right;
  123. }
  124. }, {
  125. key: "expandRow",
  126. value: function expandRow(row) {
  127. if (row) {
  128. var dimensions = row.dimensions,
  129. number = row.number;
  130. if (dimensions) {
  131. this.expand(number, dimensions.min, number, dimensions.max);
  132. }
  133. }
  134. }
  135. }, {
  136. key: "expandToAddress",
  137. value: function expandToAddress(addressStr) {
  138. var address = colCache.decodeEx(addressStr);
  139. this.expand(address.row, address.col, address.row, address.col);
  140. }
  141. }, {
  142. key: "toString",
  143. value: function toString() {
  144. return this.range;
  145. }
  146. }, {
  147. key: "intersects",
  148. value: function intersects(other) {
  149. if (other.sheetName && this.sheetName && other.sheetName !== this.sheetName) return false;
  150. if (other.bottom < this.top) return false;
  151. if (other.top > this.bottom) return false;
  152. if (other.right < this.left) return false;
  153. if (other.left > this.right) return false;
  154. return true;
  155. }
  156. }, {
  157. key: "contains",
  158. value: function contains(addressStr) {
  159. var address = colCache.decodeEx(addressStr);
  160. return this.containsEx(address);
  161. }
  162. }, {
  163. key: "containsEx",
  164. value: function containsEx(address) {
  165. if (address.sheetName && this.sheetName && address.sheetName !== this.sheetName) return false;
  166. return address.row >= this.top && address.row <= this.bottom && address.col >= this.left && address.col <= this.right;
  167. }
  168. }, {
  169. key: "forEachAddress",
  170. value: function forEachAddress(cb) {
  171. for (var col = this.left; col <= this.right; col++) {
  172. for (var row = this.top; row <= this.bottom; row++) {
  173. cb(colCache.encodeAddress(row, col), row, col);
  174. }
  175. }
  176. }
  177. }, {
  178. key: "top",
  179. get: function get() {
  180. return this.model.top || 1;
  181. },
  182. set: function set(value) {
  183. this.model.top = value;
  184. }
  185. }, {
  186. key: "left",
  187. get: function get() {
  188. return this.model.left || 1;
  189. },
  190. set: function set(value) {
  191. this.model.left = value;
  192. }
  193. }, {
  194. key: "bottom",
  195. get: function get() {
  196. return this.model.bottom || 1;
  197. },
  198. set: function set(value) {
  199. this.model.bottom = value;
  200. }
  201. }, {
  202. key: "right",
  203. get: function get() {
  204. return this.model.right || 1;
  205. },
  206. set: function set(value) {
  207. this.model.right = value;
  208. }
  209. }, {
  210. key: "sheetName",
  211. get: function get() {
  212. return this.model.sheetName;
  213. },
  214. set: function set(value) {
  215. this.model.sheetName = value;
  216. }
  217. }, {
  218. key: "_serialisedSheetName",
  219. get: function get() {
  220. var sheetName = this.model.sheetName;
  221. if (sheetName) {
  222. if (/^[a-zA-Z0-9]*$/.test(sheetName)) {
  223. return "".concat(sheetName, "!");
  224. }
  225. return "'".concat(sheetName, "'!");
  226. }
  227. return '';
  228. }
  229. }, {
  230. key: "tl",
  231. get: function get() {
  232. return colCache.n2l(this.left) + this.top;
  233. }
  234. }, {
  235. key: "$t$l",
  236. get: function get() {
  237. return "$".concat(colCache.n2l(this.left), "$").concat(this.top);
  238. }
  239. }, {
  240. key: "br",
  241. get: function get() {
  242. return colCache.n2l(this.right) + this.bottom;
  243. }
  244. }, {
  245. key: "$b$r",
  246. get: function get() {
  247. return "$".concat(colCache.n2l(this.right), "$").concat(this.bottom);
  248. }
  249. }, {
  250. key: "range",
  251. get: function get() {
  252. return "".concat(this._serialisedSheetName + this.tl, ":").concat(this.br);
  253. }
  254. }, {
  255. key: "$range",
  256. get: function get() {
  257. return "".concat(this._serialisedSheetName + this.$t$l, ":").concat(this.$b$r);
  258. }
  259. }, {
  260. key: "shortRange",
  261. get: function get() {
  262. return this.count > 1 ? this.range : this._serialisedSheetName + this.tl;
  263. }
  264. }, {
  265. key: "$shortRange",
  266. get: function get() {
  267. return this.count > 1 ? this.$range : this._serialisedSheetName + this.$t$l;
  268. }
  269. }, {
  270. key: "count",
  271. get: function get() {
  272. return (1 + this.bottom - this.top) * (1 + this.right - this.left);
  273. }
  274. }]);
  275. return Range;
  276. }();
  277. module.exports = Range;
  278. //# sourceMappingURL=range.js.map