name:
dist/Cell.js
-rw-r--r--
7174
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12exports.__esModule = true;
13var CELL_ID_ERROR = "CELL_ID_ERROR";
14exports.CELL_ID_ERROR = CELL_ID_ERROR;
15/**
16 * Represents a cell id error, and is thrown when a cells id does not conform to A1 notation.
17 */
18var CellIdError = /** @class */ (function (_super) {
19 __extends(CellIdError, _super);
20 function CellIdError(msg) {
21 var _this = _super.call(this) || this;
22 _this.message = msg;
23 _this.name = CELL_ID_ERROR;
24 return _this;
25 }
26 return CellIdError;
27}(Error));
28exports.CellIdError = CellIdError;
29/**
30 * Cell represents a cell in the spreadsheet. It contains a nullable rawFormulaText, and a value, which is not nullable unless
31 * the parsing of the rawFormulaText results in an error.
32 */
33var Cell = /** @class */ (function () {
34 /**
35 * Creates an empty cell with an id.
36 * @param id key of the cell in A1-format.
37 */
38 function Cell(id) {
39 /**
40 * The raw formula text that can be parse, excluding the proceeding =
41 * E.g: SUM(A2:A4, 10)
42 */
43 this.rawFormulaText = null;
44 this.typedValue = null;
45 this.dependencies = [];
46 this.error = null;
47 if (!id.match(/^(?:[A-Za-z]+[0-9]+)$/)) {
48 throw new CellIdError("Cell id " + id + " not valid");
49 }
50 var key = parseKey(id);
51 this.id = id;
52 this.row = key.y;
53 this.col = key.x;
54 }
55 /**
56 * Update this cell's dependencies, where `dependencies` is a unique list of A1-format cell IDs.
57 * @param dependencies to merge with existing dependencies.
58 */
59 Cell.prototype.updateDependencies = function (dependencies) {
60 for (var index in dependencies) {
61 if (this.dependencies.indexOf(dependencies[index]) === -1) {
62 this.dependencies.push(dependencies[index]);
63 }
64 }
65 };
66 /**
67 * Return a list of dependencies in A1-format cell IDs, in no particular order, but likely in order of occurrence in
68 * rawFormulaText.
69 * @returns {Array<string>} list of dependencies in A1-format
70 */
71 Cell.prototype.getDependencies = function () {
72 return this.dependencies;
73 };
74 /**
75 * Return the zero-indexed column number of this cell.
76 * @returns {number} column
77 */
78 Cell.prototype.getColumn = function () {
79 return this.col;
80 };
81 /**
82 * Return the zero-indexed row number of this cell.
83 * @returns {number} row
84 */
85 Cell.prototype.getRow = function () {
86 return this.row;
87 };
88 /**
89 * Get the A1-format ID of this cell.
90 * @returns {string} cell ID
91 */
92 Cell.prototype.getId = function () {
93 return this.id;
94 };
95 /**
96 * Get the rawFormulaText of this cell if set. Defaults to null, so should be used in combination with hasFormula().
97 * @returns {string} rawFormulaText of this cell, if set. Nullable.
98 */
99 Cell.prototype.getFormula = function () {
100 return this.rawFormulaText;
101 };
102 /**
103 * Returns true if this cell has a formula to be parsed.
104 * @returns {boolean}
105 */
106 Cell.prototype.hasFormula = function () {
107 return this.rawFormulaText !== null;
108 };
109 /**
110 * Sets the value or rawFormulaText for this cell. If the input begins with =, then it is considered to be a rawFormulaText. If it
111 * is not, then it is a value, and set as the raw value for this cell.
112 * @param rawFormula
113 */
114 Cell.prototype.setValue = function (rawFormula) {
115 if (typeof rawFormula === "string" && rawFormula.charAt(0) === "=") {
116 this.rawFormulaText = rawFormula.substr(1);
117 }
118 else {
119 this.typedValue = rawFormula;
120 }
121 };
122 /**
123 * Gets the rawFormulaText for this cell, which is either null or a string.
124 * @returns {string}
125 */
126 Cell.prototype.getRawFormulaText = function () {
127 return this.rawFormulaText;
128 };
129 /**
130 * Get the value of this cell if a value is present. If this cell was given a formula but not a value, this may return
131 * null.
132 * @returns {any}
133 */
134 Cell.prototype.getValue = function () {
135 return this.typedValue;
136 };
137 /**
138 * CLears a cells value.
139 */
140 Cell.prototype.clearValue = function () {
141 this.typedValue = null;
142 };
143 /**
144 * Set error for this cell. Usually in the case of a parse error when parsing the rawFormulaText.
145 * @param error to set.
146 */
147 Cell.prototype.setError = function (error) {
148 this.error = error;
149 };
150 /**
151 * Get the error for this cell. If the rawFormulaText is not parsed properly, or is null, this could be null.
152 * @returns {Error} error to return, could be null.
153 */
154 Cell.prototype.getError = function () {
155 return this.error;
156 };
157 /**
158 * Easier way to check if this cell has an error.
159 * @returns {boolean}
160 */
161 Cell.prototype.hasError = function () {
162 return this.error !== null;
163 };
164 /**
165 * A cell is deemed blank if it contains no value, no error, and no typed value.
166 * @returns {boolean}
167 */
168 Cell.prototype.isBlank = function () {
169 return this.error === null && this.rawFormulaText === null && this.typedValue === null;
170 };
171 /**
172 * Returns the human-readable string representation of this cell, omitting some obvious fields.
173 * @returns {string}
174 */
175 Cell.prototype.toString = function () {
176 return "id=" + this.id + ", value=" + this.typedValue + ", rawFormulaText=" + this.rawFormulaText + ", error=" + this.error;
177 };
178 /**
179 * Comparing two cells.
180 * @param other
181 * @returns {boolean}
182 */
183 Cell.prototype.equals = function (other) {
184 return this.toString() === other.toString();
185 };
186 /**
187 * Build a cell with an id and value.
188 * @param id - A1-notation id or key.
189 * @param value - value of the cell as a string
190 * @returns {Cell}
191 * @constructor
192 */
193 Cell.BuildFrom = function (id, value) {
194 var cell = new Cell(id);
195 cell.setValue(value);
196 return cell;
197 };
198 return Cell;
199}());
200exports.Cell = Cell;
201function toNum(chr) {
202 chr = chr.replace(/\$/g, '');
203 var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
204 for (i = 0, j = chr.length - 1; i < chr.length; i += 1, j -= 1) {
205 result += Math.pow(base.length, j) * (base.indexOf(chr[i]) + 1);
206 }
207 if (result) {
208 --result;
209 }
210 return result;
211}
212function parseKey(cell) {
213 var num = cell.match(/\d+$/), alpha = cell.replace(num, '');
214 return {
215 x: toNum(alpha),
216 y: parseInt(num[0], 10) - 1
217 };
218}