defined-names.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 _ = require('../utils/under-dash');
  6. var colCache = require('../utils/col-cache');
  7. var CellMatrix = require('../utils/cell-matrix');
  8. var Range = require('./range');
  9. var rangeRegexp = /[$](\w+)[$](\d+)(:[$](\w+)[$](\d+))?/;
  10. var DefinedNames = /*#__PURE__*/function () {
  11. function DefinedNames() {
  12. _classCallCheck(this, DefinedNames);
  13. this.matrixMap = {};
  14. }
  15. _createClass(DefinedNames, [{
  16. key: "getMatrix",
  17. value: function getMatrix(name) {
  18. var matrix = this.matrixMap[name] || (this.matrixMap[name] = new CellMatrix());
  19. return matrix;
  20. } // add a name to a cell. locStr in the form SheetName!$col$row or SheetName!$c1$r1:$c2:$r2
  21. }, {
  22. key: "add",
  23. value: function add(locStr, name) {
  24. var location = colCache.decodeEx(locStr);
  25. this.addEx(location, name);
  26. }
  27. }, {
  28. key: "addEx",
  29. value: function addEx(location, name) {
  30. var matrix = this.getMatrix(name);
  31. if (location.top) {
  32. for (var col = location.left; col <= location.right; col++) {
  33. for (var row = location.top; row <= location.bottom; row++) {
  34. var address = {
  35. sheetName: location.sheetName,
  36. address: colCache.n2l(col) + row,
  37. row: row,
  38. col: col
  39. };
  40. matrix.addCellEx(address);
  41. }
  42. }
  43. } else {
  44. matrix.addCellEx(location);
  45. }
  46. }
  47. }, {
  48. key: "remove",
  49. value: function remove(locStr, name) {
  50. var location = colCache.decodeEx(locStr);
  51. this.removeEx(location, name);
  52. }
  53. }, {
  54. key: "removeEx",
  55. value: function removeEx(location, name) {
  56. var matrix = this.getMatrix(name);
  57. matrix.removeCellEx(location);
  58. }
  59. }, {
  60. key: "removeAllNames",
  61. value: function removeAllNames(location) {
  62. _.each(this.matrixMap, function (matrix) {
  63. matrix.removeCellEx(location);
  64. });
  65. }
  66. }, {
  67. key: "forEach",
  68. value: function forEach(callback) {
  69. _.each(this.matrixMap, function (matrix, name) {
  70. matrix.forEach(function (cell) {
  71. callback(name, cell);
  72. });
  73. });
  74. } // get all the names of a cell
  75. }, {
  76. key: "getNames",
  77. value: function getNames(addressStr) {
  78. return this.getNamesEx(colCache.decodeEx(addressStr));
  79. }
  80. }, {
  81. key: "getNamesEx",
  82. value: function getNamesEx(address) {
  83. return _.map(this.matrixMap, function (matrix, name) {
  84. return matrix.findCellEx(address) && name;
  85. }).filter(Boolean);
  86. }
  87. }, {
  88. key: "_explore",
  89. value: function _explore(matrix, cell) {
  90. cell.mark = false;
  91. var sheetName = cell.sheetName;
  92. var range = new Range(cell.row, cell.col, cell.row, cell.col, sheetName);
  93. var x;
  94. var y; // grow vertical - only one col to worry about
  95. function vGrow(yy, edge) {
  96. var c = matrix.findCellAt(sheetName, yy, cell.col);
  97. if (!c || !c.mark) {
  98. return false;
  99. }
  100. range[edge] = yy;
  101. c.mark = false;
  102. return true;
  103. }
  104. for (y = cell.row - 1; vGrow(y, 'top'); y--) {
  105. ;
  106. }
  107. for (y = cell.row + 1; vGrow(y, 'bottom'); y++) {
  108. ;
  109. } // grow horizontal - ensure all rows can grow
  110. function hGrow(xx, edge) {
  111. var cells = [];
  112. for (y = range.top; y <= range.bottom; y++) {
  113. var c = matrix.findCellAt(sheetName, y, xx);
  114. if (c && c.mark) {
  115. cells.push(c);
  116. } else {
  117. return false;
  118. }
  119. }
  120. range[edge] = xx;
  121. for (var i = 0; i < cells.length; i++) {
  122. cells[i].mark = false;
  123. }
  124. return true;
  125. }
  126. for (x = cell.col - 1; hGrow(x, 'left'); x--) {
  127. ;
  128. }
  129. for (x = cell.col + 1; hGrow(x, 'right'); x++) {
  130. ;
  131. }
  132. return range;
  133. }
  134. }, {
  135. key: "getRanges",
  136. value: function getRanges(name, matrix) {
  137. var _this = this;
  138. matrix = matrix || this.matrixMap[name];
  139. if (!matrix) {
  140. return {
  141. name: name,
  142. ranges: []
  143. };
  144. } // mark and sweep!
  145. matrix.forEach(function (cell) {
  146. cell.mark = true;
  147. });
  148. var ranges = matrix.map(function (cell) {
  149. return cell.mark && _this._explore(matrix, cell);
  150. }).filter(Boolean).map(function (range) {
  151. return range.$shortRange;
  152. });
  153. return {
  154. name: name,
  155. ranges: ranges
  156. };
  157. }
  158. }, {
  159. key: "normaliseMatrix",
  160. value: function normaliseMatrix(matrix, sheetName) {
  161. // some of the cells might have shifted on specified sheet
  162. // need to reassign rows, cols
  163. matrix.forEachInSheet(sheetName, function (cell, row, col) {
  164. if (cell) {
  165. if (cell.row !== row || cell.col !== col) {
  166. cell.row = row;
  167. cell.col = col;
  168. cell.address = colCache.n2l(col) + row;
  169. }
  170. }
  171. });
  172. }
  173. }, {
  174. key: "spliceRows",
  175. value: function spliceRows(sheetName, start, numDelete, numInsert) {
  176. var _this2 = this;
  177. _.each(this.matrixMap, function (matrix) {
  178. matrix.spliceRows(sheetName, start, numDelete, numInsert);
  179. _this2.normaliseMatrix(matrix, sheetName);
  180. });
  181. }
  182. }, {
  183. key: "spliceColumns",
  184. value: function spliceColumns(sheetName, start, numDelete, numInsert) {
  185. var _this3 = this;
  186. _.each(this.matrixMap, function (matrix) {
  187. matrix.spliceColumns(sheetName, start, numDelete, numInsert);
  188. _this3.normaliseMatrix(matrix, sheetName);
  189. });
  190. }
  191. }, {
  192. key: "model",
  193. get: function get() {
  194. var _this4 = this;
  195. // To get names per cell - just iterate over all names finding cells if they exist
  196. return _.map(this.matrixMap, function (matrix, name) {
  197. return _this4.getRanges(name, matrix);
  198. }).filter(function (definedName) {
  199. return definedName.ranges.length;
  200. });
  201. },
  202. set: function set(value) {
  203. // value is [ { name, ranges }, ... ]
  204. var matrixMap = this.matrixMap = {};
  205. value.forEach(function (definedName) {
  206. var matrix = matrixMap[definedName.name] = new CellMatrix();
  207. definedName.ranges.forEach(function (rangeStr) {
  208. if (rangeRegexp.test(rangeStr.split('!').pop() || '')) {
  209. matrix.addCell(rangeStr);
  210. }
  211. });
  212. });
  213. }
  214. }]);
  215. return DefinedNames;
  216. }();
  217. module.exports = DefinedNames;
  218. //# sourceMappingURL=defined-names.js.map