spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[WEEKDAY] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-08 00:55:28
stats
3 file(s) changed, 137 insertions(+), 3 deletions(-)
files
src/RawFormulas/Date.ts
src/RawFormulas/RawFormulas.ts
tests/DateFormulasTest.ts
  1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
  2index c3d3101..ed320a7 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -213,12 +213,50 @@ var YEAR = function (...values) {
  6   ArgsChecker.checkLength(values, 1);
  7   var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
  8   if (date.toNumber() < 0) {
  9-    throw new NumError("Function MONTH parameter 1 value is " + date.toNumber() + ". It should be greater than or equal to 0.");
 10+    throw new NumError("Function YEAR parameter 1 value is " + date.toNumber() + ". It should be greater than or equal to 0.");
 11   }
 12   return date.toMoment().year();
 13 };
 14 
 15 
 16+/**
 17+ * Returns a number representing the day of the week of the date provided.
 18+ * @param values[0] date - The date for which to determine the day of the week. Must be a reference to a cell containing
 19+ * a date, a function returning a date type, or a number.
 20+ * @param values[1] type - [ OPTIONAL - 1 by default ] - A number indicating which numbering system to use to represent
 21+ * weekdays. By default counts starting with Sunday = 1. If type is 1, days are counted from Sunday and the value of
 22+ * Sunday is 1, therefore the value of Saturday is 7. If type is 2, days are counted from Monday and the value of Monday
 23+ * is 1, therefore the value of Sunday is 7. If type is 3, days are counted from Monday and the value of Monday is 0,
 24+ * therefore the value of Sunday is 6.
 25+ * @returns {number} day of week
 26+ * @constructor
 27+ */
 28+var WEEKDAY = function (...values) {
 29+  ArgsChecker.checkLengthWithin(values, 1, 2);
 30+  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
 31+  var offsetType = values.length === 2 ? TypeCaster.firstValueAsNumber(values[1]) : 1;
 32+  if (date.toNumber() < 0) {
 33+    throw new NumError("Function WEEKDAY parameter 1 value is " + date.toNumber() + ". It should be greater than or equal to 0.");
 34+  }
 35+  var day = date.toMoment().day();
 36+  if (offsetType === 1) {
 37+    return day + 1;
 38+  } else if (offsetType === 2) {
 39+    if (day === 0) {
 40+      return 7;
 41+    }
 42+    return day;
 43+  } else if (offsetType === 3) {
 44+    if (day === 0) {
 45+      return 6;
 46+    }
 47+    return day - 1;
 48+  } else {
 49+    throw new NumError("Function WEEKDAY parameter 2 value " + day + " is out of range.");
 50+  }
 51+};
 52+
 53+
 54 
 55 var YEARFRAC = Formula["YEARFRAC"];
 56 // Functions unimplemented.
 57@@ -235,7 +273,6 @@ var SECOND;
 58 var TIME;
 59 var TIMEVALUE;
 60 var TODAY;
 61-var WEEKDAY;
 62 var WEEKNUM;
 63 var WORKDAY;
 64 
 65@@ -249,5 +286,6 @@ export {
 66   EOMONTH,
 67   MONTH,
 68   YEAR,
 69+  WEEKDAY,
 70   YEARFRAC
 71 }
 72\ No newline at end of file
 73diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 74index a95babc..0700751 100644
 75--- a/src/RawFormulas/RawFormulas.ts
 76+++ b/src/RawFormulas/RawFormulas.ts
 77@@ -118,6 +118,7 @@ import {
 78   EOMONTH,
 79   MONTH,
 80   YEAR,
 81+  WEEKDAY,
 82   YEARFRAC
 83 } from "./Date"
 84 
 85@@ -240,5 +241,6 @@ export {
 86   YEARFRAC,
 87   RADIANS,
 88   MONTH,
 89-  YEAR
 90+  YEAR,
 91+  WEEKDAY
 92 }
 93\ No newline at end of file
 94diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 95index a454542..44c0b29 100644
 96--- a/tests/DateFormulasTest.ts
 97+++ b/tests/DateFormulasTest.ts
 98@@ -1,5 +1,5 @@
 99 
