cell-matrix.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. const _ = require('./under-dash');
  3. const colCache = require('./col-cache');
  4. class CellMatrix {
  5. constructor(template) {
  6. this.template = template;
  7. this.sheets = {};
  8. }
  9. addCell(addressStr) {
  10. this.addCellEx(colCache.decodeEx(addressStr));
  11. }
  12. getCell(addressStr) {
  13. return this.findCellEx(colCache.decodeEx(addressStr), true);
  14. }
  15. findCell(addressStr) {
  16. return this.findCellEx(colCache.decodeEx(addressStr), false);
  17. }
  18. findCellAt(sheetName, rowNumber, colNumber) {
  19. const sheet = this.sheets[sheetName];
  20. const row = sheet && sheet[rowNumber];
  21. return row && row[colNumber];
  22. }
  23. addCellEx(address) {
  24. if (address.top) {
  25. for (let row = address.top; row <= address.bottom; row++) {
  26. for (let col = address.left; col <= address.right; col++) {
  27. this.getCellAt(address.sheetName, row, col);
  28. }
  29. }
  30. } else {
  31. this.findCellEx(address, true);
  32. }
  33. }
  34. getCellEx(address) {
  35. return this.findCellEx(address, true);
  36. }
  37. findCellEx(address, create) {
  38. const sheet = this.findSheet(address, create);
  39. const row = this.findSheetRow(sheet, address, create);
  40. return this.findRowCell(row, address, create);
  41. }
  42. getCellAt(sheetName, rowNumber, colNumber) {
  43. const sheet = this.sheets[sheetName] || (this.sheets[sheetName] = []);
  44. const row = sheet[rowNumber] || (sheet[rowNumber] = []);
  45. const cell = row[colNumber] || (row[colNumber] = {
  46. sheetName,
  47. address: colCache.n2l(colNumber) + rowNumber,
  48. row: rowNumber,
  49. col: colNumber
  50. });
  51. return cell;
  52. }
  53. removeCellEx(address) {
  54. const sheet = this.findSheet(address);
  55. if (!sheet) {
  56. return;
  57. }
  58. const row = this.findSheetRow(sheet, address);
  59. if (!row) {
  60. return;
  61. }
  62. delete row[address.col];
  63. }
  64. forEachInSheet(sheetName, callback) {
  65. const sheet = this.sheets[sheetName];
  66. if (sheet) {
  67. sheet.forEach((row, rowNumber) => {
  68. if (row) {
  69. row.forEach((cell, colNumber) => {
  70. if (cell) {
  71. callback(cell, rowNumber, colNumber);
  72. }
  73. });
  74. }
  75. });
  76. }
  77. }
  78. forEach(callback) {
  79. _.each(this.sheets, (sheet, sheetName) => {
  80. this.forEachInSheet(sheetName, callback);
  81. });
  82. }
  83. map(callback) {
  84. const results = [];
  85. this.forEach(cell => {
  86. results.push(callback(cell));
  87. });
  88. return results;
  89. }
  90. findSheet(address, create) {
  91. const name = address.sheetName;
  92. if (this.sheets[name]) {
  93. return this.sheets[name];
  94. }
  95. if (create) {
  96. return this.sheets[name] = [];
  97. }
  98. return undefined;
  99. }
  100. findSheetRow(sheet, address, create) {
  101. const {
  102. row
  103. } = address;
  104. if (sheet && sheet[row]) {
  105. return sheet[row];
  106. }
  107. if (create) {
  108. return sheet[row] = [];
  109. }
  110. return undefined;
  111. }
  112. findRowCell(row, address, create) {
  113. const {
  114. col
  115. } = address;
  116. if (row && row[col]) {
  117. return row[col];
  118. }
  119. if (create) {
  120. return row[col] = this.template ? Object.assign(address, JSON.parse(JSON.stringify(this.template))) : address;
  121. }
  122. return undefined;
  123. }
  124. spliceRows(sheetName, start, numDelete, numInsert) {
  125. const sheet = this.sheets[sheetName];
  126. if (sheet) {
  127. const inserts = [];
  128. for (let i = 0; i < numInsert; i++) {
  129. inserts.push([]);
  130. }
  131. sheet.splice(start, numDelete, ...inserts);
  132. }
  133. }
  134. spliceColumns(sheetName, start, numDelete, numInsert) {
  135. const sheet = this.sheets[sheetName];
  136. if (sheet) {
  137. const inserts = [];
  138. for (let i = 0; i < numInsert; i++) {
  139. inserts.push(null);
  140. }
  141. _.each(sheet, row => {
  142. row.splice(start, numDelete, ...inserts);
  143. });
  144. }
  145. }
  146. }
  147. module.exports = CellMatrix;
  148. //# sourceMappingURL=cell-matrix.js.map