spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[MONTH] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-04-07 23:52:57
stats
3 file(s) changed, 45 insertions(+), 6 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 84f242e..514ba27 100644
  3--- a/src/RawFormulas/Date.ts
  4+++ b/src/RawFormulas/Date.ts
  5@@ -185,12 +185,29 @@ var DAYS360 = function (...values) {
  6 };
  7 
  8 
  9+/**
 10+ * Returns the month of the year a specific date falls in, in numeric format.
 11+ * @param values[0] date - The date from which to extract the month. Must be a reference to a cell containing a date, a
 12+ * function returning a date type, or a number.
 13+ * @returns {number} month of the year that the input date falls on.
 14+ * @constructor
 15+ */
 16+var MONTH = function (...values) : number {
 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().month() + 1;
 23+};
 24+
 25+
 26+
 27 var YEARFRAC = Formula["YEARFRAC"];
 28 // Functions unimplemented.
 29 var DATEDIF;
 30 var HOUR;
 31 var MINUTE;
 32-var MONTH;
 33 var NETWORKDAYS;
 34 var __COMPLEX_ITL = {
 35   "NETWORKDAYS.ITL": function () {},
 36@@ -214,5 +231,6 @@ export {
 37   DAYS360,
 38   EDATE,
 39   EOMONTH,
 40+  MONTH,
 41   YEARFRAC
 42 }
 43\ No newline at end of file
 44diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 45index 962f2cf..dd3d3aa 100644
 46--- a/src/RawFormulas/RawFormulas.ts
 47+++ b/src/RawFormulas/RawFormulas.ts
 48@@ -116,6 +116,7 @@ import {
 49   DAYS360,
 50   EDATE,
 51   EOMONTH,
 52+  MONTH,
 53   YEARFRAC
 54 } from "./Date"
 55 
 56@@ -236,5 +237,6 @@ export {
 57   TRUNC,
 58   XOR,
 59   YEARFRAC,
 60-  RADIANS
 61+  RADIANS,
 62+  MONTH
 63 }
 64\ No newline at end of file
 65diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
 66index 6097617..d9b009a 100644
 67--- a/tests/DateFormulasTest.ts
 68+++ b/tests/DateFormulasTest.ts
 69@@ -1,5 +1,5 @@
 70 
 71-import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360 } from "../src/RawFormulas/RawFormulas"
 72+import { DATE, DATEVALUE, EDATE, EOMONTH, DAY, DAYS, DAYS360, MONTH } from "../src/RawFormulas/RawFormulas"
 73 import * as ERRORS from "../src/Errors"
 74 import {assertEquals} from "./utils/Asserts"
 75 import moment = require("moment");
 76@@ -19,6 +19,25 @@ function catchAndAssertEquals(toExecute, expected) {
 77   }
 78 }
 79 
 80+
 81+// Test MONTH
 82+assertEquals(MONTH(DATE(1992, 6, 24)), 6);
 83+assertEquals(MONTH(1312212), 9);
 84+assertEquals(MONTH(13122121), 2);
 85+catchAndAssertEquals(function() {
 86+  MONTH();
 87+}, ERRORS.NA_ERROR);
 88+catchAndAssertEquals(function() {
 89+  MONTH(213123, 123123);
 90+}, ERRORS.NA_ERROR);
 91+catchAndAssertEquals(function() {
 92+  MONTH("str");
 93+}, ERRORS.VALUE_ERROR);
 94+catchAndAssertEquals(function() {
 95+  MONTH([]);
 96+}, ERRORS.REF_ERROR);
 97+
 98+
 99 // Test DAYS360
100 assertEquals(DAYS360(DATE(1992, 6, 24), DATE(1992, 6, 25)), 1);
101 assertEquals(DAYS360(DATE(1992, 6, 25), DATE(1992, 6, 24)), -1);