spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
parsing 'Month YYYY' values for DATEVALUE
author
Ben Vogt <[email protected]>
date
2017-03-25 16:21:11
stats
3 file(s) changed, 50 insertions(+), 2 deletions(-)
files
README.md
src/RawFormulas/Date.ts
tests/FormulasTest.ts
 1diff --git a/README.md b/README.md
 2index f9dff09..929acf4 100644
 3--- a/README.md
 4+++ b/README.md
 5@@ -64,4 +64,7 @@ Right now we're just using the number of days since 1900, but we should check th
 6 
 7 #### Verify that all white-space wild cards are implemented properly
 8 
 9-#### Verify that all N-times ({2,9}) are correct, and we're not parsing numbers too big.
10\ No newline at end of file
11+#### Verify that all N-times ({2,9}) are correct, and we're not parsing numbers too big.
12+
13+#### Many times I use `\s*` when I actaully mean `\s+`
14+Or the are times when I mean "Feb 20 2019" or "Feb 20,2019", and either is correct.
15\ No newline at end of file
16diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
17index 9d09a7c..69bfedc 100644
18--- a/src/RawFormulas/Date.ts
19+++ b/src/RawFormulas/Date.ts
20@@ -58,6 +58,7 @@ var DATE = function (...values) {
21  * "Sun 09 Feb 2017"        DONE
22  * "Feb-2017"
23  * "22-Feb"
24+ * "Feb 22"
25  * "10-22"
26  * "10/2022"
27  * "Sun Mar 05 2017 10:40:26"
28@@ -277,7 +278,7 @@ var DATEVALUE = function (...values) : number {
29   // Check (Dayname) Month DD YYYY
30   if (m === undefined) {
31     // For reference: https://regex101.com/r/xPcm7v/11
32-    var matches = dateString.match(/^\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?,?\s*(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec),?\s*(0?[0-9]|1[0-9]|2[0-9]|3[0-1]),?\s*([0-9]{4}|[1-9][0-9]{2}|[0-9]{2})\s*$/i);
33+    var matches = dateString.match(/^\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday|sun|mon|tues|wed|thur|fri|sat)?,?\s*(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec),?\s*(0?[0-9]|1[0-9]|2[0-9]|3[0-1]),?\s+([0-9]{4}|[1-9][0-9]{2}|[0-9]{2})\s*$/i);
34     if (matches && matches.length === 5) {
35       var years = parseInt(matches[4]);
36       var monthName = matches[2];
37@@ -318,7 +319,23 @@ var DATEVALUE = function (...values) : number {
38       }
39       m = tmpMoment.add({"days": days});
40     }
41+  }
42 
43+  // Check Month YYYY
44+  if (m === undefined) {
45+    // For reference: https://regex101.com/r/eNyVAL/3
46+    var matches = dateString.match(/^\s*(january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sep|oct|nov|dec)(,?\s*|\s*-?\.?-?\/?\s*)([0-9]{4})\s*$/i);
47+    if (matches && matches.length === 4) {
48+      var years = parseInt(matches[3]);
49+      var monthName = matches[1];
50+      var actualYear = years;
51+      if (years >= 0 && years < 30) {
52+        actualYear = Y2K_YEAR + years;
53+      } else if (years >= 30 && years < 100) {
54+        actualYear = FIRST_YEAR + years;
55+      }
56+      m = moment.utc([actualYear]).month(monthName);
57+    }
58   }
59 
60   // If we've not been able to parse the date by now, then we cannot parse it at all.
61diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
62index 818defc..d5a8f57 100644
63--- a/tests/FormulasTest.ts
64+++ b/tests/FormulasTest.ts
65@@ -1088,6 +1088,33 @@ catchAndAssertEquals(function() {
66 catchAndAssertEquals(function() {
67   DATEVALUE("32 June 2001");// overload numbers not ok
68 }, ERRORS.VALUE_ERROR);
69+// Month YYYY
70+assertEquals(DATEVALUE("Jan 2017"), 42736);
71+assertEquals(DATEVALUE("Feb 2017"), 42767);
72+assertEquals(DATEVALUE("Mar 2017"), 42795);
73+assertEquals(DATEVALUE("Apr 2017"), 42826);
74+assertEquals(DATEVALUE("May 2017"), 42856);
75+assertEquals(DATEVALUE("Jun 2017"), 42887);
76+assertEquals(DATEVALUE("Jul 2017"), 42917);
77+assertEquals(DATEVALUE("Aug 2017"), 42948);
78+assertEquals(DATEVALUE("Sep 2017"), 42979);
79+assertEquals(DATEVALUE("Oct 2017"), 43009);
80+assertEquals(DATEVALUE("Nov 2017"), 43040);
81+assertEquals(DATEVALUE("Dec 2017"), 43070);
82+assertEquals(DATEVALUE("Feb, 2017"), 42767);
83+assertEquals(DATEVALUE("  Feb    2017  "), 42767);
84+assertEquals(DATEVALUE("Feb-2017"), 42767);
85+assertEquals(DATEVALUE("Feb.2017"), 42767);
86+assertEquals(DATEVALUE("Feb/2017"), 42767);
87+assertEquals(DATEVALUE("Feb    .    2017"), 42767);
88+assertEquals(DATEVALUE("Feb -      2017"), 42767);
89+assertEquals(DATEVALUE("January 0030"), 10959);
90+assertEquals(DATEVALUE("November 4243"), 856069);
91+assertEquals(DATEVALUE("December 0100"), -657100);
92+catchAndAssertEquals(function() {
93+  DATEVALUE("December 100");// need 4 digits
94+}, ERRORS.VALUE_ERROR);
95+
96 
97 
98 // Test DB