range.js 6.1 KB

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