spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
working on parsign 'DD Month (Year)' parsing for DATEVALUE
author
Ben Vogt <[email protected]>
date
2017-03-26 19:59:28
stats
2 file(s) changed, 40 insertions(+), 0 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index ffb5769..c6100a2 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -355,6 +355,22 @@ var DATEVALUE = function (...values) : number {
 6     }
 7   }
 8 
 9+  // Check DD Month
10+  if (m === undefined) {
11+    // For reference: https://regex101.com/r/GM7KgL/2
12+    var matches = dateString.match(/^\s*(0?[1-9]|[1-2][0-9]|3[0-1])(,?\s*|\s*-?\/?\s*|\s*\.?\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*$/i);
13+    if (matches && matches.length === 4) {
14+      var monthName = matches[3];
15+      var days = parseInt(matches[1]) - 1; // Days are zero indexed.
16+      var tmpMoment = moment.utc([moment.utc().year()]).startOf('year').month(monthName);
17+      // If we're specifying more days than there are in this month
18+      if (days > tmpMoment.daysInMonth() - 1) {
19+        throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
20+      }
21+      m = tmpMoment.add({"days": days});
22+    }
23+  }
24+
25   // If we've not been able to parse the date by now, then we cannot parse it at all.
26   if (m === undefined || !m.isValid()) {
27     throw new CellError(VALUE_ERROR, "DATEVALUE parameter '" + dateString + "' cannot be parsed to date/time.");
28diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
29index 93af128..312e1f2 100644
30--- a/tests/DateFormulasTest.ts
31+++ b/tests/DateFormulasTest.ts
32@@ -399,5 +399,27 @@ assertEquals(DATEVALUE("Dec- 20"), 42358);
33 assertEquals(DATEVALUE("Dec-20"), 42358);
34 assertEquals(DATEVALUE("June 2"), 42157);
35 catchAndAssertEquals(function() {
36-  DATEVALUE("Dec.20");// need space if using command, period
37+  DATEVALUE("Dec.20");// need space if using period
38+}, ERRORS.VALUE_ERROR);
39+// DD Month
40+assertEquals(DATEVALUE("20 Jan"), 42024);
41+assertEquals(DATEVALUE("20 Feb"), 42055);
42+assertEquals(DATEVALUE("20 Mar"), 42083);
43+assertEquals(DATEVALUE("20 Apr"), 42114);
44+assertEquals(DATEVALUE("20 May"), 42144);
45+assertEquals(DATEVALUE("20 Jun"), 42175);
46+assertEquals(DATEVALUE("20 Jul"), 42205);
47+assertEquals(DATEVALUE("20 Aug"), 42236);
48+assertEquals(DATEVALUE("20 Sep"), 42267);
49+assertEquals(DATEVALUE("20 Oct"), 42297);
50+assertEquals(DATEVALUE("20 Nov"), 42328);
51+assertEquals(DATEVALUE("20 Dec"), 42358);
52+assertEquals(DATEVALUE("20. Dec"), 42358);
53+assertEquals(DATEVALUE("20, Dec"), 42358);
54+assertEquals(DATEVALUE("20/ Dec"), 42358);
55+assertEquals(DATEVALUE("20- Dec"), 42358);
56+assertEquals(DATEVALUE("20-Dec"), 42358);
57+assertEquals(DATEVALUE("2 June"), 42157);
58+catchAndAssertEquals(function() {
59+  DATEVALUE("20.Dec");// need space if using period
60 }, ERRORS.VALUE_ERROR);