cell.js 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /* eslint-disable max-classes-per-file */
  2. const colCache = require('../utils/col-cache');
  3. const _ = require('../utils/under-dash');
  4. const Enums = require('./enums');
  5. const {slideFormula} = require('../utils/shared-formula');
  6. const Note = require('./note');
  7. // Cell requirements
  8. // Operate inside a worksheet
  9. // Store and retrieve a value with a range of types: text, number, date, hyperlink, reference, formula, etc.
  10. // Manage/use and manipulate cell format either as local to cell or inherited from column or row.
  11. class Cell {
  12. constructor(row, column, address) {
  13. if (!row || !column) {
  14. throw new Error('A Cell needs a Row');
  15. }
  16. this._row = row;
  17. this._column = column;
  18. colCache.validateAddress(address);
  19. this._address = address;
  20. // TODO: lazy evaluation of this._value
  21. this._value = Value.create(Cell.Types.Null, this);
  22. this.style = this._mergeStyle(row.style, column.style, {});
  23. this._mergeCount = 0;
  24. }
  25. get worksheet() {
  26. return this._row.worksheet;
  27. }
  28. get workbook() {
  29. return this._row.worksheet.workbook;
  30. }
  31. // help GC by removing cyclic (and other) references
  32. destroy() {
  33. delete this.style;
  34. delete this._value;
  35. delete this._row;
  36. delete this._column;
  37. delete this._address;
  38. }
  39. // =========================================================================
  40. // Styles stuff
  41. get numFmt() {
  42. return this.style.numFmt;
  43. }
  44. set numFmt(value) {
  45. this.style.numFmt = value;
  46. }
  47. get font() {
  48. return this.style.font;
  49. }
  50. set font(value) {
  51. this.style.font = value;
  52. }
  53. get alignment() {
  54. return this.style.alignment;
  55. }
  56. set alignment(value) {
  57. this.style.alignment = value;
  58. }
  59. get border() {
  60. return this.style.border;
  61. }
  62. set border(value) {
  63. this.style.border = value;
  64. }
  65. get fill() {
  66. return this.style.fill;
  67. }
  68. set fill(value) {
  69. this.style.fill = value;
  70. }
  71. get protection() {
  72. return this.style.protection;
  73. }
  74. set protection(value) {
  75. this.style.protection = value;
  76. }
  77. _mergeStyle(rowStyle, colStyle, style) {
  78. const numFmt = (rowStyle && rowStyle.numFmt) || (colStyle && colStyle.numFmt);
  79. if (numFmt) style.numFmt = numFmt;
  80. const font = (rowStyle && rowStyle.font) || (colStyle && colStyle.font);
  81. if (font) style.font = font;
  82. const alignment = (rowStyle && rowStyle.alignment) || (colStyle && colStyle.alignment);
  83. if (alignment) style.alignment = alignment;
  84. const border = (rowStyle && rowStyle.border) || (colStyle && colStyle.border);
  85. if (border) style.border = border;
  86. const fill = (rowStyle && rowStyle.fill) || (colStyle && colStyle.fill);
  87. if (fill) style.fill = fill;
  88. const protection = (rowStyle && rowStyle.protection) || (colStyle && colStyle.protection);
  89. if (protection) style.protection = protection;
  90. return style;
  91. }
  92. // =========================================================================
  93. // return the address for this cell
  94. get address() {
  95. return this._address;
  96. }
  97. get row() {
  98. return this._row.number;
  99. }
  100. get col() {
  101. return this._column.number;
  102. }
  103. get $col$row() {
  104. return `$${this._column.letter}$${this.row}`;
  105. }
  106. // =========================================================================
  107. // Value stuff
  108. get type() {
  109. return this._value.type;
  110. }
  111. get effectiveType() {
  112. return this._value.effectiveType;
  113. }
  114. toCsvString() {
  115. return this._value.toCsvString();
  116. }
  117. // =========================================================================
  118. // Merge stuff
  119. addMergeRef() {
  120. this._mergeCount++;
  121. }
  122. releaseMergeRef() {
  123. this._mergeCount--;
  124. }
  125. get isMerged() {
  126. return this._mergeCount > 0 || this.type === Cell.Types.Merge;
  127. }
  128. merge(master, ignoreStyle) {
  129. this._value.release();
  130. this._value = Value.create(Cell.Types.Merge, this, master);
  131. if (!ignoreStyle) {
  132. this.style = master.style;
  133. }
  134. }
  135. unmerge() {
  136. if (this.type === Cell.Types.Merge) {
  137. this._value.release();
  138. this._value = Value.create(Cell.Types.Null, this);
  139. this.style = this._mergeStyle(this._row.style, this._column.style, {});
  140. }
  141. }
  142. isMergedTo(master) {
  143. if (this._value.type !== Cell.Types.Merge) return false;
  144. return this._value.isMergedTo(master);
  145. }
  146. get master() {
  147. if (this.type === Cell.Types.Merge) {
  148. return this._value.master;
  149. }
  150. return this; // an unmerged cell is its own master
  151. }
  152. get isHyperlink() {
  153. return this._value.type === Cell.Types.Hyperlink;
  154. }
  155. get hyperlink() {
  156. return this._value.hyperlink;
  157. }
  158. // return the value
  159. get value() {
  160. return this._value.value;
  161. }
  162. // set the value - can be number, string or raw
  163. set value(v) {
  164. // special case - merge cells set their master's value
  165. if (this.type === Cell.Types.Merge) {
  166. this._value.master.value = v;
  167. return;
  168. }
  169. this._value.release();
  170. // assign value
  171. this._value = Value.create(Value.getType(v), this, v);
  172. }
  173. get note() {
  174. return this._comment && this._comment.note;
  175. }
  176. set note(note) {
  177. this._comment = new Note(note);
  178. }
  179. get text() {
  180. return this._value.toString();
  181. }
  182. get html() {
  183. return _.escapeHtml(this.text);
  184. }
  185. toString() {
  186. return this.text;
  187. }
  188. _upgradeToHyperlink(hyperlink) {
  189. // if this cell is a string, turn it into a Hyperlink
  190. if (this.type === Cell.Types.String) {
  191. this._value = Value.create(Cell.Types.Hyperlink, this, {
  192. text: this._value.value,
  193. hyperlink,
  194. });
  195. }
  196. }
  197. // =========================================================================
  198. // Formula stuff
  199. get formula() {
  200. return this._value.formula;
  201. }
  202. get result() {
  203. return this._value.result;
  204. }
  205. get formulaType() {
  206. return this._value.formulaType;
  207. }
  208. // =========================================================================
  209. // Name stuff
  210. get fullAddress() {
  211. const {worksheet} = this._row;
  212. return {
  213. sheetName: worksheet.name,
  214. address: this.address,
  215. row: this.row,
  216. col: this.col,
  217. };
  218. }
  219. get name() {
  220. return this.names[0];
  221. }
  222. set name(value) {
  223. this.names = [value];
  224. }
  225. get names() {
  226. return this.workbook.definedNames.getNamesEx(this.fullAddress);
  227. }
  228. set names(value) {
  229. const {definedNames} = this.workbook;
  230. definedNames.removeAllNames(this.fullAddress);
  231. value.forEach(name => {
  232. definedNames.addEx(this.fullAddress, name);
  233. });
  234. }
  235. addName(name) {
  236. this.workbook.definedNames.addEx(this.fullAddress, name);
  237. }
  238. removeName(name) {
  239. this.workbook.definedNames.removeEx(this.fullAddress, name);
  240. }
  241. removeAllNames() {
  242. this.workbook.definedNames.removeAllNames(this.fullAddress);
  243. }
  244. // =========================================================================
  245. // Data Validation stuff
  246. get _dataValidations() {
  247. return this.worksheet.dataValidations;
  248. }
  249. get dataValidation() {
  250. return this._dataValidations.find(this.address);
  251. }
  252. set dataValidation(value) {
  253. this._dataValidations.add(this.address, value);
  254. }
  255. // =========================================================================
  256. // Model stuff
  257. get model() {
  258. const {model} = this._value;
  259. model.style = this.style;
  260. if (this._comment) {
  261. model.comment = this._comment.model;
  262. }
  263. return model;
  264. }
  265. set model(value) {
  266. this._value.release();
  267. this._value = Value.create(value.type, this);
  268. this._value.model = value;
  269. if (value.comment) {
  270. switch (value.comment.type) {
  271. case 'note':
  272. this._comment = Note.fromModel(value.comment);
  273. break;
  274. }
  275. }
  276. if (value.style) {
  277. this.style = value.style;
  278. } else {
  279. this.style = {};
  280. }
  281. }
  282. }
  283. Cell.Types = Enums.ValueType;
  284. // =============================================================================
  285. // Internal Value Types
  286. class NullValue {
  287. constructor(cell) {
  288. this.model = {
  289. address: cell.address,
  290. type: Cell.Types.Null,
  291. };
  292. }
  293. get value() {
  294. return null;
  295. }
  296. set value(value) {
  297. // nothing to do
  298. }
  299. get type() {
  300. return Cell.Types.Null;
  301. }
  302. get effectiveType() {
  303. return Cell.Types.Null;
  304. }
  305. get address() {
  306. return this.model.address;
  307. }
  308. set address(value) {
  309. this.model.address = value;
  310. }
  311. toCsvString() {
  312. return '';
  313. }
  314. release() {}
  315. toString() {
  316. return '';
  317. }
  318. }
  319. class NumberValue {
  320. constructor(cell, value) {
  321. this.model = {
  322. address: cell.address,
  323. type: Cell.Types.Number,
  324. value,
  325. };
  326. }
  327. get value() {
  328. return this.model.value;
  329. }
  330. set value(value) {
  331. this.model.value = value;
  332. }
  333. get type() {
  334. return Cell.Types.Number;
  335. }
  336. get effectiveType() {
  337. return Cell.Types.Number;
  338. }
  339. get address() {
  340. return this.model.address;
  341. }
  342. set address(value) {
  343. this.model.address = value;
  344. }
  345. toCsvString() {
  346. return this.model.value.toString();
  347. }
  348. release() {}
  349. toString() {
  350. return this.model.value.toString();
  351. }
  352. }
  353. class StringValue {
  354. constructor(cell, value) {
  355. this.model = {
  356. address: cell.address,
  357. type: Cell.Types.String,
  358. value,
  359. };
  360. }
  361. get value() {
  362. return this.model.value;
  363. }
  364. set value(value) {
  365. this.model.value = value;
  366. }
  367. get type() {
  368. return Cell.Types.String;
  369. }
  370. get effectiveType() {
  371. return Cell.Types.String;
  372. }
  373. get address() {
  374. return this.model.address;
  375. }
  376. set address(value) {
  377. this.model.address = value;
  378. }
  379. toCsvString() {
  380. return `"${this.model.value.replace(/"/g, '""')}"`;
  381. }
  382. release() {}
  383. toString() {
  384. return this.model.value;
  385. }
  386. }
  387. class RichTextValue {
  388. constructor(cell, value) {
  389. this.model = {
  390. address: cell.address,
  391. type: Cell.Types.String,
  392. value,
  393. };
  394. }
  395. get value() {
  396. return this.model.value;
  397. }
  398. set value(value) {
  399. this.model.value = value;
  400. }
  401. toString() {
  402. return this.model.value.richText.map(t => t.text).join('');
  403. }
  404. get type() {
  405. return Cell.Types.RichText;
  406. }
  407. get effectiveType() {
  408. return Cell.Types.RichText;
  409. }
  410. get address() {
  411. return this.model.address;
  412. }
  413. set address(value) {
  414. this.model.address = value;
  415. }
  416. toCsvString() {
  417. return `"${this.text.replace(/"/g, '""')}"`;
  418. }
  419. release() {}
  420. }
  421. class DateValue {
  422. constructor(cell, value) {
  423. this.model = {
  424. address: cell.address,
  425. type: Cell.Types.Date,
  426. value,
  427. };
  428. }
  429. get value() {
  430. return this.model.value;
  431. }
  432. set value(value) {
  433. this.model.value = value;
  434. }
  435. get type() {
  436. return Cell.Types.Date;
  437. }
  438. get effectiveType() {
  439. return Cell.Types.Date;
  440. }
  441. get address() {
  442. return this.model.address;
  443. }
  444. set address(value) {
  445. this.model.address = value;
  446. }
  447. toCsvString() {
  448. return this.model.value.toISOString();
  449. }
  450. release() {}
  451. toString() {
  452. return this.model.value.toString();
  453. }
  454. }
  455. class HyperlinkValue {
  456. constructor(cell, value) {
  457. this.model = {
  458. address: cell.address,
  459. type: Cell.Types.Hyperlink,
  460. text: value ? value.text : undefined,
  461. hyperlink: value ? value.hyperlink : undefined,
  462. };
  463. if (value && value.tooltip) {
  464. this.model.tooltip = value.tooltip;
  465. }
  466. }
  467. get value() {
  468. const v = {
  469. text: this.model.text,
  470. hyperlink: this.model.hyperlink,
  471. };
  472. if (this.model.tooltip) {
  473. v.tooltip = this.model.tooltip;
  474. }
  475. return v;
  476. }
  477. set value(value) {
  478. this.model = {
  479. text: value.text,
  480. hyperlink: value.hyperlink,
  481. };
  482. if (value.tooltip) {
  483. this.model.tooltip = value.tooltip;
  484. }
  485. }
  486. get text() {
  487. return this.model.text;
  488. }
  489. set text(value) {
  490. this.model.text = value;
  491. }
  492. /*
  493. get tooltip() {
  494. return this.model.tooltip;
  495. }
  496. set tooltip(value) {
  497. this.model.tooltip = value;
  498. } */
  499. get hyperlink() {
  500. return this.model.hyperlink;
  501. }
  502. set hyperlink(value) {
  503. this.model.hyperlink = value;
  504. }
  505. get type() {
  506. return Cell.Types.Hyperlink;
  507. }
  508. get effectiveType() {
  509. return Cell.Types.Hyperlink;
  510. }
  511. get address() {
  512. return this.model.address;
  513. }
  514. set address(value) {
  515. this.model.address = value;
  516. }
  517. toCsvString() {
  518. return this.model.hyperlink;
  519. }
  520. release() {}
  521. toString() {
  522. return this.model.text;
  523. }
  524. }
  525. class MergeValue {
  526. constructor(cell, master) {
  527. this.model = {
  528. address: cell.address,
  529. type: Cell.Types.Merge,
  530. master: master ? master.address : undefined,
  531. };
  532. this._master = master;
  533. if (master) {
  534. master.addMergeRef();
  535. }
  536. }
  537. get value() {
  538. return this._master.value;
  539. }
  540. set value(value) {
  541. if (value instanceof Cell) {
  542. if (this._master) {
  543. this._master.releaseMergeRef();
  544. }
  545. value.addMergeRef();
  546. this._master = value;
  547. } else {
  548. this._master.value = value;
  549. }
  550. }
  551. isMergedTo(master) {
  552. return master === this._master;
  553. }
  554. get master() {
  555. return this._master;
  556. }
  557. get type() {
  558. return Cell.Types.Merge;
  559. }
  560. get effectiveType() {
  561. return this._master.effectiveType;
  562. }
  563. get address() {
  564. return this.model.address;
  565. }
  566. set address(value) {
  567. this.model.address = value;
  568. }
  569. toCsvString() {
  570. return '';
  571. }
  572. release() {
  573. this._master.releaseMergeRef();
  574. }
  575. toString() {
  576. return this.value.toString();
  577. }
  578. }
  579. class FormulaValue {
  580. constructor(cell, value) {
  581. this.cell = cell;
  582. this.model = {
  583. address: cell.address,
  584. type: Cell.Types.Formula,
  585. shareType: value ? value.shareType : undefined,
  586. ref: value ? value.ref : undefined,
  587. formula: value ? value.formula : undefined,
  588. sharedFormula: value ? value.sharedFormula : undefined,
  589. result: value ? value.result : undefined,
  590. };
  591. }
  592. _copyModel(model) {
  593. const copy = {};
  594. const cp = name => {
  595. const value = model[name];
  596. if (value) {
  597. copy[name] = value;
  598. }
  599. };
  600. cp('formula');
  601. cp('result');
  602. cp('ref');
  603. cp('shareType');
  604. cp('sharedFormula');
  605. return copy;
  606. }
  607. get value() {
  608. return this._copyModel(this.model);
  609. }
  610. set value(value) {
  611. this.model = this._copyModel(value);
  612. }
  613. validate(value) {
  614. switch (Value.getType(value)) {
  615. case Cell.Types.Null:
  616. case Cell.Types.String:
  617. case Cell.Types.Number:
  618. case Cell.Types.Date:
  619. break;
  620. case Cell.Types.Hyperlink:
  621. case Cell.Types.Formula:
  622. default:
  623. throw new Error('Cannot process that type of result value');
  624. }
  625. }
  626. get dependencies() {
  627. // find all the ranges and cells mentioned in the formula
  628. const ranges = this.formula.match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g);
  629. const cells = this.formula
  630. .replace(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}:[A-Z]{1,3}\d{1,4}/g, '')
  631. .match(/([a-zA-Z0-9]+!)?[A-Z]{1,3}\d{1,4}/g);
  632. return {
  633. ranges,
  634. cells,
  635. };
  636. }
  637. get formula() {
  638. return this.model.formula || this._getTranslatedFormula();
  639. }
  640. set formula(value) {
  641. this.model.formula = value;
  642. }
  643. get formulaType() {
  644. if (this.model.formula) {
  645. return Enums.FormulaType.Master;
  646. }
  647. if (this.model.sharedFormula) {
  648. return Enums.FormulaType.Shared;
  649. }
  650. return Enums.FormulaType.None;
  651. }
  652. get result() {
  653. return this.model.result;
  654. }
  655. set result(value) {
  656. this.model.result = value;
  657. }
  658. get type() {
  659. return Cell.Types.Formula;
  660. }
  661. get effectiveType() {
  662. const v = this.model.result;
  663. if (v === null || v === undefined) {
  664. return Enums.ValueType.Null;
  665. }
  666. if (v instanceof String || typeof v === 'string') {
  667. return Enums.ValueType.String;
  668. }
  669. if (typeof v === 'number') {
  670. return Enums.ValueType.Number;
  671. }
  672. if (v instanceof Date) {
  673. return Enums.ValueType.Date;
  674. }
  675. if (v.text && v.hyperlink) {
  676. return Enums.ValueType.Hyperlink;
  677. }
  678. if (v.formula) {
  679. return Enums.ValueType.Formula;
  680. }
  681. return Enums.ValueType.Null;
  682. }
  683. get address() {
  684. return this.model.address;
  685. }
  686. set address(value) {
  687. this.model.address = value;
  688. }
  689. _getTranslatedFormula() {
  690. if (!this._translatedFormula && this.model.sharedFormula) {
  691. const {worksheet} = this.cell;
  692. const master = worksheet.findCell(this.model.sharedFormula);
  693. this._translatedFormula =
  694. master && slideFormula(master.formula, master.address, this.model.address);
  695. }
  696. return this._translatedFormula;
  697. }
  698. toCsvString() {
  699. return `${this.model.result || ''}`;
  700. }
  701. release() {}
  702. toString() {
  703. return this.model.result ? this.model.result.toString() : '';
  704. }
  705. }
  706. class SharedStringValue {
  707. constructor(cell, value) {
  708. this.model = {
  709. address: cell.address,
  710. type: Cell.Types.SharedString,
  711. value,
  712. };
  713. }
  714. get value() {
  715. return this.model.value;
  716. }
  717. set value(value) {
  718. this.model.value = value;
  719. }
  720. get type() {
  721. return Cell.Types.SharedString;
  722. }
  723. get effectiveType() {
  724. return Cell.Types.SharedString;
  725. }
  726. get address() {
  727. return this.model.address;
  728. }
  729. set address(value) {
  730. this.model.address = value;
  731. }
  732. toCsvString() {
  733. return this.model.value.toString();
  734. }
  735. release() {}
  736. toString() {
  737. return this.model.value.toString();
  738. }
  739. }
  740. class BooleanValue {
  741. constructor(cell, value) {
  742. this.model = {
  743. address: cell.address,
  744. type: Cell.Types.Boolean,
  745. value,
  746. };
  747. }
  748. get value() {
  749. return this.model.value;
  750. }
  751. set value(value) {
  752. this.model.value = value;
  753. }
  754. get type() {
  755. return Cell.Types.Boolean;
  756. }
  757. get effectiveType() {
  758. return Cell.Types.Boolean;
  759. }
  760. get address() {
  761. return this.model.address;
  762. }
  763. set address(value) {
  764. this.model.address = value;
  765. }
  766. toCsvString() {
  767. return this.model.value ? 1 : 0;
  768. }
  769. release() {}
  770. toString() {
  771. return this.model.value.toString();
  772. }
  773. }
  774. class ErrorValue {
  775. constructor(cell, value) {
  776. this.model = {
  777. address: cell.address,
  778. type: Cell.Types.Error,
  779. value,
  780. };
  781. }
  782. get value() {
  783. return this.model.value;
  784. }
  785. set value(value) {
  786. this.model.value = value;
  787. }
  788. get type() {
  789. return Cell.Types.Error;
  790. }
  791. get effectiveType() {
  792. return Cell.Types.Error;
  793. }
  794. get address() {
  795. return this.model.address;
  796. }
  797. set address(value) {
  798. this.model.address = value;
  799. }
  800. toCsvString() {
  801. return this.toString();
  802. }
  803. release() {}
  804. toString() {
  805. return this.model.value.error.toString();
  806. }
  807. }
  808. class JSONValue {
  809. constructor(cell, value) {
  810. this.model = {
  811. address: cell.address,
  812. type: Cell.Types.String,
  813. value: JSON.stringify(value),
  814. rawValue: value,
  815. };
  816. }
  817. get value() {
  818. return this.model.rawValue;
  819. }
  820. set value(value) {
  821. this.model.rawValue = value;
  822. this.model.value = JSON.stringify(value);
  823. }
  824. get type() {
  825. return Cell.Types.String;
  826. }
  827. get effectiveType() {
  828. return Cell.Types.String;
  829. }
  830. get address() {
  831. return this.model.address;
  832. }
  833. set address(value) {
  834. this.model.address = value;
  835. }
  836. toCsvString() {
  837. return this.model.value;
  838. }
  839. release() {}
  840. toString() {
  841. return this.model.value;
  842. }
  843. }
  844. // Value is a place to hold common static Value type functions
  845. const Value = {
  846. getType(value) {
  847. if (value === null || value === undefined) {
  848. return Cell.Types.Null;
  849. }
  850. if (value instanceof String || typeof value === 'string') {
  851. return Cell.Types.String;
  852. }
  853. if (typeof value === 'number') {
  854. return Cell.Types.Number;
  855. }
  856. if (typeof value === 'boolean') {
  857. return Cell.Types.Boolean;
  858. }
  859. if (value instanceof Date) {
  860. return Cell.Types.Date;
  861. }
  862. if (value.text && value.hyperlink) {
  863. return Cell.Types.Hyperlink;
  864. }
  865. if (value.formula || value.sharedFormula) {
  866. return Cell.Types.Formula;
  867. }
  868. if (value.richText) {
  869. return Cell.Types.RichText;
  870. }
  871. if (value.sharedString) {
  872. return Cell.Types.SharedString;
  873. }
  874. if (value.error) {
  875. return Cell.Types.Error;
  876. }
  877. return Cell.Types.JSON;
  878. },
  879. // map valueType to constructor
  880. types: [
  881. {t: Cell.Types.Null, f: NullValue},
  882. {t: Cell.Types.Number, f: NumberValue},
  883. {t: Cell.Types.String, f: StringValue},
  884. {t: Cell.Types.Date, f: DateValue},
  885. {t: Cell.Types.Hyperlink, f: HyperlinkValue},
  886. {t: Cell.Types.Formula, f: FormulaValue},
  887. {t: Cell.Types.Merge, f: MergeValue},
  888. {t: Cell.Types.JSON, f: JSONValue},
  889. {t: Cell.Types.SharedString, f: SharedStringValue},
  890. {t: Cell.Types.RichText, f: RichTextValue},
  891. {t: Cell.Types.Boolean, f: BooleanValue},
  892. {t: Cell.Types.Error, f: ErrorValue},
  893. ].reduce((p, t) => {
  894. p[t.t] = t.f;
  895. return p;
  896. }, []),
  897. create(type, cell, value) {
  898. const T = this.types[type];
  899. if (!T) {
  900. throw new Error(`Could not create Value of type ${type}`);
  901. }
  902. return new T(cell, value);
  903. },
  904. };
  905. module.exports = Cell;