spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[PRODUCT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-13 01:10:26
stats
10 file(s) changed, 88 insertions(+), 8 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
dist/Utilities/TypeConverter.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
src/Utilities/TypeConverter.ts
tests/Formulas/MathTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index b17242d..da1efd1 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1143,6 +1143,14 @@
  6 @returns {number} number of ways 
  7 @constructor
  8 ```
  9+
 10+### PRODUCT 
 11+
 12+```
 13+  Multiply a series of numbers together. 
 14+@param values - values or range of values to multiply by each other. 
 15+@constructor
 16+```
 17 ## Statistical
 18 
 19 
 20diff --git a/TODO.md b/TODO.md
 21index 080531e..b185020 100644
 22--- a/TODO.md
 23+++ b/TODO.md
 24@@ -58,7 +58,6 @@ For example 64 tbs to a qt.
 25 * FACTDOUBLE
 26 * MROUND
 27 * MULTINOMIAL
 28-* PRODUCT
 29 * QUOTIENT
 30 * SERIESSUM
 31 * SUBTOTAL
 32diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 33index f0f9b0c..3415886 100644
 34--- a/dist/Formulas/AllFormulas.js
 35+++ b/dist/Formulas/AllFormulas.js
 36@@ -67,6 +67,7 @@ exports.NE = Math_1.NE;
 37 exports.GCD = Math_1.GCD;
 38 exports.LCM = Math_1.LCM;
 39 exports.GAMMALN = Math_1.GAMMALN;
 40+exports.PRODUCT = Math_1.PRODUCT;
 41 var Info_1 = require("./Info");
 42 exports.NA = Info_1.NA;
 43 var Lookup_1 = require("./Lookup");
 44diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 45index a615443..20e8aa9 100644
 46--- a/dist/Formulas/Math.js
 47+++ b/dist/Formulas/Math.js
 48@@ -1223,3 +1223,22 @@ var COMBIN = function (m, k) {
 49     return fact(n) / div;
 50 };
 51 exports.COMBIN = COMBIN;
 52+/**
 53+ * Multiply a series of numbers together.
 54+ * @param values - values or range of values to multiply by each other.
 55+ * @constructor
 56+ */
 57+var PRODUCT = function () {
 58+    var values = [];
 59+    for (var _i = 0; _i < arguments.length; _i++) {
 60+        values[_i] = arguments[_i];
 61+    }
 62+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(values, 2, "PRODUCT");
 63+    var value = 1;
 64+    var numbers = Filter_1.Filter.flattenAndThrow(values);
 65+    for (var i = 0; i < numbers.length; i++) {
 66+        value *= TypeConverter_1.TypeConverter.valueToNumber(numbers[i]);
 67+    }
 68+    return value;
 69+};
 70+exports.PRODUCT = PRODUCT;
 71diff --git a/dist/Utilities/TypeConverter.js b/dist/Utilities/TypeConverter.js
 72index 66f7418..46cb3d3 100644
 73--- a/dist/Utilities/TypeConverter.js
 74+++ b/dist/Utilities/TypeConverter.js
 75@@ -403,6 +403,14 @@ var TypeConverter = (function () {
 76             }
 77             return x;
 78         }
 79+        else {
 80+            try {
 81+                return TypeConverter.stringToDateNumber(value);
 82+            }
 83+            catch (_) {
 84+                return undefined;
 85+            }
 86+        }
 87     };
 88     /**
 89      * Converts any value to a number or throws an error if it cannot coerce it to the number type
 90diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 91index ad29f1b..1c3f5d5 100644
 92--- a/src/Formulas/AllFormulas.ts
 93+++ b/src/Formulas/AllFormulas.ts
 94@@ -64,7 +64,8 @@ import {
 95   NE,
 96   GCD,
 97   LCM,
 98-  GAMMALN
 99+  GAMMALN,
100+  PRODUCT
101 } from "./Math";
102 import {
103   NA
104@@ -311,5 +312,6 @@ export {
105   LCM,
106   GAMMALN,
107   QUARTILE,
108-  PERCENTILE
109+  PERCENTILE,
110+  PRODUCT
111 }
112\ No newline at end of file
113diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
114index b555e11..05468df 100644
115--- a/src/Formulas/Math.ts
116+++ b/src/Formulas/Math.ts
117@@ -67,7 +67,6 @@ var LCM =  function (...values) {
118   return r;
119 };
120 
121-
122 /**
123  * Returns the the logarithm of a specified Gamma function, base e (Euler's number).
124  * @param value - The input number. The natural logarithm of Gamma (value) will be returned. Must be positive.
125@@ -83,7 +82,6 @@ var GAMMALN = function (value) {
126   return gammaln(x);
127 };
128 
129-
130 /**
131  * Returns the absolute value of a number.
132  * @param value to get the absolute value of.
133@@ -1237,6 +1235,21 @@ var COMBIN = function (m, k) : number {
134   return fact(n) / div;
135 };
136 
137+/**
138+ * Multiply a series of numbers together.
139+ * @param values - values or range of values to multiply by each other.
140+ * @constructor
141+ */
142+var PRODUCT =  function (...values) {
143+  ArgsChecker.checkAtLeastLength(values, 2, "PRODUCT");
144+  var value = 1;
145+  var numbers = Filter.flattenAndThrow(values);
146+  for (var i = 0; i < numbers.length; i++) {
147+    value *= TypeConverter.valueToNumber(numbers[i]);
148+  }
149+  return value;
150+};
151+
152 export {
153   ABS,
154   ACOS,
155@@ -1303,5 +1316,6 @@ export {
156   NE,
157   GCD,
158   LCM,
159-  GAMMALN
160+  GAMMALN,
161+  PRODUCT
162 }
163\ No newline at end of file
164diff --git a/src/Utilities/TypeConverter.ts b/src/Utilities/TypeConverter.ts
165index 6bb0ca1..c0f0c69 100644
166--- a/src/Utilities/TypeConverter.ts
167+++ b/src/Utilities/TypeConverter.ts
168@@ -411,6 +411,12 @@ class TypeConverter {
169         x = x * 0.01;
170       }
171       return x;
172+    } else {
173+      try {
174+        return TypeConverter.stringToDateNumber(value);
175+      } catch (_) {
176+        return undefined;
177+      }
178     }
179   }
180 
181diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
182index 8337f7e..e982e64 100644
183--- a/tests/Formulas/MathTest.ts
184+++ b/tests/Formulas/MathTest.ts
185@@ -64,7 +64,8 @@ import {
186   NE,
187   GCD,
188   LCM,
189-  GAMMALN
190+  GAMMALN,
191+  PRODUCT
192 } from "../../src/Formulas/Math";
193 import * as ERRORS from "../../src/Errors";
194 import {
195@@ -100,6 +101,24 @@ test("LCM", function(){
196 });
197 
198 
199+test("PRODUCT", function(){
200+  assertEquals(PRODUCT(2, 5), 10);
201+  assertEquals(PRODUCT(2, 5, 4, 2, 8, 1, 77, 2, 3, 1), 295680);
202+  assertEquals(PRODUCT(2, 5, 4, 2, 8, [1, 77, 2], 3, 1), 295680);
203+  assertEquals(PRODUCT(0, 1), 0);
204+  assertEquals(PRODUCT(8, "1992/2/2"), 269088);
205+  catchAndAssertEquals(function() {
206+    PRODUCT.apply(this, [1, [], 1]);
207+  }, ERRORS.REF_ERROR);
208+  catchAndAssertEquals(function() {
209+    PRODUCT.apply(this, [1]);
210+  }, ERRORS.NA_ERROR);
211+  catchAndAssertEquals(function() {
212+    PRODUCT.apply(this, []);
213+  }, ERRORS.NA_ERROR);
214+});
215+
216+
217 test("GCD", function(){
218   assertEquals(GCD(10, 100), 10);
219   assertEquals(GCD(22, 44), 22);
220diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
221index b0c1328..c304431 100644
222--- a/tests/SheetFormulaTest.ts
223+++ b/tests/SheetFormulaTest.ts
224@@ -259,6 +259,10 @@ test("Sheet GAMMALN", function(){
225   assertFormulaEquals('=GAMMALN(4.5)', 2.453736570842444);
226 });
227 
228+test("Sheet PRODUCT", function(){
229+  assertFormulaEquals('=PRODUCT(2, 2)', 4);
230+});
231+
232 test("Sheet PERCENTILE", function(){
233   assertFormulaEquals('=PERCENTILE([10], 0)', 10);
234 });