spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Starting work on Date.ts.
author
Ben Vogt <[email protected]>
date
2017-02-24 04:32:48
stats
4 file(s) changed, 109 insertions(+), 27 deletions(-)
files
README.md
src/RawFormulas/Date.ts
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
  1diff --git a/README.md b/README.md
  2index 1b05562..7c73055 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -23,6 +23,8 @@ arbitrary javascript is executed in the client machine.
  6 
  7 ### Criteria evaluations should escape reg-ex characters: http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
  8 
  9+### Criteria evaluations should accept dollar-to-number comparisons: `=COUNTIF({10, 20, 40}, ">=$20")`
 10+
 11 ### License for all code used.
 12 
 13 ### Document the functions pulled in from jStat.
 14diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 15new file mode 100644
 16index 0000000..522b48f
 17--- /dev/null
 18+++ b/src/RawFormulas/Date.ts
 19@@ -0,0 +1,74 @@
 20+/// <reference path="../../node_modules/moment/moment.d.ts"/>
 21+import * as moment from "moment";
 22+import * as Formula from "formulajs"
 23+import {
 24+  ArgsChecker, TypeCaster
 25+} from "./Utils";
 26+
 27+
 28+/**
 29+ * Converts a provided year, month, and day into a date.
 30+ * @param values[0] year - The year component of the date.
 31+ * @param values[1] month - The month component of the date.
 32+ * @param values[2] day - The day component of the date.
 33+ * @returns {Date} newly created date.
 34+ * @constructor
 35+ */
 36+var DATE = function (...values) {
 37+  ArgsChecker.checkLength(values, 3);
 38+  var year = Math.abs(Math.floor(TypeCaster.firstValueAsNumber(values[0]))); // No negative values for year
 39+  var month = Math.floor(TypeCaster.firstValueAsNumber(values[1]) - 1); // Months are between 0 and 11.
 40+  var day = Math.floor(TypeCaster.firstValueAsNumber(values[2]));
 41+  // TODO: When we create a date we should use DATEVALUE-style numeric conversion to ensure the value is greater than 0.
 42+  // TODO: (cont.) throw new CellError(ERRORS.NUM_ERROR, "DATE evaluates to an out of range value -6420. It should be
 43+  // TODO: (cont.) greater than or equal to 0.");
 44+  return new Date(year, month, day);
 45+};
 46+
 47+
 48+var DATEVALUE = function (dateString: string) : Date {
 49+  return new Date(dateString);
 50+};
 51+
 52+var DAY = Formula["DAY"];
 53+var DAYS = Formula["DAYS"];
 54+var DAYS360 = Formula["DAYS360"];
 55+var EDATE = function (start_date: Date, months) {
 56+  return moment(start_date).add(months, 'months').toDate();
 57+};
 58+var EOMONTH = function (start_date, months) {
 59+  var edate = moment(start_date).add(months, 'months');
 60+  return new Date(edate.year(), edate.month(), edate.daysInMonth());
 61+};
 62+var YEARFRAC = Formula["YEARFRAC"];
 63+
 64+// Functions unimplemented.
 65+var DATEDIF;
 66+var HOUR;
 67+var MINUTE;
 68+var MONTH;
 69+var NETWORKDAYS;
 70+var __COMPLEX_ITL = {
 71+  "NETWORKDAYS.ITL": function () {},
 72+  "WORKDAY.INTL": function () {}
 73+};
 74+var NOW;
 75+var SECOND;
 76+var TIME;
 77+var TIMEVALUE;
 78+var TODAY;
 79+var WEEKDAY;
 80+var WEEKNUM;
 81+var WORKDAY;
 82+var YEAR;
 83+
 84+export {
 85+  DATE,
 86+  DATEVALUE,
 87+  DAYS,
 88+  DAY,
 89+  DAYS360,
 90+  EDATE,
 91+  EOMONTH,
 92+  YEARFRAC
 93+}
 94\ No newline at end of file
 95diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 96index 9b44602..0aaa2a1 100644
 97--- a/src/RawFormulas/RawFormulas.ts
 98+++ b/src/RawFormulas/RawFormulas.ts
 99@@ -1,5 +1,3 @@
100-/// <reference path="../../node_modules/moment/moment.d.ts"/>
101-import * as moment from "moment";
102 import * as Formula from "formulajs"
103 import {
104   ABS,
105@@ -110,6 +108,16 @@ import {
106   SPLIT,
107   CONCATENATE
108 } from "./Text"
109+import {
110+  DATE,
111+  DATEVALUE,
112+  DAYS,
113+  DAY,
114+  DAYS360,
115+  EDATE,
116+  EOMONTH,
117+  YEARFRAC
118+} from "./Date"
119 
120 import {
121   CriteriaFunctionFactory,
122@@ -125,21 +133,6 @@ import * as ERRORS from "../Errors"
123 
124 var ACCRINT = Formula["ACCRINT"];
125 var CONVERT = Formula["CONVERT"];
126-var DATE = Formula["DATE"];
127-var DATEVALUE = function (dateString: string) : Date {
128-  return new Date(dateString);
129-};
130-var DAY = Formula["DAY"];
131-var DAYS = Formula["DAYS"];
132-var DAYS360 = Formula["DAYS360"];
133-var EDATE = function (start_date: Date, months) {
134-  return moment(start_date).add(months, 'months').toDate();
135-};
136-var EOMONTH = function (start_date, months) {
137-  var edate = moment(start_date).add(months, 'months');
138-  return new Date(edate.year(), edate.month(), edate.daysInMonth());
139-};
140-var YEARFRAC = Formula["YEARFRAC"];
141 
142 
143 // Using alias to bind dot-notation function names.
144diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
145index d113d5d..0303d3d 100644
146--- a/tests/FormulasTest.ts
147+++ b/tests/FormulasTest.ts
148@@ -757,6 +757,7 @@ catchAndAssertEquals(function() {
149 }, ERRORS.NA_ERROR);
150 
151 
152+// Test DATE
153 assertEqualsDates(DATE(1992, 6, 24), new Date("6/24/1992"));
154 assertEqualsDates(DATE(1992, 13, 24), new Date("1/24/1993"));
155 assertEqualsDates(DATE(1992, 6, 44), new Date("7/14/1992"));
156@@ -764,6 +765,16 @@ assertEqualsDates(DATE(2, 6, 44), new Date("7/14/1902"));
157 assertEqualsDates(DATE(2, 33, 44), new Date("10/14/1904"));
158 assertEqualsDates(DATE(1976, 2, 29), new Date("2/29/1976"));
159 assertEqualsDates(DATE(1976, 2, 30), new Date("3/1/1976"));
160+catchAndAssertEquals(function() {
161+  DATE();
162+}, ERRORS.NA_ERROR);
163+catchAndAssertEquals(function() {
164+  DATE(1976, 2);
165+}, ERRORS.NA_ERROR);
166+catchAndAssertEquals(function() {
167+  DATE(1976, 2, 30, 22);
168+}, ERRORS.NA_ERROR);
169+
170 
171 assertEqualsDates(DATEVALUE("1992-6-24"), new Date("6/24/1992"));
172