range.js 6.0 KB

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