spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
riteria evaluations should accept '<>'
author
Ben Vogt <[email protected]>
date
2017-04-01 22:10:47
stats
3 file(s) changed, 8 insertions(+), 4 deletions(-)
files
README.md
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
 1diff --git a/README.md b/README.md
 2index 16fcac7..ee9c1b3 100644
 3--- a/README.md
 4+++ b/README.md
 5@@ -13,8 +13,6 @@ inside parser.js? If we ever want to impliment custom formulas, or even accept d
 6 against this. Or else someone could load a CSV with javascript and when our spreadsheet opens it, then suddenly
 7 arbitrary javascript is executed in the client machine.
 8 
 9-### Criteria evaluations should accept "<>"
10-
11 ### Criteria evaluations should escape reg-ex characters: http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
12 
13 ### Criteria evaluations should accept dollar-to-number comparisons: `=COUNTIF({10, 20, 40}, ">=$20")`
14diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
15index 0f4ff31..f3386bf 100644
16--- a/src/RawFormulas/Utils.ts
17+++ b/src/RawFormulas/Utils.ts
18@@ -46,7 +46,7 @@ class CriteriaFunctionFactory {
19       };
20     } else if (typeof criteria === "string") {
21       // https://regex101.com/r/c2hxAZ/6
22-      var comparisonMatches = criteria.match(/(^<=|^>=|^=|^>|^<)\s*(-?[0-9]+([,.][0-9]+)?)\s*$/);
23+      var comparisonMatches = criteria.match(/(^<=|^>=|^=|^<>|^>|^<)\s*(-?[0-9]+([,.][0-9]+)?)\s*$/);
24       if (comparisonMatches !== null && comparisonMatches.length >= 4 && comparisonMatches[2] !== undefined) {
25         criteriaEvaluation = function (x) : boolean {
26           return eval(x + criteria);
27@@ -56,6 +56,11 @@ class CriteriaFunctionFactory {
28             return eval(x + "===" + comparisonMatches[2]);
29           };
30         }
31+        if (comparisonMatches[1] === "<>") {
32+          criteriaEvaluation = function (x) : boolean {
33+            return eval(x + "!==" + comparisonMatches[2]);
34+          };
35+        }
36       } else if (criteria.match(/\*|\~\*|\?|\~\?/) !== null) {
37         // Regular string
38         var matches = criteria.match(/\*|\~\*|\?|\~\?/);
39diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
40index bd1cfdf..8948902 100644
41--- a/tests/FormulasTest.ts
42+++ b/tests/FormulasTest.ts
43@@ -270,6 +270,7 @@ assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], "=10"), 10);
44 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], "=     10  "), 10);
45 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], ">0"), 5.166666666666667);
46 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], ">=5"), 6);
47+assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], "<>1"), 6);
48 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], "<10"), 4.2);
49 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5, 44], "<=10"), 5.166666666666667);
50 assertEquals(AVERAGEIF([1, 5, 5, 5, 10, 5], ">4.99"), 6);