spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Text.LEFT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-08 16:05:54
stats
8 file(s) changed, 90 insertions(+), 11 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Text.js
src/Formulas/AllFormulas.ts
src/Formulas/Text.ts
tests/Formulas/TextTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index cf70de2..9bcb724 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2431,3 +2431,13 @@
  6 @param value - The text whose length is to be determined. 
  7 @constructor
  8 ```
  9+
 10+### LEFT 
 11+
 12+```
 13+  Returns the first character or characters in a text string. 
 14+@param text - The text where the initial partial words are to be determined 
 15+@param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined, one character is returned. 
 16+@returns {string} 
 17+@constructor
 18+```
 19diff --git a/TODO.md b/TODO.md
 20index eec33bb..cb12bbf 100644
 21--- a/TODO.md
 22+++ b/TODO.md
 23@@ -40,7 +40,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 24 
 25 ### Easy formulas to write
 26 * CLEAN
 27-* LEFT
 28 * MID
 29 * PROPER
 30 * REGEXEXTRACT - May be difficult considering language differences.
 31diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 32index 50fde0f..f9d65ad 100644
 33--- a/dist/Formulas/AllFormulas.js
 34+++ b/dist/Formulas/AllFormulas.js
 35@@ -230,6 +230,7 @@ exports.TEXT = Text_1.TEXT;
 36 exports.FIND = Text_1.FIND;
 37 exports.JOIN = Text_1.JOIN;
 38 exports.LEN = Text_1.LEN;
 39+exports.LEFT = Text_1.LEFT;
 40 var Date_1 = require("./Date");
 41 exports.DATE = Date_1.DATE;
 42 exports.DATEVALUE = Date_1.DATEVALUE;
 43diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
 44index 08f66c7..b0326be 100644
 45--- a/dist/Formulas/Text.js
 46+++ b/dist/Formulas/Text.js
 47@@ -756,3 +756,22 @@ var LEN = function (value) {
 48     return value.length;
 49 };
 50 exports.LEN = LEN;
 51+/**
 52+ * Returns the first character or characters in a text string.
 53+ * @param text - The text where the initial partial words are to be determined
 54+ * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
 55+ * one character is returned.
 56+ * @returns {string}
 57+ * @constructor
 58+ */
 59+var LEFT = function (text, numberOfCharacters) {
 60+    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "LEFT");
 61+    text = TypeConverter_1.TypeConverter.firstValueAsString(text);
 62+    numberOfCharacters = MoreUtils_1.isUndefined(numberOfCharacters) ? 1 : numberOfCharacters;
 63+    if (numberOfCharacters < 0) {
 64+        throw new Errors_1.ValueError("Formula LEFT parameter 2 value is " + numberOfCharacters
 65+            + ", but should be greater than or equal to 0.");
 66+    }
 67+    return text.substring(0, numberOfCharacters);
 68+};
 69+exports.LEFT = LEFT;
 70diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 71index e4de41e..37ef2e7 100644
 72--- a/src/Formulas/AllFormulas.ts
 73+++ b/src/Formulas/AllFormulas.ts
 74@@ -237,7 +237,8 @@ import {
 75   TEXT,
 76   FIND,
 77   JOIN,
 78-  LEN
 79+  LEN,
 80+  LEFT
 81 } from "./Text"
 82 import {
 83   DATE,
 84@@ -535,5 +536,6 @@ export {
 85   ZTEST,
 86   FIND,
 87   JOIN,
 88-  LEN
 89+  LEN,
 90+  LEFT
 91 }
 92\ No newline at end of file
 93diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
 94index 245b128..40d9b00 100644
 95--- a/src/Formulas/Text.ts
 96+++ b/src/Formulas/Text.ts
 97@@ -788,6 +788,25 @@ let LEN = function (value) {
 98   return value.length;
 99 };
100 
101+/**
102+ * Returns the first character or characters in a text string.
103+ * @param text - The text where the initial partial words are to be determined
104+ * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
105+ * one character is returned.
106+ * @returns {string}
107+ * @constructor
108+ */
109+let LEFT = function (text, numberOfCharacters?) {
110+  ArgsChecker.checkLengthWithin(arguments, 1, 2, "LEFT");
111+  text = TypeConverter.firstValueAsString(text);
112+  numberOfCharacters = isUndefined(numberOfCharacters) ? 1 : numberOfCharacters;
113+  if (numberOfCharacters < 0) {
114+    throw new ValueError("Formula LEFT parameter 2 value is " + numberOfCharacters
115+        + ", but should be greater than or equal to 0.");
116+  }
117+  return text.substring(0, numberOfCharacters)
118+};
119+
120 export {
121   ARABIC,
122   CHAR,
123@@ -803,5 +822,6 @@ export {
124   TEXT,
125   FIND,
126   JOIN,
127-  LEN
128+  LEN,
129+  LEFT
130 }
131\ No newline at end of file
132diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
133index 357c8dc..1e36dcc 100644
134--- a/tests/Formulas/TextTest.ts
135+++ b/tests/Formulas/TextTest.ts
136@@ -13,7 +13,8 @@ import {
137   TEXT,
138   FIND,
139   JOIN,
140-  LEN
141+  LEN,
142+  LEFT
143 } from "../../src/Formulas/Text";
144 import * as ERRORS from "../../src/Errors";
145 import {
146@@ -204,7 +205,7 @@ test("ROMAN", function(){
147     ROMAN(0);
148   }, ERRORS.VALUE_ERROR);
149   catchAndAssertEquals(function() {
150-    ROMAN.apply(this, [])
151+    ROMAN.apply(this, []);
152   }, ERRORS.NA_ERROR);
153 });
154 
155@@ -346,10 +347,10 @@ test("TEXT", function(){
156     TEXT(0.99, "#0.00");
157   }, ERRORS.VALUE_ERROR);
158   catchAndAssertEquals(function() {
159-    TEXT.apply(this, [100])
160+    TEXT.apply(this, [100]);
161   }, ERRORS.NA_ERROR);
162   catchAndAssertEquals(function() {
163-    TEXT.apply(this, [100, "0", 10])
164+    TEXT.apply(this, [100, "0", 10]);
165   }, ERRORS.NA_ERROR);
166 });
167 
168@@ -371,7 +372,7 @@ test("FIND", function(){
169     FIND("m", "soup");
170   }, ERRORS.VALUE_ERROR);
171   catchAndAssertEquals(function() {
172-    FIND.apply(this, [2])
173+    FIND.apply(this, [2]);
174   }, ERRORS.NA_ERROR);
175   catchAndAssertEquals(function() {
176     FIND.apply(this, [2, 3, 4, 5])
177@@ -385,7 +386,7 @@ test("JOIN", function(){
178   assertEquals(JOIN("", [1, 2, 3, 4, 5]), "12345");
179   assertEquals(JOIN(true, [1, 2, 3, 4, 5]), "1TRUE2TRUE3TRUE4TRUE5");
180   catchAndAssertEquals(function() {
181-    JOIN.apply(this, [2])
182+    JOIN.apply(this, [2]);
183   }, ERRORS.NA_ERROR);
184 });
185 
186@@ -397,9 +398,29 @@ test("LEN", function(){
187   assertEquals(LEN(44), 2);
188   assertEquals(LEN(true), 4);
189   catchAndAssertEquals(function() {
190-    LEN.apply(this, [])
191+    LEN.apply(this, []);
192   }, ERRORS.NA_ERROR);
193   catchAndAssertEquals(function() {
194-    LEN.apply(this, [2, 4])
195+    LEN.apply(this, [2, 4]);
196+  }, ERRORS.NA_ERROR);
197+});
198+
199+test("LEFT", function(){
200+  assertEquals(LEFT("soup"), "s");
201+  assertEquals(LEFT("soup", 0), "");
202+  assertEquals(LEFT("soup", 1), "s");
203+  assertEquals(LEFT("soup", 2), "so");
204+  assertEquals(LEFT("soup", 3), "sou");
205+  assertEquals(LEFT("soup", 4), "soup");
206+  assertEquals(LEFT("soup", 5), "soup");
207+  assertEquals(LEFT("", 1000), "");
208+  catchAndAssertEquals(function() {
209+    LEFT("soup", -1);
210+  }, ERRORS.VALUE_ERROR);
211+  catchAndAssertEquals(function() {
212+    LEFT.apply(this, []);
213+  }, ERRORS.NA_ERROR);
214+  catchAndAssertEquals(function() {
215+    LEFT.apply(this, [1, 2, 3]);
216   }, ERRORS.NA_ERROR);
217 });
218diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
219index 66dcd8e..17af122 100644
220--- a/tests/SheetFormulaTest.ts
221+++ b/tests/SheetFormulaTest.ts
222@@ -1057,6 +1057,10 @@ test("Sheet LEN", function(){
223   assertFormulaEquals('=LEN("soup")', 4);
224 });
225 
226+test("Sheet LEFT", function(){
227+  assertFormulaEquals('=LEFT("soup")', "s");
228+});
229+
230 test("Sheet parsing error", function(){
231   assertFormulaEqualsError('= 10e', PARSE_ERROR);
232   assertFormulaEqualsError('= SUM(', PARSE_ERROR);