spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Text.RIGHT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-08 16:12:10
stats
8 file(s) changed, 86 insertions(+), 9 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 9bcb724..6976c42 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2436,7 +2436,17 @@
  6 
  7 ```
  8   Returns the first character or characters in a text string. 
  9-@param text - The text where the initial partial words are to be determined 
 10+@param text - The text where the initial partial words are to be determined. 
 11+@param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined, one character is returned. 
 12+@returns {string} 
 13+@constructor
 14+```
 15+
 16+### RIGHT 
 17+
 18+```
 19+  Defines the last character or characters in a text string. 
 20+@param text - The text where the initial partial words are to be determined. 
 21 @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined, one character is returned. 
 22 @returns {string} 
 23 @constructor
 24diff --git a/TODO.md b/TODO.md
 25index cb12bbf..f8fd717 100644
 26--- a/TODO.md
 27+++ b/TODO.md
 28@@ -47,7 +47,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 29 * REGEXREPLACE - May be difficult considering language differences.
 30 * REPLACE
 31 * REPT
 32-* RIGHT
 33 * SEARCH
 34 * SUBSTITUTE
 35 * VALUE
 36diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 37index f9d65ad..4185998 100644
 38--- a/dist/Formulas/AllFormulas.js
 39+++ b/dist/Formulas/AllFormulas.js
 40@@ -231,6 +231,7 @@ exports.FIND = Text_1.FIND;
 41 exports.JOIN = Text_1.JOIN;
 42 exports.LEN = Text_1.LEN;
 43 exports.LEFT = Text_1.LEFT;
 44+exports.RIGHT = Text_1.RIGHT;
 45 var Date_1 = require("./Date");
 46 exports.DATE = Date_1.DATE;
 47 exports.DATEVALUE = Date_1.DATEVALUE;
 48diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
 49index b0326be..3083c85 100644
 50--- a/dist/Formulas/Text.js
 51+++ b/dist/Formulas/Text.js
 52@@ -758,7 +758,7 @@ var LEN = function (value) {
 53 exports.LEN = LEN;
 54 /**
 55  * Returns the first character or characters in a text string.
 56- * @param text - The text where the initial partial words are to be determined
 57+ * @param text - The text where the initial partial words are to be determined.
 58  * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
 59  * one character is returned.
 60  * @returns {string}
 61@@ -775,3 +775,22 @@ var LEFT = function (text, numberOfCharacters) {
 62     return text.substring(0, numberOfCharacters);
 63 };
 64 exports.LEFT = LEFT;
 65+/**
 66+ * Defines the last character or characters in a text string.
 67+ * @param text - The text where the initial partial words are to be determined.
 68+ * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
 69+ * one character is returned.
 70+ * @returns {string}
 71+ * @constructor
 72+ */
 73+var RIGHT = function (text, numberOfCharacters) {
 74+    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "RIGHT");
 75+    text = TypeConverter_1.TypeConverter.firstValueAsString(text);
 76+    numberOfCharacters = MoreUtils_1.isUndefined(numberOfCharacters) ? 1 : numberOfCharacters;
 77+    if (numberOfCharacters < 0) {
 78+        throw new Errors_1.ValueError("Formula RIGHT parameter 2 value is " + numberOfCharacters
 79+            + ", but should be greater than or equal to 0.");
 80+    }
 81+    return text.substring(text.length - numberOfCharacters);
 82+};
 83+exports.RIGHT = RIGHT;
 84diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 85index 37ef2e7..b2ecbc5 100644
 86--- a/src/Formulas/AllFormulas.ts
 87+++ b/src/Formulas/AllFormulas.ts
 88@@ -238,7 +238,8 @@ import {
 89   FIND,
 90   JOIN,
 91   LEN,
 92-  LEFT
 93+  LEFT,
 94+  RIGHT
 95 } from "./Text"
 96 import {
 97   DATE,
 98@@ -537,5 +538,6 @@ export {
 99   FIND,
100   JOIN,
101   LEN,
102-  LEFT
103+  LEFT,
104+  RIGHT
105 }
106\ No newline at end of file
107diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
108index 40d9b00..8cbbf89 100644
109--- a/src/Formulas/Text.ts
110+++ b/src/Formulas/Text.ts
111@@ -790,7 +790,7 @@ let LEN = function (value) {
112 
113 /**
114  * Returns the first character or characters in a text string.
115- * @param text - The text where the initial partial words are to be determined
116+ * @param text - The text where the initial partial words are to be determined.
117  * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
118  * one character is returned.
119  * @returns {string}
120@@ -804,7 +804,26 @@ let LEFT = function (text, numberOfCharacters?) {
121     throw new ValueError("Formula LEFT parameter 2 value is " + numberOfCharacters
122         + ", but should be greater than or equal to 0.");
123   }
124-  return text.substring(0, numberOfCharacters)
125+  return text.substring(0, numberOfCharacters);
126+};
127+
128+/**
129+ * Defines the last character or characters in a text string.
130+ * @param text - The text where the initial partial words are to be determined.
131+ * @param numberOfCharacters [OPTIONAL] - The number of characters for the start text. If this parameter is not defined,
132+ * one character is returned.
133+ * @returns {string}
134+ * @constructor
135+ */
136+let RIGHT = function (text, numberOfCharacters?) {
137+  ArgsChecker.checkLengthWithin(arguments, 1, 2, "RIGHT");
138+  text = TypeConverter.firstValueAsString(text);
139+  numberOfCharacters = isUndefined(numberOfCharacters) ? 1 : numberOfCharacters;
140+  if (numberOfCharacters < 0) {
141+    throw new ValueError("Formula RIGHT parameter 2 value is " + numberOfCharacters
142+      + ", but should be greater than or equal to 0.");
143+  }
144+  return text.substring(text.length - numberOfCharacters);
145 };
146 
147 export {
148@@ -823,5 +842,6 @@ export {
149   FIND,
150   JOIN,
151   LEN,
152-  LEFT
153+  LEFT,
154+  RIGHT
155 }
156\ No newline at end of file
157diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
158index 1e36dcc..7391fcb 100644
159--- a/tests/Formulas/TextTest.ts
160+++ b/tests/Formulas/TextTest.ts
161@@ -14,7 +14,8 @@ import {
162   FIND,
163   JOIN,
164   LEN,
165-  LEFT
166+  LEFT,
167+  RIGHT
168 } from "../../src/Formulas/Text";
169 import * as ERRORS from "../../src/Errors";
170 import {
171@@ -424,3 +425,23 @@ test("LEFT", function(){
172     LEFT.apply(this, [1, 2, 3]);
173   }, ERRORS.NA_ERROR);
174 });
175+
176+test("RIGHT", function(){
177+  assertEquals(RIGHT("soup"), "p");
178+  assertEquals(RIGHT("soup", 0), "");
179+  assertEquals(RIGHT("soup", 1), "p");
180+  assertEquals(RIGHT("soup", 2), "up");
181+  assertEquals(RIGHT("soup", 3), "oup");
182+  assertEquals(RIGHT("soup", 4), "soup");
183+  assertEquals(RIGHT("soup", 5), "soup");
184+  assertEquals(RIGHT("", 1000), "");
185+  catchAndAssertEquals(function() {
186+    RIGHT("soup", -1);
187+  }, ERRORS.VALUE_ERROR);
188+  catchAndAssertEquals(function() {
189+    RIGHT.apply(this, []);
190+  }, ERRORS.NA_ERROR);
191+  catchAndAssertEquals(function() {
192+    RIGHT.apply(this, [1, 2, 3]);
193+  }, ERRORS.NA_ERROR);
194+});
195diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
196index 17af122..f209edc 100644
197--- a/tests/SheetFormulaTest.ts
198+++ b/tests/SheetFormulaTest.ts
199@@ -1061,6 +1061,10 @@ test("Sheet LEFT", function(){
200   assertFormulaEquals('=LEFT("soup")', "s");
201 });
202 
203+test("Sheet RIGHT", function(){
204+  assertFormulaEquals('=RIGHT("soup")', "p");
205+});
206+
207 test("Sheet parsing error", function(){
208   assertFormulaEqualsError('= 10e', PARSE_ERROR);
209   assertFormulaEqualsError('= SUM(', PARSE_ERROR);