spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.ACOS written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-16 21:34:14
stats
2 file(s) changed, 27 insertions(+), 2 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
 2index d550cfe..24ed276 100644
 3--- a/src/RawFormulas.ts
 4+++ b/src/RawFormulas.ts
 5@@ -23,7 +23,7 @@ function checkArgumentsLength(args: any, length: number) {
 6  */
 7 function valueToNumber(value: any) : number {
 8   if (typeof value === "number") {
 9-    return Math.abs(value);
10+    return value;
11   } else if (typeof value === "string") {
12     if (value.indexOf(".") > -1) {
13       var fl = parseFloat(value);
14@@ -105,7 +105,18 @@ var ACCRINT = function (issue, first, settlement, rate, par, frequency, basis) {
15   }
16   return par * rate * factor;
17 };
18-var ACOS = Formula["ACOS"];
19+
20+var ACOS = function (value?) {
21+  checkArgumentsLength(arguments, 1);
22+  value = valueToNumber(value);
23+  if (value === -1) {
24+    return Math.PI;
25+  } else if (value > 1 || value < -1) {
26+    throw new CellError(ERRORS.NUM_ERROR, "Function ____ parameter 1 value is " + value + ". Valid values are between -1 and 1 inclusive.");
27+  }
28+  return Math.acos(value);
29+};
30+
31 var ACOSH = Formula["ACOSH"];
32 var ACOTH = Formula["ACOTH"];
33 var AND = Formula["AND"];
34diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
35index bfb5d84..d0ef1c7 100644
36--- a/tests/FormulasTest.ts
37+++ b/tests/FormulasTest.ts
38@@ -22,6 +22,7 @@ function catchAndAssertEquals(toExecute, expected) {
39   }
40 }
41 
42+
43 assertEquals(ABS(-10), 10);
44 assertEquals(ABS(-10.111), 10.111);
45 assertEquals(ABS(0), 0);
46@@ -34,11 +35,23 @@ catchAndAssertEquals(function() {
47   return ABS("str");
48 }, ERRORS.VALUE_ERROR);
49 
50+
51 // TODO: This formula doesn't work properly under some circumstances.
52 assertEquals(ACCRINT(DATE(2011, 1, 1), DATE(2011, 2, 1), DATE(2014, 7, 1), 0.1, 1000, 1, 0), 350);
53-// assertEquals(ACCRINT(DATE(2000, 1, 1), DATE(2000, 2, 1), DATE(2002, 12, 31), 0.05, 100, 4), 14.98611111);
54+
55 
56 assertEquals(ACOS(0), 1.5707963267948966);
57+assertEquals(ACOS(-1), 3.141592653589793);
58+assertEquals(ACOS(1), 0);
59+assertEquals(ACOS("-1"), 3.141592653589793);
60+assertEquals(ACOS(false), 1.5707963267948966);
61+catchAndAssertEquals(function() {
62+  return ACOS("str");
63+}, ERRORS.VALUE_ERROR);
64+catchAndAssertEquals(function() {
65+  return ACOS(2);
66+}, ERRORS.NUM_ERROR);
67+
68 
69 assertEquals(ACOSH(22), 3.783672704329451);
70