commit
message
[IFERROR] work-in-progress on IFERROR
author
Ben Vogt <benjvogt@gmail.com>
date
2017-09-05 02:39:00
stats
6 file(s) changed,
17 insertions(+),
22 deletions(-)
files
TODO.md
src/Formulas/AllFormulas.ts
src/Formulas/Info.ts
src/Sheet.ts
tests/Formulas/InfoTest.ts
tests/SheetFormulaTest.ts
1diff --git a/TODO.md b/TODO.md
2index cdf5d6b..1fdf193 100644
3--- a/TODO.md
4+++ b/TODO.md
5@@ -20,9 +20,6 @@ We could give each function a type-array that matches the arguments, and could b
6 ### Fix documentation regular expression, it is butchering URLs.
7
8
9-### ISERROR, ISNA need to be called inside a try-catch-block inside the Parser or Sheet.
10-
11-
12 ### Cells/Sheet/Parser should be able to parse raw errors in the format `#N/A`
13 All errors should be able to be input/thrown in this way.
14
15diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
16index 0930dae..b67327c 100644
17--- a/src/Formulas/AllFormulas.ts
18+++ b/src/Formulas/AllFormulas.ts
19@@ -268,7 +268,8 @@ const __TRY_CATCH_FORMULAS : Object = {
20 "ERRORTYPE": ERRORTYPE,
21 "ISERR": ISERR,
22 "ISERROR": ISERROR,
23- "ISNA": ISNA
24+ "ISNA": ISNA,
25+ "IFERROR": IFERROR
26 };
27
28 export {
29diff --git a/src/Formulas/Info.ts b/src/Formulas/Info.ts
30index 747c08a..19e557d 100644
31--- a/src/Formulas/Info.ts
32+++ b/src/Formulas/Info.ts
33@@ -19,7 +19,7 @@ import {Filter} from "../Utilities/Filter";
34 * @constructor
35 */
36 let NA = function () {
37- ArgsChecker.checkLength(arguments, 1, "NA");
38+ ArgsChecker.checkLength(arguments, 0, "NA");
39 throw new NAError("NA Error thrown.");
40 };
41
42@@ -257,7 +257,7 @@ let ISERROR = function (value) {
43 if (value instanceof Cell) {
44 return value.hasError();
45 }
46- return value instanceof Error;
47+ return (value instanceof Error);
48 };
49
50
51@@ -294,14 +294,19 @@ let ISNA = function (value) {
52 * @param valueIfError - [OPTIONAL] - Value to return if no error is present in the first argument.
53 * @returns {any}
54 * @constructor
55- * TODO: This formula needs to be called from inside a try-catch-block in the Sheet/Parser, like ERROR.TYPE.
56 */
57 let IFERROR = function (value, valueIfError?) {
58 ArgsChecker.checkLengthWithin(arguments, 1, 2, "IFERROR");
59- if (value instanceof Cell && valueIfError === undefined) {
60- return ISERROR(value) ? new Cell(value.getId()) : value;
61+ if (value instanceof Cell) {
62+ if (value.hasError()) {
63+ return;
64+ }
65+ return value;
66+ }
67+ if (!ISERROR(value)) {
68+ return value;
69 }
70- return ISERROR(value) ? valueIfError : value;
71+ return;
72 };
73
74
75diff --git a/src/Sheet.ts b/src/Sheet.ts
76index e99bf4d..88935c9 100644
77--- a/src/Sheet.ts
78+++ b/src/Sheet.ts
79@@ -217,14 +217,6 @@ let Sheet = (function () {
80 return value instanceof Function;
81 },
82
83- isNull: function (value) {
84- return value === null;
85- },
86-
87- isSet: function (value) {
88- return !utils.isNull(value);
89- },
90-
91 toNum: function (chr) {
92 chr = utils.clearFormula(chr);
93 let base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', i, j, result = 0;
94diff --git a/tests/Formulas/InfoTest.ts b/tests/Formulas/InfoTest.ts
95index 9ba556d..0d9dd65 100644
96--- a/tests/Formulas/InfoTest.ts
97+++ b/tests/Formulas/InfoTest.ts
98@@ -230,7 +230,8 @@ test("ISNA", function(){
99 test("IFERROR", function(){
100 let errorCell = new Cell("A1");
101 errorCell.setError(new NAError("err"));
102- assertEquals(IFERROR(errorCell, 10), 10);
103+ assertEquals(IFERROR(errorCell, 10), undefined);
104+ assertEquals(IFERROR(new NAError("err")), undefined);
105 assertEquals(IFERROR(10), 10);
106 assertEquals(IFERROR(Cell.BuildFrom("A1", 10), "abc"), Cell.BuildFrom("A1", 10));
107 assertEquals(IFERROR(new Cell("A1")), new Cell("A1"));
108diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
109index c3cd7aa..4249f09 100644
110--- a/tests/SheetFormulaTest.ts
111+++ b/tests/SheetFormulaTest.ts
112@@ -884,6 +884,7 @@ test("Sheet ISNA", function(){
113
114 test("Sheet IFERROR", function(){
115 assertFormulaEquals('=IFERROR(10)', 10);
116+ assertFormulaEquals('=IFERROR(NA())', undefined);
117 });
118
119 test("Sheet TYPE", function(){