commit
message
Documenting Cell.ts
author
Ben Vogt <[email protected]>
date
2017-01-01 20:51:24
stats
2 file(s) changed,
84 insertions(+),
5 deletions(-)
files
README.md
src/Cell.ts
1diff --git a/README.md b/README.md
2index 5d9c4db..1e6600d 100644
3--- a/README.md
4+++ b/README.md
5@@ -4,8 +4,9 @@ TypeScript implementation of a spreadsheet.
6 ## TODO
7 Things I should do.
8
9-### Write documentation for Cell
10-
11 ### Write documentation for Sheet
12
13-### Write documentation for Matrix
14\ No newline at end of file
15+### Write documentation for Matrix
16+
17+### Cell.error should not be of type any
18+Errors should have specific types so we can have type safety when passing them.
19\ No newline at end of file
20diff --git a/src/Cell.ts b/src/Cell.ts
21index e3986f4..68d0c88 100644
22--- a/src/Cell.ts
23+++ b/src/Cell.ts
24@@ -1,3 +1,7 @@
25+/**
26+ * Cell represents a cell in the spreadsheet. It contains a nullable formula, and a value, which is not nullable unless
27+ * the parsing of the formula results in an error.
28+ */
29 class Cell {
30 private formula: string;
31 private value: string;
32@@ -6,6 +10,11 @@ class Cell {
33 private id: string;
34 private row: number;
35 private col: number;
36+
37+ /**
38+ * Creates an empty cell with an id.
39+ * @param id key of the cell in A1-format.
40+ */
41 constructor(id: string) {
42 var key = parseKey(id);
43
44@@ -17,6 +26,11 @@ class Cell {
45 this.row = key.y;
46 this.col = key.x;
47 }
48+
49+ /**
50+ * Update this cell's dependencies, where `dependencies` is a unique list of A1-format cell IDs.
51+ * @param dependencies to merge with existing dependencies.
52+ */
53 updateDependencies(dependencies: Array<string>) {
54 for (var index in dependencies) {
55 if (this.dependencies.indexOf(dependencies[index]) === -1) {
56@@ -24,42 +38,105 @@ class Cell {
57 }
58 }
59 }
60+
61+ /**
62+ * Return a list of dependencies in A1-format cell IDs, in no particular order, but likely in order of occurrence in
63+ * formula.
64+ * @returns {Array<string>} list of dependencies in A1-format
65+ */
66 getDependencies() : Array<string> {
67 return this.dependencies;
68 }
69+
70+ /**
71+ * Return the zero-indexed column number of this cell.
72+ * @returns {number} column
73+ */
74 getColumn() : number {
75 return this.col;
76 }
77+
78+ /**
79+ * Return the zero-indexed row number of this cell.
80+ * @returns {number} row
81+ */
82 getRow() : number {
83 return this.row;
84 }
85+
86+ /**
87+ * Get the A1-format ID of this cell.
88+ * @returns {string} cell ID
89+ */
90 getId() : string {
91 return this.id;
92 }
93+
94+ /**
95+ * Set the formula of this cell.
96+ * @param formula to set.
97+ */
98 setFormula(formula: string) {
99 this.formula = formula;
100 }
101+
102+ /**
103+ * Get the formula of this cell if set. Defaults to null, so could return null.
104+ * @returns {string} formula of this cell, if set. Nullable.
105+ */
106 getFormula() : string {
107 return this.formula;
108 }
109+
110+ /**
111+ * Set the value of this cell. If this cell has a primitive value (does not contain a formula), it could be set to a
112+ * value while the formula field is still null.
113+ * @param value to set
114+ */
115 setValue(value: string) {
116 this.value = value;
117 }
118+
119+ /**
120+ * Get the value of this cell. Since value could be null do to an error in the formula, this could return null.
121+ * @returns {string}
122+ */
123 getValue() : string {
124 return this.value;
125 }
126+
127+ /**
128+ * Set error for this cell. Usually in the case of a parse error when parsing the formula.
129+ * @param error to set.
130+ */
131 setError(error: any) {
132 this.error = error;
133 }
134+
135+ /**
136+ * Get the error for this cell. If the formula is parsed properly, or is null, this could be null.
137+ * @returns {any} error to return, could be Null.
138+ */
139 getError() : any {
140 return this.error;
141 }
142+
143+ /**
144+ * Get the renderable value for this cell, meaning the value that is the most meaningful to the user. If the error is
145+ * null, we return the error. If we don't have an error, return the value.
146+ * @returns {string} renderable string value to return.
147+ */
148 getRenderedValue() : string {
149 if (this.error !== null) {
150- return this.value;
151+ return this.value.toString();
152 }
153- return this.error.toString()
154+ return this.error.toString();
155 }
156+
157+ /**
158+ * Returns the human-readable string representation of this cell, omitting some obvious fields.
159+ * @returns {string} to return
160+ */
161 toString() : string {
162 return "id=" + this.id + ", value=" + this.value + ", formula=" + this.formula + ", error=" + this.error;
163 }