spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[DAYS] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-03 01:04:41
stats
2 file(s) changed, 51 insertions(+), 3 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index 4f0c47e..a2bb312 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -122,10 +122,22 @@ var DAY = function (...values) : number {
 6 };
 7 
 8 
 9+/**
10+ * Returns the number of days between two dates.
11+ * @param values[0] end_date most recently occurring
12+ * @param values[1] start_date not most recently occurring
13+ * @returns {number} of days between start_date and end_date
14+ * @constructor
15+ */
16+var DAYS = function (...values) : number {
17+  ArgsChecker.checkLength(values, 2);
18+  var end = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
19+  var start = TypeCaster.firstValueAsExcelDate(values[1], true); // tell firstValueAsExcelDate to coerce boolean
20+  return end.toNumber() - start.toNumber();
21+};
22 
23-var DAYS = Formula["DAYS"];
24-var DAYS360 = Formula["DAYS360"];
25 
26+var DAYS360 = Formula["DAYS360"];
27 var YEARFRAC = Formula["YEARFRAC"];
28 
29 // Functions unimplemented.
30diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
31index 330ad1c..d02ed43 100644
32--- a/tests/DateFormulasTest.ts
33+++ b/tests/DateFormulasTest.ts
34@@ -1,5 +1,5 @@
35 
36-import { DATE, DATEVALUE, EDATE, EOMONTH, DAY } from "../src/RawFormulas/RawFormulas"
37+import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS } from "../src/RawFormulas/RawFormulas"
38 import * as ERRORS from "../src/Errors"
39 import {assertEquals} from "./utils/Asserts"
40 import moment = require("moment");
41@@ -19,6 +19,42 @@ function catchAndAssertEquals(toExecute, expected) {
42   }
43 }
44 
45+
46+// Test DAYS
47+assertEquals(DAYS(DATE(1992, 6, 24), DATE(1992, 6, 25)), -1);
48+assertEquals(DAYS(DATE(1992, 6, 25), DATE(1992, 6, 24)), 1);
49+assertEquals(DAYS(DATE(1992, 6, 25), DATE(1992, 6, 23)), 2);
50+assertEquals(DAYS(DATE(1992, 6, 24), DATE(1991, 6, 24)), 366);
51+assertEquals(DAYS(DATE(1993, 6, 24), DATE(1992, 6, 24)), 365);
52+assertEquals(DAYS("2191-6-24", "1992-6-24"), 72683);
53+assertEquals(DAYS(0, 1), -1);
54+assertEquals(DAYS(false, true), -1);
55+assertEquals(DAYS(0, 100), -100);
56+assertEquals(DAYS(-100, 100), -200);
57+assertEquals(DAYS(100, -100), 200);
58+assertEquals(DAYS(100, 0), 100);
59+assertEquals(DAYS([0, "str"], [100, "str"]), -100);
60+assertEquals(DAYS("1992, 6, 25", "1992, 6, 24"), 1);
61+catchAndAssertEquals(function() {
62+  DAYS();
63+}, ERRORS.NA_ERROR);
64+catchAndAssertEquals(function() {
65+  DAYS(100);
66+}, ERRORS.NA_ERROR);
67+catchAndAssertEquals(function() {
68+  DAYS(100, 200, 300);
69+}, ERRORS.NA_ERROR);
70+catchAndAssertEquals(function() {
71+  DAYS([[], 100], 22);
72+}, ERRORS.REF_ERROR);
73+catchAndAssertEquals(function() {
74+  DAYS("str", 100);
75+}, ERRORS.VALUE_ERROR);
76+catchAndAssertEquals(function() {
77+  DAYS("false", "true");
78+}, ERRORS.VALUE_ERROR);
79+
80+
81 // Test DAY
82 assertEquals(DAY(DATE(1992, 6, 24)), 24);
83 assertEquals(DAY(DATE(1992, 5, 10)), 10);