spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[WIP:WEEKNUM] formula added and *partially* tested
author
Ben Vogt <[email protected]>
date
2017-04-11 01:48:50
stats
3 file(s) changed, 85 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 ed320a7..528f509 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -257,6 +257,22 @@ var WEEKDAY = function (...values) {
  6 };
  7 
  8 
  9+var WEEKNUM = function (...values) {
 10+  ArgsChecker.checkLength(values, 1);
 11+  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
 12+  if (date.toNumber() < 0) {
 13+    throw new NumError("Function YEAR parameter 1 value is " + date.toNumber() + ". It should be greater than or equal to 0.");
 14+  }
 15+  var dm = date.toMoment();
 16+  var week = dm.week();
 17+  // If this weekYear is not the same as the year, then we're technically in "week-53"
 18+  // See https://momentjs.com/docs/#/get-set/week-year/ for more info.
 19+  if (dm.weekYear() !== dm.year()) {
 20+    return 53;
 21+  }
 22+  return week;
 23+};
 24+
 25 
 26 var YEARFRAC = Formula["YEARFRAC"];
 27 // Functions unimplemented.
 28@@ -273,7 +289,6 @@ var SECOND;
 29 var TIME;
 30 var TIMEVALUE;
 31 var TODAY;
 32-var WEEKNUM;
 33 var WORKDAY;
 34 
 35 export {
 36@@ -287,5 +302,6 @@ export {
 37   MONTH,
 38   YEAR,
 39   WEEKDAY,
 40+  WEEKNUM,
 41   YEARFRAC
 42 }
 43\ No newline at end of file
 44diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 45index 0700751..f884677 100644
 46--- a/src/RawFormulas/RawFormulas.ts
 47+++ b/src/RawFormulas/RawFormulas.ts
 48@@ -119,6 +119,7 @@ import {
 49   MONTH,
 50   YEAR,
 51   WEEKDAY,
 52+  WEEKNUM,
 53   YEARFRAC
 54 } from "./Date"
 55 
 56@@ -242,5 +243,6 @@ export {
 57   RADIANS,
 58   MONTH,
 59   YEAR,
 60-  WEEKDAY
 61+  WEEKDAY,
 62+  WEEKNUM
 63 }
 64\ No newline at end of file
 65diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 66index 44c0b29..4e59172 100644
 67--- a/tests/DateFormulasTest.ts
 68+++ b/tests/DateFormulasTest.ts
 69@@ -1,5 +1,17 @@
 70 
 71-import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH, YEAR, WEEKDAY } from "../src/RawFormulas/RawFormulas"
 72+import {
 73+  DATE,
 74+  DATEVALUE,
 75+  EDATE,
 76+  EOMONTH,
 77+  DAY,
 78+  DAYS,
 79+  DAYS360,
 80+  MONTH,
 81+  YEAR,
 82+  WEEKDAY,
 83+  WEEKNUM
 84+} from "../src/RawFormulas/RawFormulas"
 85 import * as ERRORS from "../src/Errors"
 86 import {assertEquals} from "./utils/Asserts"
 87 import moment = require("moment");
 88@@ -20,6 +32,55 @@ function catchAndAssertEquals(toExecute, expected) {
 89 }
 90 
 91 
 92+// Test WEEKNUM
 93+assertEquals(WEEKNUM(DATE(1992, 6, 19)), 25);
 94+assertEquals(WEEKNUM(DATE(1992, 6, 20)), 25);
 95+assertEquals(WEEKNUM(DATE(1992, 6, 21)), 26);
 96+assertEquals(WEEKNUM(0), 52);
 97+assertEquals(WEEKNUM(false), 52);
 98+assertEquals(WEEKNUM(1), 53);
 99+assertEquals(WEEKNUM(true), 53);
100+assertEquals(WEEKNUM(2), 1);
101+assertEquals(WEEKNUM(3), 1);
102+assertEquals(WEEKNUM(4), 1);
103+assertEquals(WEEKNUM(5), 1);
104+assertEquals(WEEKNUM(6), 1);
105+assertEquals(WEEKNUM(7), 1);
106+assertEquals(WEEKNUM(8), 2);
107+assertEquals(WEEKNUM(9), 2);
108+assertEquals(WEEKNUM(10), 2);
109+assertEquals(WEEKNUM(11), 2);
110+assertEquals(WEEKNUM(12), 2);
111+assertEquals(WEEKNUM(13), 2);
112+assertEquals(WEEKNUM(14), 2);
113+assertEquals(WEEKNUM(15), 3);
114+assertEquals(WEEKNUM(16), 3);
115+assertEquals(WEEKNUM(17), 3);
116+assertEquals(WEEKNUM(18), 3);
117+assertEquals(WEEKNUM(23734), 52);
118+assertEquals(WEEKNUM(23735), 52);
119+assertEquals(WEEKNUM(23736), 52);
120+assertEquals(WEEKNUM(23737), 52);
121+assertEquals(WEEKNUM(23738), 53);
122+assertEquals(WEEKNUM(23739), 53);
123+assertEquals(WEEKNUM(23740), 53);
124+assertEquals(WEEKNUM(23741), 53);
125+assertEquals(WEEKNUM(23742), 53);
126+assertEquals(WEEKNUM(23743), 1);
127+assertEquals(WEEKNUM(23744), 1);
128+assertEquals(WEEKNUM(23745), 2);
129+assertEquals(WEEKNUM(23746), 2);
130+assertEquals(WEEKNUM(23747), 2);
131+assertEquals(WEEKNUM(23748), 2);
132+assertEquals(WEEKNUM(23749), 2);
133+assertEquals(WEEKNUM(23750), 2);
134+assertEquals(WEEKNUM(23751), 2);
135+assertEquals(WEEKNUM(23752), 3);
136+
137+
138+
139+
140+
141 // Test WEEKDAY
142 assertEquals(WEEKDAY(DATE(1992, 6, 20)), 7);
143 assertEquals(WEEKDAY(DATE(1992, 6, 21)), 1);