commit
message
Added Formula.IF
author
Ben Vogt <[email protected]>
date
2017-02-15 03:37:13
stats
3 file(s) changed,
38 insertions(+),
3 deletions(-)
files
src/RawFormulas/RawFormulas.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
2index 45fcb40..f200c45 100644
3--- a/src/RawFormulas/RawFormulas.ts
4+++ b/src/RawFormulas/RawFormulas.ts
5@@ -121,7 +121,6 @@ var __COMPLEX = {
6 };
7 var FISHER = Formula["FISHER"];
8 var FISHERINV = Formula["FISHERINV"];
9-var IF = Formula["IF"];
10 var SPLIT = Formula["SPLIT"];
11 var SQRTPI = Formula["SQRTPI"];
12 var SUMPRODUCT = Formula["SUMPRODUCT"];
13@@ -131,6 +130,27 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
14 var TRUNC = Formula["TRUNC"];
15 var YEARFRAC = Formula["YEARFRAC"];
16
17+/**
18+ * Returns one value if a logical expression is TRUE and another if it is FALSE.
19+ * @param values[0] An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE.
20+ * @param values[1] The value the function returns if logical_expression is TRUE
21+ * @param values[2] The value the function returns if logical_expression is FALSE.
22+ * @returns one value if a logical expression is TRUE and another if it is FALSE.
23+ * @constructor
24+ */
25+var IF = function (...values) : any {
26+ checkArgumentsLength(values, 3);
27+ if (values[0] instanceof Array) {
28+ if (values[0].length === 0) {
29+ throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
30+ }
31+ return IF(values[0][0], values[1], values[2]);
32+ } else if (values[0] === "") {
33+ return values[2];
34+ }
35+ return (valueToBoolean(values[0])) ? values[1] : values[2];
36+};
37+
38 /**
39 * Returns the a count of the number of numeric values in a dataset.
40 * @param values The values or ranges to consider when counting.
41diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
42index 9ca22da..d5d0145 100644
43--- a/src/RawFormulas/Utils.ts
44+++ b/src/RawFormulas/Utils.ts
45@@ -175,7 +175,7 @@ function valueToBoolean(value: any) : boolean {
46 if (typeof value === "number") {
47 return value !== 0;
48 } else if (typeof value === "string") {
49- throw new CellError(ERRORS.VALUE_ERROR, "AND expects boolean values. But '" + value + "' is a text and cannot be coerced to a boolean.")
50+ throw new CellError(ERRORS.VALUE_ERROR, "___ expects boolean values. But '" + value + "' is a text and cannot be coerced to a boolean.")
51 } else if (typeof value === "boolean") {
52 return value;
53 }
54diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
55index c0cffcc..d4593cf 100644
56--- a/tests/FormulasTest.ts
57+++ b/tests/FormulasTest.ts
58@@ -658,7 +658,22 @@ assertEquals(FISHER(0.962), 1.972066740199461);
59
60 assertEquals(FISHERINV(0.962), 0.7451676440945232);
61
62-assertEquals(IF("m" == "m", "hit", "miss"), 'hit');
63+
64+// Test IF
65+assertEquals(IF(true, "hit", "miss"), "hit");
66+assertEquals(IF(false, "hit", "miss"), "miss");
67+assertEquals(IF("", "hit", "miss"), "miss");
68+assertEquals(IF("", "hit", "miss"), "miss");
69+assertEquals(IF([true], "hit", "miss"), "hit");
70+assertEquals(IF([false], "hit", "miss"), "miss");
71+assertEquals(IF([""], "hit", "miss"), "miss");
72+assertEquals(IF([""], "hit", "miss"), "miss");
73+catchAndAssertEquals(function() {
74+ IF("str", 1, 2);
75+}, ERRORS.VALUE_ERROR);
76+catchAndAssertEquals(function() {
77+ IF([], 1, 2);
78+}, ERRORS.REF_ERROR);
79
80
81 // Test INT