cell-matrix.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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('./under-dash');
  6. var colCache = require('./col-cache');
  7. var CellMatrix = /*#__PURE__*/function () {
  8. function CellMatrix(template) {
  9. _classCallCheck(this, CellMatrix);
  10. this.template = template;
  11. this.sheets = {};
  12. }
  13. _createClass(CellMatrix, [{
  14. key: "addCell",
  15. value: function addCell(addressStr) {
  16. this.addCellEx(colCache.decodeEx(addressStr));
  17. }
  18. }, {
  19. key: "getCell",
  20. value: function getCell(addressStr) {
  21. return this.findCellEx(colCache.decodeEx(addressStr), true);
  22. }
  23. }, {
  24. key: "findCell",
  25. value: function findCell(addressStr) {
  26. return this.findCellEx(colCache.decodeEx(addressStr), false);
  27. }
  28. }, {
  29. key: "findCellAt",
  30. value: function findCellAt(sheetName, rowNumber, colNumber) {
  31. var sheet = this.sheets[sheetName];
  32. var row = sheet && sheet[rowNumber];
  33. return row && row[colNumber];
  34. }
  35. }, {
  36. key: "addCellEx",
  37. value: function addCellEx(address) {
  38. if (address.top) {
  39. for (var row = address.top; row <= address.bottom; row++) {
  40. for (var col = address.left; col <= address.right; col++) {
  41. this.getCellAt(address.sheetName, row, col);
  42. }
  43. }
  44. } else {
  45. this.findCellEx(address, true);
  46. }
  47. }
  48. }, {
  49. key: "getCellEx",
  50. value: function getCellEx(address) {
  51. return this.findCellEx(address, true);
  52. }
  53. }, {
  54. key: "findCellEx",
  55. value: function findCellEx(address, create) {
  56. var sheet = this.findSheet(address, create);
  57. var row = this.findSheetRow(sheet, address, create);
  58. return this.findRowCell(row, address, create);
  59. }
  60. }, {
  61. key: "getCellAt",
  62. value: function getCellAt(sheetName, rowNumber, colNumber) {
  63. var sheet = this.sheets[sheetName] || (this.sheets[sheetName] = []);
  64. var row = sheet[rowNumber] || (sheet[rowNumber] = []);
  65. var cell = row[colNumber] || (row[colNumber] = {
  66. sheetName: sheetName,
  67. address: colCache.n2l(colNumber) + rowNumber,
  68. row: rowNumber,
  69. col: colNumber
  70. });
  71. return cell;
  72. }
  73. }, {
  74. key: "removeCellEx",
  75. value: function removeCellEx(address) {
  76. var sheet = this.findSheet(address);
  77. if (!sheet) {
  78. return;
  79. }
  80. var row = this.findSheetRow(sheet, address);
  81. if (!row) {
  82. return;
  83. }
  84. delete row[address.col];
  85. }
  86. }, {
  87. key: "forEachInSheet",
  88. value: function forEachInSheet(sheetName, callback) {
  89. var sheet = this.sheets[sheetName];
  90. if (sheet) {
  91. sheet.forEach(function (row, rowNumber) {
  92. if (row) {
  93. row.forEach(function (cell, colNumber) {
  94. if (cell) {
  95. callback(cell, rowNumber, colNumber);
  96. }
  97. });
  98. }
  99. });
  100. }
  101. }
  102. }, {
  103. key: "forEach",
  104. value: function forEach(callback) {
  105. var _this = this;
  106. _.each(this.sheets, function (sheet, sheetName) {
  107. _this.forEachInSheet(sheetName, callback);
  108. });
  109. }
  110. }, {
  111. key: "map",
  112. value: function map(callback) {
  113. var results = [];
  114. this.forEach(function (cell) {
  115. results.push(callback(cell));
  116. });
  117. return results;
  118. }
  119. }, {
  120. key: "findSheet",
  121. value: function findSheet(address, create) {
  122. var name = address.sheetName;
  123. if (this.sheets[name]) {
  124. return this.sheets[name];
  125. }
  126. if (create) {
  127. return this.sheets[name] = [];
  128. }
  129. return undefined;
  130. }
  131. }, {
  132. key: "findSheetRow",
  133. value: function findSheetRow(sheet, address, create) {
  134. var row = address.row;
  135. if (sheet && sheet[row]) {
  136. return sheet[row];
  137. }
  138. if (create) {
  139. return sheet[row] = [];
  140. }
  141. return undefined;
  142. }
  143. }, {
  144. key: "findRowCell",
  145. value: function findRowCell(row, address, create) {
  146. var col = address.col;
  147. if (row && row[col]) {
  148. return row[col];
  149. }
  150. if (create) {
  151. return row[col] = this.template ? Object.assign(address, JSON.parse(JSON.stringify(this.template))) : address;
  152. }
  153. return undefined;
  154. }
  155. }, {
  156. key: "spliceRows",
  157. value: function spliceRows(sheetName, start, numDelete, numInsert) {
  158. var sheet = this.sheets[sheetName];
  159. if (sheet) {
  160. var inserts = [];
  161. for (var i = 0; i < numInsert; i++) {
  162. inserts.push([]);
  163. }
  164. sheet.splice.apply(sheet, [start, numDelete].concat(inserts));
  165. }
  166. }
  167. }, {
  168. key: "spliceColumns",
  169. value: function spliceColumns(sheetName, start, numDelete, numInsert) {
  170. var sheet = this.sheets[sheetName];
  171. if (sheet) {
  172. var inserts = [];
  173. for (var i = 0; i < numInsert; i++) {
  174. inserts.push(null);
  175. }
  176. _.each(sheet, function (row) {
  177. row.splice.apply(row, [start, numDelete].concat(inserts));
  178. });
  179. }
  180. }
  181. }]);
  182. return CellMatrix;
  183. }();
  184. module.exports = CellMatrix;
  185. //# sourceMappingURL=cell-matrix.js.map