cell.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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. /* eslint-disable max-classes-per-file */
  6. var colCache = require('../utils/col-cache');
  7. var _ = require('../utils/under-dash');
  8. var Enums = require('./enums');
  9. var _require = require('../utils/shared-formula'),
  10. slideFormula = _require.slideFormula;
  11. var Note = require('./note'); // Cell requirements
  12. // Operate inside a worksheet
  13. // Store and retrieve a value with a range of types: text, number, date, hyperlink, reference, formula, etc.
  14. // Manage/use and manipulate cell format either as local to cell or inherited from column or row.
  15. var Cell = /*#__PURE__*/function () {
  16. function Cell(row, column, address) {
  17. _classCallCheck(this, Cell);
  18. if (!row || !column) {
  19. throw new Error('A Cell needs a Row');
  20. }
  21. this._row = row;
  22. this._column = column;
  23. colCache.validateAddress(address);
  24. this._address = address; // TODO: lazy evaluation of this._value
  25. this._value = Value.create(Cell.Types.Null, this);
  26. this.style = this._mergeStyle(row.style, column.style, {});
  27. this._mergeCount = 0;
  28. }
  29. _createClass(Cell, [{
  30. key: "destroy",
  31. // help GC by removing cyclic (and other) references
  32. value: function destroy() {
  33. delete this.style;
  34. delete this._value;
  35. delete this._row;
  36. delete this._column;
  37. delete this._address;
  38. } // =========================================================================
  39. // Styles stuff
  40. }, {
  41. key: "_mergeStyle",
  42. value: function _mergeStyle(rowStyle, colStyle, style) {
  43. var numFmt = rowStyle && rowStyle.numFmt || colStyle && colStyle.numFmt;
  44. if (numFmt) style.numFmt = numFmt;
  45. var font = rowStyle && rowStyle.font || colStyle && colStyle.font;
  46. if (font) style.font = font;
  47. var alignment = rowStyle && rowStyle.alignment || colStyle && colStyle.alignment;
  48. if (alignment) style.alignment = alignment;
  49. var border = rowStyle && rowStyle.border || colStyle && colStyle.border;
  50. if (border) style.border = border;
  51. var fill = rowStyle && rowStyle.fill || colStyle && colStyle.fill;
  52. if (fill) style.fill = fill;
  53. var protection = rowStyle && rowStyle.protection || colStyle && colStyle.protection;
  54. if (protection) style.protection = protection;
  55. return style;
  56. } // =========================================================================
  57. // return the address for this cell
  58. }, {
  59. key: "toCsvString",
  60. value: function toCsvString() {
  61. return this._value.toCsvString();
  62. } // =========================================================================
  63. // Merge stuff
  64. }, {
  65. key: "addMergeRef",
  66. value: function addMergeRef() {
  67. this._mergeCount++;
  68. }
  69. }, {
  70. key: "releaseMergeRef",
  71. value: function releaseMergeRef() {
  72. this._mergeCount--;
  73. }
  74. }, {
  75. key: "merge",
  76. value: function merge(master, ignoreStyle) {
  77. this._value.release();
  78. this._value = Value.create(Cell.Types.Merge, this, master);
  79. if (!ignoreStyle) {
  80. this.style = master.style;
  81. }
  82. }
  83. }, {
  84. key: "unmerge",
  85. value: function unmerge() {
  86. if (this.type === Cell.Types.Merge) {
  87. this._value.release();
  88. this._value = Value.create(Cell.Types.Null, this);
  89. this.style = this._mergeStyle(this._row.style, this._column.style, {});
  90. }
  91. }
  92. }, {
  93. key: "isMergedTo",
  94. value: function isMergedTo(master) {
  95. if (this._value.type !== Cell.Types.Merge) return false;
  96. return this._value.isMergedTo(master);
  97. }
  98. }, {
  99. key: "toString",
  100. value: function toString() {
  101. return this.text;
  102. }
  103. }, {
  104. key: "_upgradeToHyperlink",
  105. value: function _upgradeToHyperlink(hyperlink) {
  106. // if this cell is a string, turn it into a Hyperlink
  107. if (this.type === Cell.Types.String) {
  108. this._value = Value.create(Cell.Types.Hyperlink, this, {
  109. text: this._value.value,
  110. hyperlink: hyperlink
  111. });
  112. }
  113. } // =========================================================================
  114. // Formula stuff
  115. }, {
  116. key: "addName",
  117. value: function addName(name) {
  118. this.workbook.definedNames.addEx(this.fullAddress, name);
  119. }
  120. }, {
  121. key: "removeName",
  122. value: function removeName(name) {
  123. this.workbook.definedNames.removeEx(this.fullAddress, name);
  124. }
  125. }, {
  126. key: "removeAllNames",
  127. value: function removeAllNames() {
  128. this.workbook.definedNames.removeAllNames(this.fullAddress);
  129. } // =========================================================================
  130. // Data Validation stuff
  131. }, {
  132. key: "worksheet",
  133. get: function get() {
  134. return this._row.worksheet;
  135. }
  136. }, {
  137. key: "workbook",
  138. get: function get() {
  139. return this._row.worksheet.workbook;
  140. }
  141. }, {
  142. key: "numFmt",
  143. get: function get() {
  144. return this.style.numFmt;
  145. },
  146. set: function set(value) {
  147. this.style.numFmt = value;
  148. }
  149. }, {
  150. key: "font",
  151. get: function get() {
  152. return this.style.font;
  153. },
  154. set: function set(value) {
  155. this.style.font = value;
  156. }
  157. }, {
  158. key: "alignment",
  159. get: function get() {
  160. return this.style.alignment;
  161. },
  162. set: function set(value) {
  163. this.style.alignment = value;
  164. }
  165. }, {
  166. key: "border",
  167. get: function get() {
  168. return this.style.border;
  169. },
  170. set: function set(value) {
  171. this.style.border = value;
  172. }
  173. }, {
  174. key: "fill",
  175. get: function get() {
  176. return this.style.fill;
  177. },
  178. set: function set(value) {
  179. this.style.fill = value;
  180. }
  181. }, {
  182. key: "protection",
  183. get: function get() {
  184. return this.style.protection;
  185. },
  186. set: function set(value) {
  187. this.style.protection = value;
  188. }
  189. }, {
  190. key: "address",
  191. get: function get() {
  192. return this._address;
  193. }
  194. }, {
  195. key: "row",
  196. get: function get() {
  197. return this._row.number;
  198. }
  199. }, {
  200. key: "col",
  201. get: function get() {
  202. return this._column.number;
  203. }
  204. }, {
  205. key: "$col$row",
  206. get: function get() {
  207. return "$".concat(this._column.letter, "$").concat(this.row);
  208. } // =========================================================================
  209. // Value stuff
  210. }, {
  211. key: "type",
  212. get: function get() {
  213. return this._value.type;
  214. }
  215. }, {
  216. key: "effectiveType",
  217. get: function get() {
  218. return this._value.effectiveType;
  219. }
  220. }, {
  221. key: "isMerged",
  222. get: function get() {
  223. return this._mergeCount > 0 || this.type === Cell.Types.Merge;
  224. }
  225. }, {
  226. key: "master",
  227. get: function get() {
  228. if (this.type === Cell.Types.Merge) {
  229. return this._value.master;
  230. }
  231. return this; // an unmerged cell is its own master
  232. }
  233. }, {
  234. key: "isHyperlink",
  235. get: function get() {
  236. return this._value.type === Cell.Types.Hyperlink;
  237. }
  238. }, {
  239. key: "hyperlink",
  240. get: function get() {
  241. return this._value.hyperlink;
  242. } // return the value
  243. }, {
  244. key: "value",
  245. get: function get() {
  246. return this._value.value;
  247. } // set the value - can be number, string or raw
  248. ,
  249. set: function set(v) {
  250. // special case - merge cells set their master's value
  251. if (this.type === Cell.Types.Merge) {
  252. this._value.master.value = v;
  253. return;
  254. }
  255. this._value.release(); // assign value
  256. this._value = Value.create(Value.getType(v), this, v);
  257. }
  258. }, {
  259. key: "note",
  260. get: function get() {
  261. return this._comment && this._comment.note;
  262. },
  263. set: function set(note) {
  264. this._comment = new Note(note);
  265. }
  266. }, {
  267. key: "text",
  268. get: function get() {
  269. return this._value.toString();
  270. }
  271. }, {
  272. key: "html",
  273. get: function get() {
  274. return _.escapeHtml(this.text);
  275. }
  276. }, {
  277. key: "formula",
  278. get: function get() {
  279. return this._value.formula;
  280. }
  281. }, {
  282. key: "result",
  283. get: function get() {
  284. return this._value.result;
  285. }
  286. }, {
  287. key: "formulaType",
  288. get: function get() {
  289. return this._value.formulaType;
  290. } // =========================================================================
  291. // Name stuff
  292. }, {
  293. key: "fullAddress",
  294. get: function get() {
  295. var worksheet = this._row.worksheet;
  296. return {
  297. sheetName: worksheet.name,
  298. address: this.address,
  299. row: this.row,
  300. col: this.col
  301. };
  302. }
  303. }, {
  304. key: "name",
  305. get: function get() {
  306. return this.names[0];
  307. },
  308. set: function set(value) {
  309. this.names = [value];
  310. }
  311. }, {
  312. key: "names",
  313. get: function get() {
  314. return this.workbook.definedNames.getNamesEx(this.fullAddress);
  315. },
  316. set: function set(value) {
  317. var _this = this;
  318. var definedNames = this.workbook.definedNames;
  319. definedNames.removeAllNames(this.fullAddress);
  320. value.forEach(function (name) {
  321. definedNames.addEx(_this.fullAddress, name);
  322. });
  323. }
  324. }, {
  325. key: "_dataValidations",
  326. get: function get() {
  327. return this.worksheet.dataValidations;
  328. }
  329. }, {
  330. key: "dataValidation",
  331. get: function get() {
  332. return this._dataValidations.find(this.address);
  333. },
  334. set: function set(value) {
  335. this._dataValidations.add(this.address, value);
  336. } // =========================================================================
  337. // Model stuff
  338. }, {
  339. key: "model",
  340. get: function get() {
  341. var model = this._value.model;
  342. model.style = this.style;
  343. if (this._comment) {
  344. model.comment = this._comment.model;
  345. }
  346. return model;
  347. },
  348. set: function set(value) {
  349. this._value.release();
  350. this._value = Value.create(value.type, this);
  351. this._value.model = value;
  352. if (value.comment) {
  353. switch (value.comment.type) {
  354. case 'note':
  355. this._comment = Note.fromModel(value.comment);
  356. break;
  357. }
  358. }
  359. if (value.style) {
  360. this.style = value.style;
  361. } else {
  362. this.style = {};
  363. }
  364. }
  365. }]);
  366. return Cell;
  367. }();
  368. Cell.Types = Enums.ValueType; // =============================================================================
  369. // Internal Value Types
  370. var NullValue = /*#__PURE__*/function () {
  371. function NullValue(cell) {
  372. _classCallCheck(this, NullValue);
  373. this.model = {
  374. address: cell.address,
  375. type: Cell.Types.Null
  376. };
  377. }
  378. _createClass(NullValue, [{
  379. key: "toCsvString",
  380. value: function toCsvString() {
  381. return '';
  382. }
  383. }, {
  384. key: "release",
  385. value: function release() {}
  386. }, {
  387. key: "toString",
  388. value: function toString() {
  389. return '';
  390. }
  391. }, {
  392. key: "value",
  393. get: function get() {
  394. return null;
  395. },
  396. set: function set(value) {// nothing to do
  397. }
  398. }, {
  399. key: "type",
  400. get: function get() {
  401. return Cell.Types.Null;
  402. }
  403. }, {
  404. key: "effectiveType",
  405. get: function get() {
  406. return Cell.Types.Null;
  407. }
  408. }, {
  409. key: "address",
  410. get: function get() {
  411. return this.model.address;
  412. },
  413. set: function set(value) {
  414. this.model.address = value;
  415. }
  416. }]);
  417. return NullValue;
  418. }();
  419. var NumberValue = /*#__PURE__*/function () {
  420. function NumberValue(cell, value) {
  421. _classCallCheck(this, NumberValue);
  422. this.model = {
  423. address: cell.address,
  424. type: Cell.Types.Number,
  425. value: value
  426. };
  427. }
  428. _createClass(NumberValue, [{
  429. key: "toCsvString",
  430. value: function toCsvString() {
  431. return this.model.value.toString();
  432. }
  433. }, {
  434. key: "release",
  435. value: function release() {}
  436. }, {
  437. key: "toString",
  438. value: function toString() {
  439. return this.model.value.toString();
  440. }
  441. }, {
  442. key: "value",
  443. get: function get() {
  444. return this.model.value;
  445. },
  446. set: function set(value) {
  447. this.model.value = value;
  448. }
  449. }, {
  450. key: "type",
  451. get: function get() {
  452. return Cell.Types.Number;
  453. }
  454. }, {
  455. key: "effectiveType",
  456. get: function get() {
  457. return Cell.Types.Number;
  458. }
  459. }, {
  460. key: "address",
  461. get: function get() {
  462. return this.model.address;
  463. },
  464. set: function set(value) {
  465. this.model.address = value;
  466. }
  467. }]);
  468. return NumberValue;
  469. }();
  470. var StringValue = /*#__PURE__*/function () {
  471. function StringValue(cell, value) {
  472. _classCallCheck(this, StringValue);
  473. this.model = {
  474. address: cell.address,
  475. type: Cell.Types.String,
  476. value: value
  477. };
  478. }
  479. _createClass(StringValue, [{
  480. key: "toCsvString",
  481. value: function toCsvString() {
  482. return "\"".concat(this.model.value.replace(/"/g, '""'), "\"");
  483. }
  484. }, {
  485. key: "release",
  486. value: function release() {}
  487. }, {
  488. key: "toString",
  489. value: function toString() {
  490. return this.model.value;
  491. }
  492. }, {
  493. key: "value",
  494. get: function get() {
  495. return this.model.value;
  496. },
  497. set: function set(value) {
  498. this.model.value = value;
  499. }
  500. }, {
  501. key: "type",
  502. get: function get() {
  503. return Cell.Types.String;
  504. }
  505. }, {
  506. key: "effectiveType",
  507. get: function get() {
  508. return Cell.Types.String;
  509. }
  510. }, {
  511. key: "address",
  512. get: function get() {
  513. return this.model.address;
  514. },
  515. set: function set(value) {
  516. this.model.address = value;
  517. }
  518. }]);
  519. return StringValue;
  520. }();
  521. var RichTextValue = /*#__PURE__*/function () {
  522. function RichTextValue(cell, value) {
  523. _classCallCheck(this, RichTextValue);
  524. this.model = {
  525. address: cell.address,
  526. type: Cell.Types.String,
  527. value: value
  528. };
  529. }
  530. _createClass(RichTextValue, [{
  531. key: "toString",
  532. value: function toString() {
  533. return this.model.value.richText.map(function (t) {
  534. return t.text;
  535. }).join('');
  536. }
  537. }, {
  538. key: "toCsvString",
  539. value: function toCsvString() {
  540. return "\"".concat(this.text.replace(/"/g, '""'), "\"");
  541. }
  542. }, {
  543. key: "release",
  544. value: function release() {}
  545. }, {
  546. key: "value",
  547. get: function get() {
  548. return this.model.value;
  549. },
  550. set: function set(value) {
  551. this.model.value = value;
  552. }
  553. }, {
  554. key: "type",
  555. get: function get() {
  556. return Cell.Types.RichText;
  557. }
  558. }, {
  559. key: "effectiveType",
  560. get: function get() {
  561. return Cell.Types.RichText;
  562. }
  563. }, {
  564. key: "address",
  565. get: function get() {
  566. return this.model.address;
  567. },
  568. set: function set(value) {
  569. this.model.address = value;
  570. }
  571. }]);
  572. return RichTextValue;
  573. }();
  574. var DateValue = /*#__PURE__*/function () {
  575. function DateValue(cell, value) {
  576. _classCallCheck(this, DateValue);
  577. this.model = {
  578. address: cell.address,
  579. type: Cell.Types.Date,
  580. value: value
  581. };
  582. }
  583. _createClass(DateValue, [{
  584. key: "toCsvString",
  585. value: function toCsvString() {
  586. return this.model.value.toISOString();
  587. }
  588. }, {
  589. key: "release",
  590. value: function release() {}
  591. }, {
  592. key: "toString",
  593. value: function toString() {
  594. return this.model.value.toString();
  595. }
  596. }, {
  597. key: "value",
  598. get: function get() {
  599. return this.model.value;
  600. },
  601. set: function set(value) {
  602. this.model.value = value;
  603. }
  604. }, {
  605. key: "type",
  606. get: function get() {
  607. return Cell.Types.Date;
  608. }
  609. }, {
  610. key: "effectiveType",
  611. get: function get() {
  612. return Cell.Types.Date;
  613. }
  614. }, {
  615. key: "address",
  616. get: function get() {
  617. return this.model.address;
  618. },
  619. set: function set(value) {
  620. this.model.address = value;
  621. }
  622. }]);
  623. return DateValue;
  624. }();
  625. var HyperlinkValue = /*#__PURE__*/function () {
  626. function HyperlinkValue(cell, value) {
  627. _classCallCheck(this, HyperlinkValue);
  628. this.model = {
  629. address: cell.address,
  630. type: Cell.Types.Hyperlink,
  631. text: value ? value.text : undefined,
  632. hyperlink: value ? value.hyperlink : undefined
  633. };
  634. if (value && value.tooltip) {
  635. this.model.tooltip = value.tooltip;
  636. }
  637. }
  638. _createClass(HyperlinkValue, [{
  639. key: "toCsvString",
  640. value: function toCsvString() {
  641. return this.model.hyperlink;
  642. }
  643. }, {
  644. key: "release",
  645. value: function release() {}
  646. }, {
  647. key: "toString",
  648. value: function toString() {
  649. return this.model.text;
  650. }
  651. }, {
  652. key: "value",
  653. get: function get() {
  654. var v = {
  655. text: this.model.text,
  656. hyperlink: this.model.hyperlink
  657. };
  658. if (this.model.tooltip) {
  659. v.tooltip = this.model.tooltip;
  660. }
  661. return v;
  662. },
  663. set: function set(value) {
  664. this.model = {
  665. text: value.text,
  666. hyperlink: value.hyperlink
  667. };
  668. if (value.tooltip) {
  669. this.model.tooltip = value.tooltip;
  670. }
  671. }
  672. }, {
  673. key: "text",
  674. get: function get() {
  675. return this.model.text;
  676. },
  677. set: function set(value) {
  678. this.model.text = value;
  679. }
  680. /*
  681. get tooltip() {
  682. return this.model.tooltip;
  683. }
  684. set tooltip(value) {
  685. this.model.tooltip = value;
  686. } */
  687. }, {
  688. key: "hyperlink",
  689. get: function get() {
  690. return this.model.hyperlink;
  691. },
  692. set: function set(value) {
  693. this.model.hyperlink = value;
  694. }
  695. }, {
  696. key: "type",
  697. get: function get() {
  698. return Cell.Types.Hyperlink;
  699. }
  700. }, {
  701. key: "effectiveType",
  702. get: function get() {
  703. return Cell.Types.Hyperlink;
  704. }
  705. }, {
  706. key: "address",
  707. get: function get() {
  708. return this.model.address;
  709. },
  710. set: function set(value) {
  711. this.model.address = value;
  712. }
  713. }]);
  714. return HyperlinkValue;
  715. }();
  716. var MergeValue = /*#__PURE__*/function () {
  717. function MergeValue(cell, master) {
  718. _classCallCheck(this, MergeValue);
  719. this.model = {
  720. address: cell.address,
  721. type: Cell.Types.Merge,
  722. master: master ? master.address : undefined
  723. };
  724. this._master = master;
  725. if (master) {
  726. master.addMergeRef();
  727. }
  728. }
  729. _createClass(MergeValue, [{
  730. key: "isMergedTo",
  731. value: function isMergedTo(master) {
  732. return master === this._master;
  733. }
  734. }, {
  735. key: "toCsvString",
  736. value: function toCsvString() {
  737. return '';
  738. }
  739. }, {
  740. key: "release",
  741. value: function release() {
  742. this._master.releaseMergeRef();
  743. }
  744. }, {
  745. key: "toString",
  746. value: function toString() {
  747. return this.value.toString();
  748. }
  749. }, {
  750. key: "value",
  751. get: function get() {
  752. return this._master.value;
  753. },
  754. set: function set(value) {
  755. if (value instanceof Cell) {
  756. if (this._master) {
  757. this._master.releaseMergeRef();
  758. }
  759. value.addMergeRef();
  760. this._master = value;
  761. } else {
  762. this._master.value = value;
  763. }
  764. }
  765. }, {
  766. key: "master",
  767. get: function get() {
  768. return this._master;
  769. }
  770. }, {
  771. key: "type",
  772. get: function get() {
  773. return Cell.Types.Merge;
  774. }
  775. }, {
  776. key: "effectiveType",
  777. get: function get() {
  778. return this._master.effectiveType;
  779. }
  780. }, {
  781. key: "address",
  782. get: function get() {
  783. return this.model.address;
  784. },
  785. set: function set(value) {
  786. this.model.address = value;
  787. }
  788. }]);
  789. return MergeValue;
  790. }();
  791. var FormulaValue = /*#__PURE__*/function () {
  792. function FormulaValue(cell, value) {
  793. _classCallCheck(this, FormulaValue);
  794. this.cell = cell;
  795. this.model = {
  796. address: cell.address,
  797. type: Cell.Types.Formula,
  798. shareType: value ? value.shareType : undefined,
  799. ref: value ? value.ref : undefined,
  800. formula: value ? value.formula : undefined,
  801. sharedFormula: value ? value.sharedFormula : undefined,
  802. result: value ? value.result : undefined
  803. };
  804. }
  805. _createClass(FormulaValue, [{
  806. key: "_copyModel",
  807. value: function _copyModel(model) {
  808. var copy = {};
  809. var cp = function cp(name) {
  810. var value = model[name];
  811. if (value) {
  812. copy[name] = value;
  813. }
  814. };
  815. cp('formula');
  816. cp('result');
  817. cp('ref');
  818. cp('shareType');
  819. cp('sharedFormula');
  820. return copy;
  821. }
  822. }, {
  823. key: "validate",
  824. value: function validate(value) {
  825. switch (Value.getType(value)) {
  826. case Cell.Types.Null:
  827. case Cell.Types.String:
  828. case Cell.Types.Number:
  829. case Cell.Types.Date:
  830. break;
  831. case Cell.Types.Hyperlink:
  832. case Cell.Types.Formula:
  833. default:
  834. throw new Error('Cannot process that type of result value');
  835. }
  836. }
  837. }, {
  838. key: "_getTranslatedFormula",
  839. value: function _getTranslatedFormula() {
  840. if (!this._translatedFormula && this.model.sharedFormula) {
  841. var worksheet = this.cell.worksheet;
  842. var master = worksheet.findCell(this.model.sharedFormula);
  843. this._translatedFormula = master && slideFormula(master.formula, master.address, this.model.address);
  844. }
  845. return this._translatedFormula;
  846. }
  847. }, {
  848. key: "toCsvString",
  849. value: function toCsvString() {
  850. return "".concat(this.model.result || '');
  851. }
  852. }, {
  853. key: "release",
  854. value: function release() {}
  855. }, {
  856. key: "toString",
  857. value: function toString() {
  858. return this.model.result ? this.model.result.toString() : '';
  859. }
  860. }, {
  861. key: "value",
  862. get: function get() {
  863. return this._copyModel(this.model);
  864. },
  865. set: function set(value) {
  866. this.model = this._copyModel(value);
  867. }
  868. }, {
  869. key: "dependencies",
  870. get: function get() {
  871. // find all the ranges and cells mentioned in the formula
  872. var ranges = this.formula.match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g);
  873. var cells = this.formula.replace(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g, '').match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}/g);
  874. return {
  875. ranges: ranges,
  876. cells: cells
  877. };
  878. }
  879. }, {
  880. key: "formula",
  881. get: function get() {
  882. return this.model.formula || this._getTranslatedFormula();
  883. },
  884. set: function set(value) {
  885. this.model.formula = value;
  886. }
  887. }, {
  888. key: "formulaType",
  889. get: function get() {
  890. if (this.model.formula) {
  891. return Enums.FormulaType.Master;
  892. }
  893. if (this.model.sharedFormula) {
  894. return Enums.FormulaType.Shared;
  895. }
  896. return Enums.FormulaType.None;
  897. }
  898. }, {
  899. key: "result",
  900. get: function get() {
  901. return this.model.result;
  902. },
  903. set: function set(value) {
  904. this.model.result = value;
  905. }
  906. }, {
  907. key: "type",
  908. get: function get() {
  909. return Cell.Types.Formula;
  910. }
  911. }, {
  912. key: "effectiveType",
  913. get: function get() {
  914. var v = this.model.result;
  915. if (v === null || v === undefined) {
  916. return Enums.ValueType.Null;
  917. }
  918. if (v instanceof String || typeof v === 'string') {
  919. return Enums.ValueType.String;
  920. }
  921. if (typeof v === 'number') {
  922. return Enums.ValueType.Number;
  923. }
  924. if (v instanceof Date) {
  925. return Enums.ValueType.Date;
  926. }
  927. if (v.text && v.hyperlink) {
  928. return Enums.ValueType.Hyperlink;
  929. }
  930. if (v.formula) {
  931. return Enums.ValueType.Formula;
  932. }
  933. return Enums.ValueType.Null;
  934. }
  935. }, {
  936. key: "address",
  937. get: function get() {
  938. return this.model.address;
  939. },
  940. set: function set(value) {
  941. this.model.address = value;
  942. }
  943. }]);
  944. return FormulaValue;
  945. }();
  946. var SharedStringValue = /*#__PURE__*/function () {
  947. function SharedStringValue(cell, value) {
  948. _classCallCheck(this, SharedStringValue);
  949. this.model = {
  950. address: cell.address,
  951. type: Cell.Types.SharedString,
  952. value: value
  953. };
  954. }
  955. _createClass(SharedStringValue, [{
  956. key: "toCsvString",
  957. value: function toCsvString() {
  958. return this.model.value.toString();
  959. }
  960. }, {
  961. key: "release",
  962. value: function release() {}
  963. }, {
  964. key: "toString",
  965. value: function toString() {
  966. return this.model.value.toString();
  967. }
  968. }, {
  969. key: "value",
  970. get: function get() {
  971. return this.model.value;
  972. },
  973. set: function set(value) {
  974. this.model.value = value;
  975. }
  976. }, {
  977. key: "type",
  978. get: function get() {
  979. return Cell.Types.SharedString;
  980. }
  981. }, {
  982. key: "effectiveType",
  983. get: function get() {
  984. return Cell.Types.SharedString;
  985. }
  986. }, {
  987. key: "address",
  988. get: function get() {
  989. return this.model.address;
  990. },
  991. set: function set(value) {
  992. this.model.address = value;
  993. }
  994. }]);
  995. return SharedStringValue;
  996. }();
  997. var BooleanValue = /*#__PURE__*/function () {
  998. function BooleanValue(cell, value) {
  999. _classCallCheck(this, BooleanValue);
  1000. this.model = {
  1001. address: cell.address,
  1002. type: Cell.Types.Boolean,
  1003. value: value
  1004. };
  1005. }
  1006. _createClass(BooleanValue, [{
  1007. key: "toCsvString",
  1008. value: function toCsvString() {
  1009. return this.model.value ? 1 : 0;
  1010. }
  1011. }, {
  1012. key: "release",
  1013. value: function release() {}
  1014. }, {
  1015. key: "toString",
  1016. value: function toString() {
  1017. return this.model.value.toString();
  1018. }
  1019. }, {
  1020. key: "value",
  1021. get: function get() {
  1022. return this.model.value;
  1023. },
  1024. set: function set(value) {
  1025. this.model.value = value;
  1026. }
  1027. }, {
  1028. key: "type",
  1029. get: function get() {
  1030. return Cell.Types.Boolean;
  1031. }
  1032. }, {
  1033. key: "effectiveType",
  1034. get: function get() {
  1035. return Cell.Types.Boolean;
  1036. }
  1037. }, {
  1038. key: "address",
  1039. get: function get() {
  1040. return this.model.address;
  1041. },
  1042. set: function set(value) {
  1043. this.model.address = value;
  1044. }
  1045. }]);
  1046. return BooleanValue;
  1047. }();
  1048. var ErrorValue = /*#__PURE__*/function () {
  1049. function ErrorValue(cell, value) {
  1050. _classCallCheck(this, ErrorValue);
  1051. this.model = {
  1052. address: cell.address,
  1053. type: Cell.Types.Error,
  1054. value: value
  1055. };
  1056. }
  1057. _createClass(ErrorValue, [{
  1058. key: "toCsvString",
  1059. value: function toCsvString() {
  1060. return this.toString();
  1061. }
  1062. }, {
  1063. key: "release",
  1064. value: function release() {}
  1065. }, {
  1066. key: "toString",
  1067. value: function toString() {
  1068. return this.model.value.error.toString();
  1069. }
  1070. }, {
  1071. key: "value",
  1072. get: function get() {
  1073. return this.model.value;
  1074. },
  1075. set: function set(value) {
  1076. this.model.value = value;
  1077. }
  1078. }, {
  1079. key: "type",
  1080. get: function get() {
  1081. return Cell.Types.Error;
  1082. }
  1083. }, {
  1084. key: "effectiveType",
  1085. get: function get() {
  1086. return Cell.Types.Error;
  1087. }
  1088. }, {
  1089. key: "address",
  1090. get: function get() {
  1091. return this.model.address;
  1092. },
  1093. set: function set(value) {
  1094. this.model.address = value;
  1095. }
  1096. }]);
  1097. return ErrorValue;
  1098. }();
  1099. var JSONValue = /*#__PURE__*/function () {
  1100. function JSONValue(cell, value) {
  1101. _classCallCheck(this, JSONValue);
  1102. this.model = {
  1103. address: cell.address,
  1104. type: Cell.Types.String,
  1105. value: JSON.stringify(value),
  1106. rawValue: value
  1107. };
  1108. }
  1109. _createClass(JSONValue, [{
  1110. key: "toCsvString",
  1111. value: function toCsvString() {
  1112. return this.model.value;
  1113. }
  1114. }, {
  1115. key: "release",
  1116. value: function release() {}
  1117. }, {
  1118. key: "toString",
  1119. value: function toString() {
  1120. return this.model.value;
  1121. }
  1122. }, {
  1123. key: "value",
  1124. get: function get() {
  1125. return this.model.rawValue;
  1126. },
  1127. set: function set(value) {
  1128. this.model.rawValue = value;
  1129. this.model.value = JSON.stringify(value);
  1130. }
  1131. }, {
  1132. key: "type",
  1133. get: function get() {
  1134. return Cell.Types.String;
  1135. }
  1136. }, {
  1137. key: "effectiveType",
  1138. get: function get() {
  1139. return Cell.Types.String;
  1140. }
  1141. }, {
  1142. key: "address",
  1143. get: function get() {
  1144. return this.model.address;
  1145. },
  1146. set: function set(value) {
  1147. this.model.address = value;
  1148. }
  1149. }]);
  1150. return JSONValue;
  1151. }(); // Value is a place to hold common static Value type functions
  1152. var Value = {
  1153. getType: function getType(value) {
  1154. if (value === null || value === undefined) {
  1155. return Cell.Types.Null;
  1156. }
  1157. if (value instanceof String || typeof value === 'string') {
  1158. return Cell.Types.String;
  1159. }
  1160. if (typeof value === 'number') {
  1161. return Cell.Types.Number;
  1162. }
  1163. if (typeof value === 'boolean') {
  1164. return Cell.Types.Boolean;
  1165. }
  1166. if (value instanceof Date) {
  1167. return Cell.Types.Date;
  1168. }
  1169. if (value.text && value.hyperlink) {
  1170. return Cell.Types.Hyperlink;
  1171. }
  1172. if (value.formula || value.sharedFormula) {
  1173. return Cell.Types.Formula;
  1174. }
  1175. if (value.richText) {
  1176. return Cell.Types.RichText;
  1177. }
  1178. if (value.sharedString) {
  1179. return Cell.Types.SharedString;
  1180. }
  1181. if (value.error) {
  1182. return Cell.Types.Error;
  1183. }
  1184. return Cell.Types.JSON;
  1185. },
  1186. // map valueType to constructor
  1187. types: [{
  1188. t: Cell.Types.Null,
  1189. f: NullValue
  1190. }, {
  1191. t: Cell.Types.Number,
  1192. f: NumberValue
  1193. }, {
  1194. t: Cell.Types.String,
  1195. f: StringValue
  1196. }, {
  1197. t: Cell.Types.Date,
  1198. f: DateValue
  1199. }, {
  1200. t: Cell.Types.Hyperlink,
  1201. f: HyperlinkValue
  1202. }, {
  1203. t: Cell.Types.Formula,
  1204. f: FormulaValue
  1205. }, {
  1206. t: Cell.Types.Merge,
  1207. f: MergeValue
  1208. }, {
  1209. t: Cell.Types.JSON,
  1210. f: JSONValue
  1211. }, {
  1212. t: Cell.Types.SharedString,
  1213. f: SharedStringValue
  1214. }, {
  1215. t: Cell.Types.RichText,
  1216. f: RichTextValue
  1217. }, {
  1218. t: Cell.Types.Boolean,
  1219. f: BooleanValue
  1220. }, {
  1221. t: Cell.Types.Error,
  1222. f: ErrorValue
  1223. }].reduce(function (p, t) {
  1224. p[t.t] = t.f;
  1225. return p;
  1226. }, []),
  1227. create: function create(type, cell, value) {
  1228. var T = this.types[type];
  1229. if (!T) {
  1230. throw new Error("Could not create Value of type ".concat(type));
  1231. }
  1232. return new T(cell, value);
  1233. }
  1234. };
  1235. module.exports = Cell;
  1236. //# sourceMappingURL=cell.js.map