column.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 DEFAULT_COLUMN_WIDTH = 9; // Column defines the column properties for 1 column.
  9. // This includes header rows, widths, key, (style), etc.
  10. // Worksheet will condense the columns as appropriate during serialization
  11. var Column = /*#__PURE__*/function () {
  12. function Column(worksheet, number, defn) {
  13. _classCallCheck(this, Column);
  14. this._worksheet = worksheet;
  15. this._number = number;
  16. if (defn !== false) {
  17. // sometimes defn will follow
  18. this.defn = defn;
  19. }
  20. }
  21. _createClass(Column, [{
  22. key: "toString",
  23. value: function toString() {
  24. return JSON.stringify({
  25. key: this.key,
  26. width: this.width,
  27. headers: this.headers.length ? this.headers : undefined
  28. });
  29. }
  30. }, {
  31. key: "equivalentTo",
  32. value: function equivalentTo(other) {
  33. return this.width === other.width && this.hidden === other.hidden && this.outlineLevel === other.outlineLevel && _.isEqual(this.style, other.style);
  34. }
  35. }, {
  36. key: "eachCell",
  37. value: function eachCell(options, iteratee) {
  38. var colNumber = this.number;
  39. if (!iteratee) {
  40. iteratee = options;
  41. options = null;
  42. }
  43. this._worksheet.eachRow(options, function (row, rowNumber) {
  44. iteratee(row.getCell(colNumber), rowNumber);
  45. });
  46. }
  47. }, {
  48. key: "_applyStyle",
  49. // =========================================================================
  50. // styles
  51. value: function _applyStyle(name, value) {
  52. this.style[name] = value;
  53. this.eachCell(function (cell) {
  54. cell[name] = value;
  55. });
  56. return value;
  57. }
  58. }, {
  59. key: "number",
  60. get: function get() {
  61. return this._number;
  62. }
  63. }, {
  64. key: "worksheet",
  65. get: function get() {
  66. return this._worksheet;
  67. }
  68. }, {
  69. key: "letter",
  70. get: function get() {
  71. return colCache.n2l(this._number);
  72. }
  73. }, {
  74. key: "isCustomWidth",
  75. get: function get() {
  76. return this.width !== undefined && this.width !== DEFAULT_COLUMN_WIDTH;
  77. }
  78. }, {
  79. key: "defn",
  80. get: function get() {
  81. return {
  82. header: this._header,
  83. key: this.key,
  84. width: this.width,
  85. style: this.style,
  86. hidden: this.hidden,
  87. outlineLevel: this.outlineLevel
  88. };
  89. },
  90. set: function set(value) {
  91. if (value) {
  92. this.key = value.key;
  93. this.width = value.width !== undefined ? value.width : DEFAULT_COLUMN_WIDTH;
  94. this.outlineLevel = value.outlineLevel;
  95. if (value.style) {
  96. this.style = value.style;
  97. } else {
  98. this.style = {};
  99. } // headers must be set after style
  100. this.header = value.header;
  101. this._hidden = !!value.hidden;
  102. } else {
  103. delete this._header;
  104. delete this._key;
  105. delete this.width;
  106. this.style = {};
  107. this.outlineLevel = 0;
  108. }
  109. }
  110. }, {
  111. key: "headers",
  112. get: function get() {
  113. return this._header && this._header instanceof Array ? this._header : [this._header];
  114. }
  115. }, {
  116. key: "header",
  117. get: function get() {
  118. return this._header;
  119. },
  120. set: function set(value) {
  121. var _this = this;
  122. if (value !== undefined) {
  123. this._header = value;
  124. this.headers.forEach(function (text, index) {
  125. _this._worksheet.getCell(index + 1, _this.number).value = text;
  126. });
  127. } else {
  128. this._header = undefined;
  129. }
  130. }
  131. }, {
  132. key: "key",
  133. get: function get() {
  134. return this._key;
  135. },
  136. set: function set(value) {
  137. var column = this._key && this._worksheet.getColumnKey(this._key);
  138. if (column === this) {
  139. this._worksheet.deleteColumnKey(this._key);
  140. }
  141. this._key = value;
  142. if (value) {
  143. this._worksheet.setColumnKey(this._key, this);
  144. }
  145. }
  146. }, {
  147. key: "hidden",
  148. get: function get() {
  149. return !!this._hidden;
  150. },
  151. set: function set(value) {
  152. this._hidden = value;
  153. }
  154. }, {
  155. key: "outlineLevel",
  156. get: function get() {
  157. return this._outlineLevel || 0;
  158. },
  159. set: function set(value) {
  160. this._outlineLevel = value;
  161. }
  162. }, {
  163. key: "collapsed",
  164. get: function get() {
  165. return !!(this._outlineLevel && this._outlineLevel >= this._worksheet.properties.outlineLevelCol);
  166. }
  167. }, {
  168. key: "isDefault",
  169. get: function get() {
  170. if (this.isCustomWidth) {
  171. return false;
  172. }
  173. if (this.hidden) {
  174. return false;
  175. }
  176. if (this.outlineLevel) {
  177. return false;
  178. }
  179. var s = this.style;
  180. if (s && (s.font || s.numFmt || s.alignment || s.border || s.fill || s.protection)) {
  181. return false;
  182. }
  183. return true;
  184. }
  185. }, {
  186. key: "headerCount",
  187. get: function get() {
  188. return this.headers.length;
  189. }
  190. }, {
  191. key: "values",
  192. get: function get() {
  193. var v = [];
  194. this.eachCell(function (cell, rowNumber) {
  195. if (cell && cell.type !== Enums.ValueType.Null) {
  196. v[rowNumber] = cell.value;
  197. }
  198. });
  199. return v;
  200. },
  201. set: function set(v) {
  202. var _this2 = this;
  203. if (!v) {
  204. return;
  205. }
  206. var colNumber = this.number;
  207. var offset = 0;
  208. if (v.hasOwnProperty('0')) {
  209. // assume contiguous array, start at row 1
  210. offset = 1;
  211. }
  212. v.forEach(function (value, index) {
  213. _this2._worksheet.getCell(index + offset, colNumber).value = value;
  214. });
  215. }
  216. }, {
  217. key: "numFmt",
  218. get: function get() {
  219. return this.style.numFmt;
  220. },
  221. set: function set(value) {
  222. this._applyStyle('numFmt', value);
  223. }
  224. }, {
  225. key: "font",
  226. get: function get() {
  227. return this.style.font;
  228. },
  229. set: function set(value) {
  230. this._applyStyle('font', value);
  231. }
  232. }, {
  233. key: "alignment",
  234. get: function get() {
  235. return this.style.alignment;
  236. },
  237. set: function set(value) {
  238. this._applyStyle('alignment', value);
  239. }
  240. }, {
  241. key: "protection",
  242. get: function get() {
  243. return this.style.protection;
  244. },
  245. set: function set(value) {
  246. this._applyStyle('protection', value);
  247. }
  248. }, {
  249. key: "border",
  250. get: function get() {
  251. return this.style.border;
  252. },
  253. set: function set(value) {
  254. this._applyStyle('border', value);
  255. }
  256. }, {
  257. key: "fill",
  258. get: function get() {
  259. return this.style.fill;
  260. },
  261. set: function set(value) {
  262. this._applyStyle('fill', value);
  263. } // =============================================================================
  264. // static functions
  265. }], [{
  266. key: "toModel",
  267. value: function toModel(columns) {
  268. // Convert array of Column into compressed list cols
  269. var cols = [];
  270. var col = null;
  271. if (columns) {
  272. columns.forEach(function (column, index) {
  273. if (column.isDefault) {
  274. if (col) {
  275. col = null;
  276. }
  277. } else if (!col || !column.equivalentTo(col)) {
  278. col = {
  279. min: index + 1,
  280. max: index + 1,
  281. width: column.width !== undefined ? column.width : DEFAULT_COLUMN_WIDTH,
  282. style: column.style,
  283. isCustomWidth: column.isCustomWidth,
  284. hidden: column.hidden,
  285. outlineLevel: column.outlineLevel,
  286. collapsed: column.collapsed
  287. };
  288. cols.push(col);
  289. } else {
  290. col.max = index + 1;
  291. }
  292. });
  293. }
  294. return cols.length ? cols : undefined;
  295. }
  296. }, {
  297. key: "fromModel",
  298. value: function fromModel(worksheet, cols) {
  299. cols = cols || [];
  300. var columns = [];
  301. var count = 1;
  302. var index = 0;
  303. while (index < cols.length) {
  304. var col = cols[index++];
  305. while (count < col.min) {
  306. columns.push(new Column(worksheet, count++));
  307. }
  308. while (count <= col.max) {
  309. columns.push(new Column(worksheet, count++, col));
  310. }
  311. }
  312. return columns.length ? columns : null;
  313. }
  314. }]);
  315. return Column;
  316. }();
  317. module.exports = Column;
  318. //# sourceMappingURL=column.js.map