spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[YEAR] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-08 00:04:30
stats
3 file(s) changed, 50 insertions(+), 4 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 514ba27..c3d3101 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -202,6 +202,23 @@ var MONTH = function (...values) : number {
  6 };
  7 
  8 
  9+/**
 10+ * Returns the year specified by a given date.
 11+ * @param values[0] date - The date from which to calculate the year. Must be a cell reference to a cell containing a
 12+ * date, a function returning a date type, or a number.
 13+ * @returns {number} year of the input date
 14+ * @constructor
 15+ */
 16+var YEAR = function (...values) {
 17+  ArgsChecker.checkLength(values, 1);
 18+  var date = TypeCaster.firstValueAsExcelDate(values[0], true); // tell firstValueAsExcelDate to coerce boolean
 19+  if (date.toNumber() < 0) {
 20+    throw new NumError("Function MONTH parameter 1 value is " + date.toNumber() + ". It should be greater than or equal to 0.");
 21+  }
 22+  return date.toMoment().year();
 23+};
 24+
 25+
 26 
 27 var YEARFRAC = Formula["YEARFRAC"];
 28 // Functions unimplemented.
 29@@ -221,7 +238,6 @@ var TODAY;
 30 var WEEKDAY;
 31 var WEEKNUM;
 32 var WORKDAY;
 33-var YEAR;
 34 
 35 export {
 36   DATE,
 37@@ -232,5 +248,6 @@ export {
 38   EDATE,
 39   EOMONTH,
 40   MONTH,
 41+  YEAR,
 42   YEARFRAC
 43 }
 44\ No newline at end of file
 45diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 46index dd3d3aa..a95babc 100644
 47--- a/src/RawFormulas/RawFormulas.ts
 48+++ b/src/RawFormulas/RawFormulas.ts
 49@@ -117,6 +117,7 @@ import {
 50   EDATE,
 51   EOMONTH,
 52   MONTH,
 53+  YEAR,
 54   YEARFRAC
 55 } from "./Date"
 56 
 57@@ -238,5 +239,6 @@ export {
 58   XOR,
 59   YEARFRAC,
 60   RADIANS,
 61-  MONTH
 62+  MONTH,
 63+  YEAR
 64 }
 65\ No newline at end of file
 66diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 67index d9b009a..a454542 100644
 68--- a/tests/DateFormulasTest.ts
 69+++ b/tests/DateFormulasTest.ts
 70@@ -1,5 +1,5 @@
 71 
 72-import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH } from "../src/RawFormulas/RawFormulas"
 73+import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH, YEAR } from "../src/RawFormulas/RawFormulas"
 74 import * as ERRORS from "../src/Errors"
 75 import {assertEquals} from "./utils/Asserts"
 76 import moment = require("moment");
 77@@ -20,6 +20,32 @@ function catchAndAssertEquals(toExecute, expected) {
 78 }
 79 
 80 
 81+// Test YEAR
 82+assertEquals(YEAR(DATE(1992, 6, 24)), 1992);
 83+assertEquals(YEAR(DATE(2000, 6, 24)), 2000);
 84+assertEquals(YEAR(DATE(100, 6, 24)), 2000);
 85+assertEquals(YEAR(DATE(44, 6, 24)), 1944);
 86+assertEquals(YEAR(1312212), 5492);
 87+assertEquals(YEAR(9), 1900);
 88+assertEquals(YEAR(0), 1899);
 89+assertEquals(YEAR(false), 1899);
 90+assertEquals(YEAR(1), 1899);
 91+assertEquals(YEAR(true), 1899);
 92+assertEquals(YEAR([1, "str"]), 1899);
 93+catchAndAssertEquals(function() {
 94+  YEAR();
 95+}, ERRORS.NA_ERROR);
 96+catchAndAssertEquals(function() {
 97+  YEAR(213123, 123123);
 98+}, ERRORS.NA_ERROR);
 99+catchAndAssertEquals(function() {
100+  YEAR("str");
101+}, ERRORS.VALUE_ERROR);
102+catchAndAssertEquals(function() {
103+  YEAR([]);
104+}, ERRORS.REF_ERROR);
105+
106+
107 // Test MONTH
108 assertEquals(MONTH(DATE(1992, 6, 24)), 6);
109 assertEquals(MONTH(1312212), 9);