row.js 9.7 KB

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