spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[ROWS] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-09-09 16:13:26
stats
7 file(s) changed, 112 insertions(+), 5 deletions(-)
files
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Lookup.js
src/Formulas/AllFormulas.ts
src/Formulas/Lookup.ts
tests/Formulas/LookupTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/TODO.md b/TODO.md
  2index 5f6f938..932a931 100644
  3--- a/TODO.md
  4+++ b/TODO.md
  5@@ -29,7 +29,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
  6 * LOOKUP
  7 * MATCH
  8 * OFFSET
  9-* ROWS
 10 * VLOOKUP
 11 * COUNTBLANK - Requires changes to to Parser/Sheet so when we iterate through a range to return an array, we call a special function that accumulates all values, blank/null/undefined or otherwise.
 12 
 13diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 14index db5ef61..84104b2 100644
 15--- a/dist/Formulas/AllFormulas.js
 16+++ b/dist/Formulas/AllFormulas.js
 17@@ -103,6 +103,7 @@ var Lookup_1 = require("./Lookup");
 18 exports.CHOOSE = Lookup_1.CHOOSE;
 19 exports.ADDRESS = Lookup_1.ADDRESS;
 20 exports.COLUMNS = Lookup_1.COLUMNS;
 21+exports.ROWS = Lookup_1.ROWS;
 22 var Convert_1 = require("./Convert");
 23 exports.TO_DATE = Convert_1.TO_DATE;
 24 exports.TO_DOLLARS = Convert_1.TO_DOLLARS;
 25diff --git a/dist/Formulas/Lookup.js b/dist/Formulas/Lookup.js
 26index 713ef5e..b53b455 100644
 27--- a/dist/Formulas/Lookup.js
 28+++ b/dist/Formulas/Lookup.js
 29@@ -157,3 +157,24 @@ var COLUMNS = function (value) {
 30     }
 31 };
 32 exports.COLUMNS = COLUMNS;
 33+var ROWS = function (value) {
 34+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ROWS");
 35+    if (value instanceof Array) {
 36+        if (value.length === 0) {
 37+            throw new Errors_1.RefError("Reference does not exist.");
 38+        }
 39+        if (value[0] instanceof Cell_1.Cell) {
 40+            var start = value[0];
 41+            var end = value[value.length - 1];
 42+            return end.getRow() - start.getRow() + 1; // counting columns inclusively
 43+        }
 44+        else {
 45+            // if the array passed in is just values, array is considered to be a single row
 46+            return 1;
 47+        }
 48+    }
 49+    else {
 50+        return 1;
 51+    }
 52+};
 53+exports.ROWS = ROWS;
 54diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 55index fe28789..800378f 100644
 56--- a/src/Formulas/AllFormulas.ts
 57+++ b/src/Formulas/AllFormulas.ts
 58@@ -103,7 +103,8 @@ import {
 59 import {
 60   CHOOSE,
 61   ADDRESS,
 62-  COLUMNS
 63+  COLUMNS,
 64+  ROWS
 65 } from "./Lookup";
 66 import {
 67   TO_DATE,
 68@@ -509,5 +510,6 @@ export {
 69   TO_TEXT,
 70   ISFORMULA,
 71   ADDRESS,
 72-  COLUMNS
 73+  COLUMNS,
 74+  ROWS
 75 }
 76\ No newline at end of file
 77diff --git a/src/Formulas/Lookup.ts b/src/Formulas/Lookup.ts
 78index 6c188b7..33e84a2 100644
 79--- a/src/Formulas/Lookup.ts
 80+++ b/src/Formulas/Lookup.ts
 81@@ -161,8 +161,29 @@ let COLUMNS = function (value) {
 82 };
 83 
 84 
 85+let ROWS = function (value) {
 86+  ArgsChecker.checkLength(arguments, 1, "ROWS");
 87+  if (value instanceof Array) {
 88+    if (value.length === 0) {
 89+      throw new RefError("Reference does not exist.");
 90+    }
 91+    if (value[0] instanceof Cell) {
 92+      let start = value[0];
 93+      let end = value[value.length - 1];
 94+      return end.getRow() - start.getRow() + 1; // counting columns inclusively
 95+    } else {
 96+      // if the array passed in is just values, array is considered to be a single row
 97+      return 1;
 98+    }
 99+  } else {
100+    return 1;
101+  }
102+};
103+
104+
105 export {
106   CHOOSE,
107   ADDRESS,
108-  COLUMNS
109+  COLUMNS,
110+  ROWS
111 }
112\ No newline at end of file
113diff --git a/tests/Formulas/LookupTest.ts b/tests/Formulas/LookupTest.ts
114index 6b31b5f..bfb1e32 100644
115--- a/tests/Formulas/LookupTest.ts
116+++ b/tests/Formulas/LookupTest.ts
117@@ -1,7 +1,8 @@
118 import {
119   CHOOSE,
120   ADDRESS,
121-  COLUMNS
122+  COLUMNS,
123+  ROWS
124 } from "../../src/Formulas/Lookup";
125 import * as ERRORS from "../../src/Errors";
126 import {
127@@ -121,3 +122,56 @@ test("COLUMNS", function(){
128     COLUMNS([]);
129   }, ERRORS.REF_ERROR);
130 });
131+
132+
133+test("ROWS", function(){
134+  assertEquals(ROWS(1), 1);
135+  assertEquals(ROWS("str"), 1);
136+  assertEquals(ROWS(false), 1);
137+  assertEquals(ROWS(Cell.BuildFrom("A1", "str")), 1);
138+  assertEquals(ROWS([1]), 1);
139+  assertEquals(ROWS([1, 2, 3, 4]), 1);
140+  //A1:C5
141+  assertEquals(ROWS([
142+    Cell.BuildFrom("A1", "str"),
143+    Cell.BuildFrom("A2", "str"),
144+    Cell.BuildFrom("A3", "str"),
145+    Cell.BuildFrom("A4", "str"),
146+    Cell.BuildFrom("A5", "str"),
147+    Cell.BuildFrom("B1", "str"),
148+    Cell.BuildFrom("B2", "str"),
149+    Cell.BuildFrom("B3", "str"),
150+    Cell.BuildFrom("B4", "str"),
151+    Cell.BuildFrom("B5", "str"),
152+    Cell.BuildFrom("C1", "str"),
153+    Cell.BuildFrom("C2", "str"),
154+    Cell.BuildFrom("C3", "str"),
155+    Cell.BuildFrom("C4", "str"),
156+    Cell.BuildFrom("C5", "str"),
157+  ]), 5);
158+  //A5:C5
159+  assertEquals(ROWS([
160+    Cell.BuildFrom("A5", "str"),
161+    Cell.BuildFrom("B5", "str"),
162+    Cell.BuildFrom("C5", "str"),
163+  ]), 1);
164+  //A1:B2
165+  assertEquals(ROWS([
166+    Cell.BuildFrom("A1", "str"),
167+    Cell.BuildFrom("A2", "str"),
168+    Cell.BuildFrom("B1", "str"),
169+    Cell.BuildFrom("B2", "str")
170+  ]), 2);
171+  //A1:A3
172+  assertEquals(ROWS([
173+    Cell.BuildFrom("A1", "str"),
174+    Cell.BuildFrom("A2", "str"),
175+    Cell.BuildFrom("A3", "str")
176+  ]), 3);
177+  catchAndAssertEquals(function() {
178+    ROWS.apply(this, []);
179+  }, ERRORS.NA_ERROR);
180+  catchAndAssertEquals(function() {
181+    ROWS([]);
182+  }, ERRORS.REF_ERROR);
183+});
184\ No newline at end of file
185diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
186index 5ab35da..2d99ed9 100644
187--- a/tests/SheetFormulaTest.ts
188+++ b/tests/SheetFormulaTest.ts
189@@ -1039,6 +1039,13 @@ test("Sheet COLUMNS", function(){
190   assertFormulaEquals('=COLUMNS(B1:M44)', 12);
191 });
192 
193+test("Sheet ROWS", function(){
194+  assertFormulaEquals('=ROWS(1)', 1);
195+  assertFormulaEquals('=ROWS([1, 2, 3, 4])', 1);
196+  assertFormulaEquals('=ROWS(M1)', 1);
197+  assertFormulaEquals('=ROWS(B1:M44)', 44);
198+});
199+
200 test("Sheet parsing error", function(){
201   assertFormulaEqualsError('= 10e', PARSE_ERROR);
202   assertFormulaEqualsError('= SUM(', PARSE_ERROR);