100-import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH, YEAR } from "../src/RawFormulas/RawFormulas"
101+import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH, YEAR, WEEKDAY } from "../src/RawFormulas/RawFormulas"
102 import * as ERRORS from "../src/Errors"
103 import {assertEquals} from "./utils/Asserts"
104 import moment = require("moment");
105@@ -20,6 +20,68 @@ function catchAndAssertEquals(toExecute, expected) {
106 }
107 
108 
109+// Test WEEKDAY
110+assertEquals(WEEKDAY(DATE(1992, 6, 20)), 7);
111+assertEquals(WEEKDAY(DATE(1992, 6, 21)), 1);
112+assertEquals(WEEKDAY(DATE(1992, 6, 24)), 4);
113+assertEquals(WEEKDAY(DATE(1992, 6, 25)), 5);
114+assertEquals(WEEKDAY(1312211), 5);
115+assertEquals(WEEKDAY(1312212), 6);
116+assertEquals(WEEKDAY(1312213), 7);
117+assertEquals(WEEKDAY(0), 7);
118+assertEquals(WEEKDAY(false), 7);
119+assertEquals(WEEKDAY(1), 1);
120+assertEquals(WEEKDAY(true), 1);
121+assertEquals(WEEKDAY(40909, 1), 1);
122+assertEquals(WEEKDAY(40909, 2), 7);
123+assertEquals(WEEKDAY(40909, 3), 6);
124+assertEquals(WEEKDAY(411, 1), 5);
125+assertEquals(WEEKDAY(411, 2), 4);
126+assertEquals(WEEKDAY(411, 3), 3);
127+assertEquals(WEEKDAY(40909, 1), 1);
128+assertEquals(WEEKDAY(40910, 1), 2);
129+assertEquals(WEEKDAY(40911, 1), 3);
130+assertEquals(WEEKDAY(40912, 1), 4);
131+assertEquals(WEEKDAY(40913, 1), 5);
132+assertEquals(WEEKDAY(40914, 1), 6);
133+assertEquals(WEEKDAY(40915, 1), 7);
134+assertEquals(WEEKDAY(40916, 1), 1);
135+assertEquals(WEEKDAY(40909, 2), 7);
136+assertEquals(WEEKDAY(40910, 2), 1);
137+assertEquals(WEEKDAY(40911, 2), 2);
138+assertEquals(WEEKDAY(40912, 2), 3);
139+assertEquals(WEEKDAY(40913, 2), 4);
140+assertEquals(WEEKDAY(40914, 2), 5);
141+assertEquals(WEEKDAY(40915, 2), 6);
142+assertEquals(WEEKDAY(40916, 2), 7);
143+assertEquals(WEEKDAY(40909, 3), 6);
144+assertEquals(WEEKDAY(40910, 3), 0);
145+assertEquals(WEEKDAY(40911, 3), 1);
146+assertEquals(WEEKDAY(40912, 3), 2);
147+assertEquals(WEEKDAY(40913, 3), 3);
148+assertEquals(WEEKDAY(40914, 3), 4);
149+assertEquals(WEEKDAY(40915, 3), 5);
150+assertEquals(WEEKDAY(40916, 3), 6);
151+catchAndAssertEquals(function() {
152+  WEEKDAY();
153+}, ERRORS.NA_ERROR);
154+catchAndAssertEquals(function() {
155+  WEEKDAY(213123, 1, 1);
156+}, ERRORS.NA_ERROR);
157+catchAndAssertEquals(function() {
158+  WEEKDAY("str");
159+}, ERRORS.VALUE_ERROR);
160+catchAndAssertEquals(function() {
161+  WEEKDAY([]);
162+}, ERRORS.REF_ERROR);
163+catchAndAssertEquals(function() {
164+  WEEKDAY(-10);
165+}, ERRORS.NUM_ERROR);
166+catchAndAssertEquals(function() {
167+  WEEKDAY(10, 4);
168+}, ERRORS.NUM_ERROR);
169+
170+
171 // Test YEAR
172 assertEquals(YEAR(DATE(1992, 6, 24)), 1992);
173 assertEquals(YEAR(DATE(2000, 6, 24)), 2000);
174@@ -44,6 +106,9 @@ catchAndAssertEquals(function() {
175 catchAndAssertEquals(function() {
176   YEAR([]);
177 }, ERRORS.REF_ERROR);
178+catchAndAssertEquals(function() {
179+  YEAR(-10);
180+}, ERRORS.NUM_ERROR);
181 
182 
183 // Test MONTH
184@@ -62,6 +127,9 @@ catchAndAssertEquals(function() {
185 catchAndAssertEquals(function() {
186   MONTH([]);
187 }, ERRORS.REF_ERROR);
188+catchAndAssertEquals(function() {
189+  MONTH(-10);
190+}, ERRORS.NUM_ERROR);
191 
192 
193 // Test DAYS360