spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[WORKDAY] formula fully tested
author
Ben Vogt <[email protected]>
date
2017-04-29 16:11:05
stats
2 file(s) changed, 27 insertions(+), 10 deletions(-)
files
src/RawFormulas/Date.ts
tests/DateFormulasTest.ts
 1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
 2index 463f08c..a4aaf4f 100644
 3--- a/src/RawFormulas/Date.ts
 4+++ b/src/RawFormulas/Date.ts
 5@@ -836,18 +836,22 @@ var WORKDAY = function (...values) {
 6   var hasHolidays = values.length === 3;
 7   var holidays = [];
 8   if (hasHolidays) {
 9-    if (values[2].length === 0) {
10-      throw new RefError("Reference does not exist.");
11-    }
12-    for (var holidayDateValue of values[2]) {
13-      if (holidayDateValue instanceof ExcelDate) {
14-        holidays.push(holidayDateValue.toNumber());
15-      } else if (typeof holidayDateValue === "number") {
16-        holidays.push(holidayDateValue);
17-      } else {
18-        throw new ValueError("WORKDAY expects number values. But '" + holidayDateValue + "' is a " +
19-          (typeof holidayDateValue) + " and cannot be coerced to a number.")
20+    if (values[2] instanceof Array) {
21+      if (values[2].length === 0) {
22+        throw new RefError("Reference does not exist.");
23       }
24+      for (var holidayDateValue of values[2]) {
25+        if (holidayDateValue instanceof ExcelDate) {
26+          holidays.push(holidayDateValue.toNumber());
27+        } else if (typeof holidayDateValue === "number") {
28+          holidays.push(holidayDateValue);
29+        } else {
30+          throw new ValueError("WORKDAY expects number values. But '" + holidayDateValue + "' is a " +
31+            (typeof holidayDateValue) + " and cannot be coerced to a number.")
32+        }
33+      }
34+    } else {
35+      holidays.push(TypeCaster.valueToNumber(values[2]));
36     }
37   }
38 
39diff --git a/tests/DateFormulasTest.ts b/tests/DateFormulasTest.ts
40index ea33e4a..d9045a3 100644
41--- a/tests/DateFormulasTest.ts
42+++ b/tests/DateFormulasTest.ts
43@@ -33,7 +33,19 @@ import moment = require("moment");
44 // Test WORKDAY
45 assertEquals(WORKDAY(DATE(1999, 2, 2), 10), DATE(1999, 2, 16));
46 assertEquals(WORKDAY(DATE(1999, 10, 10), 100), DATE(2000, 2, 25));
47-
48+assertEquals(WORKDAY(DATE(1909, 12, 11), 222), DATE(1910, 10, 18));
49+assertEquals(WORKDAY(DATE(1922, 4, 1), 1234), DATE(1926, 12, 23));
50+assertEquals(WORKDAY(DATE(1945, 1, 14), 6000), DATE(1968, 1, 12));
51+assertEquals(WORKDAY(DATE(1945, 1, 14), 6000, [23855, 23856, 23857, 23858, 23859]), DATE(1968, 1, 17));
52+assertEquals(WORKDAY(DATE(1945, 1, 14), 6000, 23859), DATE(1968, 1, 15));
53+assertEquals(WORKDAY(DATE(2012, 5, 29), 1000, [41058, 41059, 41060, 41061, 41062]), DATE(2016, 4, 1));
54+assertEquals(WORKDAY([DATE(1999, 2, 2)], [10]), DATE(1999, 2, 16));
55+catchAndAssertEquals(function() {
56+  WORKDAY();
57+}, ERRORS.NA_ERROR);
58+catchAndAssertEquals(function() {
59+  WORKDAY(DATE(2012, 5, 29), 1000, [10], 11);
60+}, ERRORS.NA_ERROR);
61 
62 
63 // Test TIME