spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.CONCATENATE
author
Ben Vogt <[email protected]>
date
2017-02-16 01:42:28
stats
2 file(s) changed, 41 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 9cfaa95..fbceab8 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -78,7 +78,8 @@ import {
 6   checkArgumentsLength,
 7   firstValueAsNumber,
 8   firstValueAsString,
 9-  valueToBoolean
10+  valueToBoolean,
11+  valueToString
12 } from "./Utils";
13 import { CellError } from "../Errors"
14 import * as ERRORS from "../Errors"
15@@ -89,7 +90,6 @@ var BIN2HEX = Formula["BIN2HEX"];
16 var BIN2OCT = Formula["BIN2OCT"];
17 var DECIMAL = Formula["DECIMAL"];
18 var COMBIN = Formula["COMBIN"];
19-var CONCATENATE = Formula["CONCATENATE"];
20 var CONVERT = Formula["CONVERT"];
21 var CORREL = Formula["CORREL"];
22 var COUNTUNIQUE = Formula["COUNTUNIQUE"];
23@@ -142,6 +142,28 @@ var SUMX2PY2 = Formula["SUMX2PY2"];
24 var TRUNC = Formula["TRUNC"];
25 var YEARFRAC = Formula["YEARFRAC"];
26 
27+/**
28+ * Appends strings to one another.
29+ * @param values to append to one another. Must contain at least one value
30+ * @returns {string} concatenated string
31+ * @constructor
32+ */
33+var CONCATENATE = function (...values) {
34+  checkArgumentsAtLeastLength(values, 1);
35+  var string = '';
36+  for (var i = 0; i < values.length; i++) {
37+    if (values[i] instanceof Array) {
38+      if (values[i].length === 0) {
39+        throw new CellError(ERRORS.REF_ERROR, "Reference does not exist.");
40+      }
41+      string += CONCATENATE.apply(this, arguments[i]);
42+    } else {
43+      string += valueToString(values[i]);
44+    }
45+  }
46+  return string;
47+};
48+
49 export {
50   __COMPLEX,
51 
52diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
53index c746642..6ff0be1 100644
54--- a/tests/FormulasTest.ts
55+++ b/tests/FormulasTest.ts
56@@ -359,7 +359,24 @@ assertEquals(CODE([['a'], 'p']), 97);
57 
58 assertEquals(COMBIN(4, 2), 6);
59 
60+
61+// Test CONCATENATE
62 assertEquals(CONCATENATE("hey", " ", "there"), "hey there");
63+assertEquals(CONCATENATE(["hey", " ", "there"]), "hey there");
64+assertEquals(CONCATENATE("hey"), "hey");
65+assertEquals(CONCATENATE("hey", 2), "hey2");
66+assertEquals(CONCATENATE("hey", false), "heyFALSE");
67+assertEquals(CONCATENATE([22, 14, "m", false]), "2214mFALSE");
68+assertEquals(CONCATENATE([22, 14, ["m", false]]), "2214mFALSE");
69+catchAndAssertEquals(function() {
70+  CONCATENATE();
71+}, ERRORS.NA_ERROR);
72+catchAndAssertEquals(function() {
73+  CONCATENATE("10", 4, false, []);
74+}, ERRORS.REF_ERROR);
75+catchAndAssertEquals(function() {
76+  CONCATENATE([]);
77+}, ERRORS.REF_ERROR);
78 
79 assertEquals(CONVERT(5.1, "mm", "m"), 0.0050999999999999995);
80