row.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 Enums = require('./enums');
  7. var colCache = require('../utils/col-cache');
  8. var Cell = require('./cell');
  9. var Row = /*#__PURE__*/function () {
  10. function Row(worksheet, number) {
  11. _classCallCheck(this, Row);
  12. this._worksheet = worksheet;
  13. this._number = number;
  14. this._cells = [];
  15. this.style = {};
  16. this.outlineLevel = 0;
  17. } // return the row number
  18. _createClass(Row, [{
  19. key: "commit",
  20. // Inform Streaming Writer that this row (and all rows before it) are complete
  21. // and ready to write. Has no effect on Worksheet document
  22. value: function commit() {
  23. this._worksheet._commitRow(this); // eslint-disable-line no-underscore-dangle
  24. } // helps GC by breaking cyclic references
  25. }, {
  26. key: "destroy",
  27. value: function destroy() {
  28. delete this._worksheet;
  29. delete this._cells;
  30. delete this.style;
  31. }
  32. }, {
  33. key: "findCell",
  34. value: function findCell(colNumber) {
  35. return this._cells[colNumber - 1];
  36. } // given {address, row, col}, find or create new cell
  37. }, {
  38. key: "getCellEx",
  39. value: function getCellEx(address) {
  40. var cell = this._cells[address.col - 1];
  41. if (!cell) {
  42. var column = this._worksheet.getColumn(address.col);
  43. cell = new Cell(this, column, address.address);
  44. this._cells[address.col - 1] = cell;
  45. }
  46. return cell;
  47. } // get cell by key, letter or column number
  48. }, {
  49. key: "getCell",
  50. value: function getCell(col) {
  51. if (typeof col === 'string') {
  52. // is it a key?
  53. var column = this._worksheet.getColumnKey(col);
  54. if (column) {
  55. col = column.number;
  56. } else {
  57. col = colCache.l2n(col);
  58. }
  59. }
  60. return this._cells[col - 1] || this.getCellEx({
  61. address: colCache.encodeAddress(this._number, col),
  62. row: this._number,
  63. col: col
  64. });
  65. } // remove cell(s) and shift all higher cells down by count
  66. }, {
  67. key: "splice",
  68. value: function splice(start, count) {
  69. var nKeep = start + count;
  70. for (var _len = arguments.length, inserts = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  71. inserts[_key - 2] = arguments[_key];
  72. }
  73. var nExpand = inserts.length - count;
  74. var nEnd = this._cells.length;
  75. var i;
  76. var cSrc;
  77. var cDst;
  78. if (nExpand < 0) {
  79. // remove cells
  80. for (i = start + inserts.length; i <= nEnd; i++) {
  81. cDst = this._cells[i - 1];
  82. cSrc = this._cells[i - nExpand - 1];
  83. if (cSrc) {
  84. cDst = this.getCell(i);
  85. cDst.value = cSrc.value;
  86. cDst.style = cSrc.style; // eslint-disable-next-line no-underscore-dangle
  87. cDst._comment = cSrc._comment;
  88. } else if (cDst) {
  89. cDst.value = null;
  90. cDst.style = {}; // eslint-disable-next-line no-underscore-dangle
  91. cDst._comment = undefined;
  92. }
  93. }
  94. } else if (nExpand > 0) {
  95. // insert new cells
  96. for (i = nEnd; i >= nKeep; i--) {
  97. cSrc = this._cells[i - 1];
  98. if (cSrc) {
  99. cDst = this.getCell(i + nExpand);
  100. cDst.value = cSrc.value;
  101. cDst.style = cSrc.style; // eslint-disable-next-line no-underscore-dangle
  102. cDst._comment = cSrc._comment;
  103. } else {
  104. this._cells[i + nExpand - 1] = undefined;
  105. }
  106. }
  107. } // now add the new values
  108. for (i = 0; i < inserts.length; i++) {
  109. cDst = this.getCell(start + i);
  110. cDst.value = inserts[i];
  111. cDst.style = {}; // eslint-disable-next-line no-underscore-dangle
  112. cDst._comment = undefined;
  113. }
  114. } // Iterate over all non-null cells in this row
  115. }, {
  116. key: "eachCell",
  117. value: function eachCell(options, iteratee) {
  118. if (!iteratee) {
  119. iteratee = options;
  120. options = null;
  121. }
  122. if (options && options.includeEmpty) {
  123. var n = this._cells.length;
  124. for (var i = 1; i <= n; i++) {
  125. iteratee(this.getCell(i), i);
  126. }
  127. } else {
  128. this._cells.forEach(function (cell, index) {
  129. if (cell && cell.type !== Enums.ValueType.Null) {
  130. iteratee(cell, index + 1);
  131. }
  132. });
  133. }
  134. } // ===========================================================================
  135. // Page Breaks
  136. }, {
  137. key: "addPageBreak",
  138. value: function addPageBreak(lft, rght) {
  139. var ws = this._worksheet;
  140. var left = Math.max(0, lft - 1) || 0;
  141. var right = Math.max(0, rght - 1) || 16838;
  142. var pb = {
  143. id: this._number,
  144. max: right,
  145. man: 1
  146. };
  147. if (left) pb.min = left;
  148. ws.rowBreaks.push(pb);
  149. } // return a sparse array of cell values
  150. }, {
  151. key: "_applyStyle",
  152. // =========================================================================
  153. // styles
  154. value: function _applyStyle(name, value) {
  155. this.style[name] = value;
  156. this._cells.forEach(function (cell) {
  157. if (cell) {
  158. cell[name] = value;
  159. }
  160. });
  161. return value;
  162. }
  163. }, {
  164. key: "number",
  165. get: function get() {
  166. return this._number;
  167. }
  168. }, {
  169. key: "worksheet",
  170. get: function get() {
  171. return this._worksheet;
  172. }
  173. }, {
  174. key: "values",
  175. get: function get() {
  176. var values = [];
  177. this._cells.forEach(function (cell) {
  178. if (cell && cell.type !== Enums.ValueType.Null) {
  179. values[cell.col] = cell.value;
  180. }
  181. });
  182. return values;
  183. } // set the values by contiguous or sparse array, or by key'd object literal
  184. ,
  185. set: function set(value) {
  186. var _this = this;
  187. // this operation is not additive - any prior cells are removed
  188. this._cells = [];
  189. if (!value) {// empty row
  190. } else if (value instanceof Array) {
  191. var offset = 0;
  192. if (value.hasOwnProperty('0')) {
  193. // contiguous array - start at column 1
  194. offset = 1;
  195. }
  196. value.forEach(function (item, index) {
  197. if (item !== undefined) {
  198. _this.getCellEx({
  199. address: colCache.encodeAddress(_this._number, index + offset),
  200. row: _this._number,
  201. col: index + offset
  202. }).value = item;
  203. }
  204. });
  205. } else {
  206. // assume object with column keys
  207. this._worksheet.eachColumnKey(function (column, key) {
  208. if (value[key] !== undefined) {
  209. _this.getCellEx({
  210. address: colCache.encodeAddress(_this._number, column.number),
  211. row: _this._number,
  212. col: column.number
  213. }).value = value[key];
  214. }
  215. });
  216. }
  217. } // returns true if the row includes at least one cell with a value
  218. }, {
  219. key: "hasValues",
  220. get: function get() {
  221. return _.some(this._cells, function (cell) {
  222. return cell && cell.type !== Enums.ValueType.Null;
  223. });
  224. }
  225. }, {
  226. key: "cellCount",
  227. get: function get() {
  228. return this._cells.length;
  229. }
  230. }, {
  231. key: "actualCellCount",
  232. get: function get() {
  233. var count = 0;
  234. this.eachCell(function () {
  235. count++;
  236. });
  237. return count;
  238. } // get the min and max column number for the non-null cells in this row or null
  239. }, {
  240. key: "dimensions",
  241. get: function get() {
  242. var min = 0;
  243. var max = 0;
  244. this._cells.forEach(function (cell) {
  245. if (cell && cell.type !== Enums.ValueType.Null) {
  246. if (!min || min > cell.col) {
  247. min = cell.col;
  248. }
  249. if (max < cell.col) {
  250. max = cell.col;
  251. }
  252. }
  253. });
  254. return min > 0 ? {
  255. min: min,
  256. max: max
  257. } : null;
  258. }
  259. }, {
  260. key: "numFmt",
  261. get: function get() {
  262. return this.style.numFmt;
  263. },
  264. set: function set(value) {
  265. this._applyStyle('numFmt', value);
  266. }
  267. }, {
  268. key: "font",
  269. get: function get() {
  270. return this.style.font;
  271. },
  272. set: function set(value) {
  273. this._applyStyle('font', value);
  274. }
  275. }, {
  276. key: "alignment",
  277. get: function get() {
  278. return this.style.alignment;
  279. },
  280. set: function set(value) {
  281. this._applyStyle('alignment', value);
  282. }
  283. }, {
  284. key: "protection",
  285. get: function get() {
  286. return this.style.protection;
  287. },
  288. set: function set(value) {
  289. this._applyStyle('protection', value);
  290. }
  291. }, {
  292. key: "border",
  293. get: function get() {
  294. return this.style.border;
  295. },
  296. set: function set(value) {
  297. this._applyStyle('border', value);
  298. }
  299. }, {
  300. key: "fill",
  301. get: function get() {
  302. return this.style.fill;
  303. },
  304. set: function set(value) {
  305. this._applyStyle('fill', value);
  306. }
  307. }, {
  308. key: "hidden",
  309. get: function get() {
  310. return !!this._hidden;
  311. },
  312. set: function set(value) {
  313. this._hidden = value;
  314. }
  315. }, {
  316. key: "outlineLevel",
  317. get: function get() {
  318. return this._outlineLevel || 0;
  319. },
  320. set: function set(value) {
  321. this._outlineLevel = value;
  322. }
  323. }, {
  324. key: "collapsed",
  325. get: function get() {
  326. return !!(this._outlineLevel && this._outlineLevel >= this._worksheet.properties.outlineLevelRow);
  327. } // =========================================================================
  328. }, {
  329. key: "model",
  330. get: function get() {
  331. var cells = [];
  332. var min = 0;
  333. var max = 0;
  334. this._cells.forEach(function (cell) {
  335. if (cell) {
  336. var cellModel = cell.model;
  337. if (cellModel) {
  338. if (!min || min > cell.col) {
  339. min = cell.col;
  340. }
  341. if (max < cell.col) {
  342. max = cell.col;
  343. }
  344. cells.push(cellModel);
  345. }
  346. }
  347. });
  348. return this.height || cells.length ? {
  349. cells: cells,
  350. number: this.number,
  351. min: min,
  352. max: max,
  353. height: this.height,
  354. style: this.style,
  355. hidden: this.hidden,
  356. outlineLevel: this.outlineLevel,
  357. collapsed: this.collapsed
  358. } : null;
  359. },
  360. set: function set(value) {
  361. var _this2 = this;
  362. if (value.number !== this._number) {
  363. throw new Error('Invalid row number in model');
  364. }
  365. this._cells = [];
  366. var previousAddress;
  367. value.cells.forEach(function (cellModel) {
  368. switch (cellModel.type) {
  369. case Cell.Types.Merge:
  370. // special case - don't add this types
  371. break;
  372. default:
  373. {
  374. var address;
  375. if (cellModel.address) {
  376. address = colCache.decodeAddress(cellModel.address);
  377. } else if (previousAddress) {
  378. // This is a <c> element without an r attribute
  379. // Assume that it's the cell for the next column
  380. var _previousAddress = previousAddress,
  381. row = _previousAddress.row;
  382. var col = previousAddress.col + 1;
  383. address = {
  384. row: row,
  385. col: col,
  386. address: colCache.encodeAddress(row, col),
  387. $col$row: "$".concat(colCache.n2l(col), "$").concat(row)
  388. };
  389. }
  390. previousAddress = address;
  391. var cell = _this2.getCellEx(address);
  392. cell.model = cellModel;
  393. break;
  394. }
  395. }
  396. });
  397. if (value.height) {
  398. this.height = value.height;
  399. } else {
  400. delete this.height;
  401. }
  402. this.hidden = value.hidden;
  403. this.outlineLevel = value.outlineLevel || 0;
  404. this.style = value.style && JSON.parse(JSON.stringify(value.style)) || {};
  405. }
  406. }]);
  407. return Row;
  408. }();
  409. module.exports = Row;
  410. //# sourceMappingURL=row.js.map