commit
message
[COLUMN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-10 23:54:25
stats
10 file(s) changed,
73 insertions(+),
23 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Info.js
dist/Sheet.js
src/Formulas/AllFormulas.ts
src/Formulas/Info.ts
src/Sheet.ts
tests/Formulas/InfoTest.ts
tests/SheetFormulaTest.ts
1diff --git a/DOCS.md b/DOCS.md
2index 40f2d0f..1b2844b 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -684,6 +684,14 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### COLUMN
11+
12+```
13+ Returns the column number of a specified cell, starting with column 1 for A.
14+@param cell - Cell, defaults to the cell calling this formula, when used in the context of a spreadsheet.
15+@constructor
16+```
17 ## Logical
18
19
20diff --git a/TODO.md b/TODO.md
21index a1323f1..884fa24 100644
22--- a/TODO.md
23+++ b/TODO.md
24@@ -41,7 +41,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
25 * ISFORMULA - Requires changes to Parser/Sheet to fetch a cell, and check the formula field to see if it contains a formula.
26 * CELL - Requires changes to Parser/Sheet so that the raw cell is returned to the function. The raw cell should contain all information necessary for returning specified info.
27 * ADDRESS - In order to implement this, cells need to be aware of their sheet.
28-* COLUMN
29 * COLUMNS
30 * HLOOKUP
31 * INDEX
32diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
33index c172082..c1165bc 100644
34--- a/dist/Formulas/AllFormulas.js
35+++ b/dist/Formulas/AllFormulas.js
36@@ -96,6 +96,7 @@ exports.ISERROR = Info_1.ISERROR;
37 exports.ISNA = Info_1.ISNA;
38 exports.IFERROR = Info_1.IFERROR;
39 exports.TYPE = Info_1.TYPE;
40+exports.COLUMN = Info_1.COLUMN;
41 var Lookup_1 = require("./Lookup");
42 exports.CHOOSE = Lookup_1.CHOOSE;
43 var Logical_1 = require("./Logical");
44diff --git a/dist/Formulas/Info.js b/dist/Formulas/Info.js
45index c3120fd..682b230 100644
46--- a/dist/Formulas/Info.js
47+++ b/dist/Formulas/Info.js
48@@ -320,3 +320,16 @@ var TYPE = function (value) {
49 return 128;
50 };
51 exports.TYPE = TYPE;
52+/**
53+ * Returns the column number of a specified cell, starting with column 1 for A.
54+ * @param cell - Cell, defaults to the cell calling this formula, when used in the context of a spreadsheet.
55+ * @constructor
56+ */
57+var COLUMN = function (cell) {
58+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "COLUMN");
59+ if (!(cell instanceof Cell_1.Cell)) {
60+ throw new Errors_1.NAError("Argument must be a range or reference.");
61+ }
62+ return cell.getColumn() + 1;
63+};
64+exports.COLUMN = COLUMN;
65diff --git a/dist/Sheet.js b/dist/Sheet.js
66index 34e0231..3c27e22 100644
67--- a/dist/Sheet.js
68+++ b/dist/Sheet.js
69@@ -399,14 +399,7 @@ var Sheet = (function () {
70 if (!cell.isBlank() && cell.getError()) {
71 throw cell.getError();
72 }
73- if (cell.isBlank()) {
74- return cell;
75- }
76- // return value if is set
77- if (utils.isSet(value)) {
78- var result = instance.helper.number(value);
79- return !isNaN(result) ? result : value;
80- }
81+ return cell;
82 },
83 cellRangeValue: function (start, end) {
84 var coordsStart = utils.cellCoords(start), coordsEnd = utils.cellCoords(end), origin = this;
85diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
86index c1a7e50..266c9a4 100644
87--- a/src/Formulas/AllFormulas.ts
88+++ b/src/Formulas/AllFormulas.ts
89@@ -95,7 +95,8 @@ import {
90 ISERROR,
91 ISNA,
92 IFERROR,
93- TYPE
94+ TYPE,
95+ COLUMN
96 } from "./Info";
97 import {
98 CHOOSE
99@@ -441,5 +442,6 @@ export {
100 ISERROR,
101 ISNA,
102 IFERROR,
103- TYPE
104+ TYPE,
105+ COLUMN
106 }
107\ No newline at end of file
108diff --git a/src/Formulas/Info.ts b/src/Formulas/Info.ts
109index 8984e43..58d0236 100644
110--- a/src/Formulas/Info.ts
111+++ b/src/Formulas/Info.ts
112@@ -339,6 +339,20 @@ var TYPE = function (value) {
113 };
114
115
116+/**
117+ * Returns the column number of a specified cell, starting with column 1 for A.
118+ * @param cell - Cell, defaults to the cell calling this formula, when used in the context of a spreadsheet.
119+ * @constructor
120+ */
121+var COLUMN = function (cell) {
122+ ArgsChecker.checkLength(arguments, 1, "COLUMN");
123+ if (!(cell instanceof Cell)) {
124+ throw new NAError("Argument must be a range or reference.");
125+ }
126+ return cell.getColumn() + 1;
127+};
128+
129+
130 export {
131 NA,
132 ISTEXT,
133@@ -355,5 +369,6 @@ export {
134 ISERROR,
135 ISNA,
136 IFERROR,
137- TYPE
138+ TYPE,
139+ COLUMN
140 }
141\ No newline at end of file
142diff --git a/src/Sheet.ts b/src/Sheet.ts
143index 73768a3..20ab06d 100644
144--- a/src/Sheet.ts
145+++ b/src/Sheet.ts
146@@ -475,21 +475,11 @@ var Sheet = (function () {
147 throw new RefError("Reference does not exist.");
148 }
149 }
150-
151 // check if any error occurs
152 if (!cell.isBlank() && cell.getError()) {
153 throw cell.getError()
154 }
155-
156- if (cell.isBlank()) {
157- return cell;
158- }
159-
160- // return value if is set
161- if (utils.isSet(value)) {
162- var result = instance.helper.number(value);
163- return !isNaN(result) ? result : value;
164- }
165+ return cell;
166 },
167
168 cellRangeValue: function (start: string, end: string) {
169diff --git a/tests/Formulas/InfoTest.ts b/tests/Formulas/InfoTest.ts
170index b0ffd2d..038912a 100644
171--- a/tests/Formulas/InfoTest.ts
172+++ b/tests/Formulas/InfoTest.ts
173@@ -14,7 +14,8 @@ import {
174 ISERROR,
175 ISNA,
176 IFERROR,
177- TYPE
178+ TYPE,
179+ COLUMN
180 } from "../../src/Formulas/Info";
181 import * as ERRORS from "../../src/Errors";
182 import {
183@@ -255,3 +256,16 @@ test("TYPE", function(){
184 TYPE.apply(this, [])
185 }, ERRORS.NA_ERROR);
186 });
187+
188+
189+test("COLUMN", function(){
190+ assertEquals(COLUMN(new Cell("A1")), 1);
191+ assertEquals(COLUMN(new Cell("B1")), 2);
192+ assertEquals(COLUMN(new Cell("C1")), 3);
193+ catchAndAssertEquals(function() {
194+ COLUMN(10)
195+ }, ERRORS.NA_ERROR);
196+ catchAndAssertEquals(function() {
197+ COLUMN.apply(this, [])
198+ }, ERRORS.NA_ERROR);
199+});
200diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
201index bca685a..074a5d3 100644
202--- a/tests/SheetFormulaTest.ts
203+++ b/tests/SheetFormulaTest.ts
204@@ -27,6 +27,15 @@ function assertFormulaEquals(formula: string, expectation: any) {
205 assertEquals(cell.getValue(), expectation);
206 }
207
208+function assertFormulaEqualsDependsOnReference(refId: string, value: any, formula: string, expectation: any) {
209+ var sheet = new Sheet();
210+ sheet.setCell(refId, value);
211+ sheet.setCell("A1", formula);
212+ var cell = sheet.getCell("A1");
213+ assertEquals(cell.getError(), null);
214+ assertEquals(cell.getValue(), expectation);
215+}
216+
217 function assertFormulaResultsInType(formula: string, type: string) {
218 var sheet = new Sheet();
219 sheet.setCell("A1", formula);
220@@ -860,6 +869,10 @@ test("Sheet TYPE", function(){
221 assertFormulaEquals('=TYPE(10)', 1);
222 });
223
224+test("Sheet COLUMN", function(){
225+ assertFormulaEqualsDependsOnReference('D1', 10, '=COLUMN(D1)', 4);
226+});
227+
228 test("Sheet *", function(){
229 assertFormulaEquals('= 10 * 10', 100);
230 assertFormulaEquals('= 10 * 0', 0);