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 regex for parsing '1/23/2012' date strings
author
Ben Vogt <[email protected]>
date
2017-02-26 20:14:05
stats
2 file(s) changed, 14 insertions(+), 8 deletions(-)
files
src/RawFormulas/Date.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index fe2f448..9b1e8f2 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -19,6 +19,7 @@ import {
 6  * @param values[2] day - The day component of the date.
 7  * @returns {Date} newly created date.
 8  * @constructor
 9+ * TODO: This function should take overflow values for month and day (eg: 44) and roll them over to the next unit.
10  */
11 var DATE = function (...values) {
12   ArgsChecker.checkLength(values, 3);
13@@ -44,7 +45,17 @@ var DATE = function (...values) {
14  */
15 var DATEVALUE = function (...values) : number {
16   ArgsChecker.checkLength(values, 1);
17-  var dateNumber = new ExcelDate(moment()).toNumber();
18+  var dateString = TypeCaster.firstValueAsString(values[0]);
19+  var format = "M/D/Y";
20+  var dateNumber;
21+  if (false) { // Check "2012/1/23"
22+
23+  } if (dateString.match(/^\s*([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\/([1-9][0-9][0-9][0-9])\s*$/)) { // Check "1/23/2012"
24+    dateNumber = new ExcelDate(moment(dateString, format)).toNumber();
25+  }
26+  if (dateNumber === undefined) {
27+    // TODO: Throw error that we couldn't parse the dateString.
28+  }
29   if (dateNumber < 0) {
30     throw new CellError(NUM_ERROR, "DATEVALUE evaluates to an out of range value " + dateNumber
31       + ". It should be greater than or equal to 0.");
32diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
33index dc2d422..994f8d1 100644
34--- a/tests/FormulasTest.ts
35+++ b/tests/FormulasTest.ts
36@@ -776,13 +776,8 @@ assertEquals(DATE(2004, 3, 1).toNumber(), 38047);
37 
38 
39 
40-// assertEqualsDates(DATEVALUE("1992-6-24"), new Date("6/24/1992"));
41-//
42-// assertEquals(DAY(DATEVALUE("1992-6-24")), 24);
43-//
44-// assertEquals(DAYS(DATEVALUE("1993-6-24"), DATEVALUE("1992-6-24")), 365);
45-//
46-// assertEquals(DAYS360(DATE(1969, 7, 16), DATE(1970, 7, 24), 1), 368);
47+// Test DATEVALUE
48+assertEquals(DATEVALUE("6/24/1992"), 33779);
49 
50 
51 // Test DB