spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Text.MID] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-10 00:48:49
stats
8 file(s) changed, 96 insertions(+), 6 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 62f8a56..6c92509 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2488,3 +2488,14 @@
  6 @returns {string} 
  7 @constructor
  8 ```
  9+
 10+### MID 
 11+
 12+```
 13+  Returns a text segment of a character string. The parameters specify the starting position and the number of characters. 
 14+@param text - The text containing the characters to extract. 
 15+@param start - The position of the first character in the text to extract. 
 16+@param number - The number of characters in the part of the text. 
 17+@returns {string} 
 18+@constructor
 19+```
 20diff --git a/TODO.md b/TODO.md
 21index e8553a7..05a283f 100644
 22--- a/TODO.md
 23+++ b/TODO.md
 24@@ -43,7 +43,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 25 
 26 
 27 ### Easy formulas to write
 28-* MID
 29 * PROPER
 30 * REGEXEXTRACT - May be difficult considering language differences.
 31 * REGEXMATCH - May be difficult considering language differences.
 32diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 33index e1d2c7a..9a25942 100644
 34--- a/dist/Formulas/AllFormulas.js
 35+++ b/dist/Formulas/AllFormulas.js
 36@@ -236,6 +236,7 @@ exports.SEARCH = Text_1.SEARCH;
 37 exports.REPT = Text_1.REPT;
 38 exports.VALUE = Text_1.VALUE;
 39 exports.CLEAN = Text_1.CLEAN;
 40+exports.MID = Text_1.MID;
 41 var Date_1 = require("./Date");
 42 exports.DATE = Date_1.DATE;
 43 exports.DATEVALUE = Date_1.DATEVALUE;
 44diff --git a/dist/Formulas/Text.js b/dist/Formulas/Text.js
 45index 8eb403f..e3d1aa0 100644
 46--- a/dist/Formulas/Text.js
 47+++ b/dist/Formulas/Text.js
 48@@ -862,3 +862,29 @@ var CLEAN = function (text) {
 49     return text.replace(/[\0-\x1F]/g, "");
 50 };
 51 exports.CLEAN = CLEAN;
 52+/**
 53+ * Returns a text segment of a character string. The parameters specify the starting position and the number of
 54+ * characters.
 55+ * @param text - The text containing the characters to extract.
 56+ * @param start - The position of the first character in the text to extract.
 57+ * @param number - The number of characters in the part of the text.
 58+ * @returns {string}
 59+ * @constructor
 60+ */
 61+var MID = function (text, start, number) {
 62+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 3, "MID");
 63+    text = TypeConverter_1.TypeConverter.firstValueAsString(text);
 64+    start = TypeConverter_1.TypeConverter.firstValueAsNumber(start);
 65+    number = TypeConverter_1.TypeConverter.firstValueAsNumber(number);
 66+    if (number === 0) {
 67+        return "";
 68+    }
 69+    if (number < 0) {
 70+        throw new Errors_1.ValueError("MID parameter 3 value is " + number + ", but should be greater than or equal to 0.");
 71+    }
 72+    if (start < 1) {
 73+        throw new Errors_1.NumError("Function MID parameter 2 value is " + start + ", but should be greater than or equal to 1.");
 74+    }
 75+    return text.substring(start - 1, start + number - 1);
 76+};
 77+exports.MID = MID;
 78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 79index 33620e6..d620625 100644
 80--- a/src/Formulas/AllFormulas.ts
 81+++ b/src/Formulas/AllFormulas.ts
 82@@ -243,7 +243,8 @@ import {
 83   SEARCH,
 84   REPT,
 85   VALUE,
 86-  CLEAN
 87+  CLEAN,
 88+  MID
 89 } from "./Text"
 90 import {
 91   DATE,
 92@@ -547,5 +548,6 @@ export {
 93   SEARCH,
 94   REPT,
 95   VALUE,
 96-  CLEAN
 97+  CLEAN,
 98+  MID
 99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
102index 9a6953d..4de1a97 100644
103--- a/src/Formulas/Text.ts
104+++ b/src/Formulas/Text.ts
105@@ -894,6 +894,32 @@ let CLEAN = function (text) {
106   return text.replace(/[\0-\x1F]/g, "");
107 };
108 
109+/**
110+ * Returns a text segment of a character string. The parameters specify the starting position and the number of
111+ * characters.
112+ * @param text - The text containing the characters to extract.
113+ * @param start - The position of the first character in the text to extract.
114+ * @param number - The number of characters in the part of the text.
115+ * @returns {string}
116+ * @constructor
117+ */
118+let MID = function (text, start, number) {
119+  ArgsChecker.checkLength(arguments, 3, "MID");
120+  text = TypeConverter.firstValueAsString(text);
121+  start = TypeConverter.firstValueAsNumber(start);
122+  number = TypeConverter.firstValueAsNumber(number);
123+  if (number === 0) {
124+    return "";
125+  }
126+  if (number < 0) {
127+    throw new ValueError("MID parameter 3 value is " + number + ", but should be greater than or equal to 0.");
128+  }
129+  if (start < 1) {
130+    throw new NumError("Function MID parameter 2 value is " + start + ", but should be greater than or equal to 1.");
131+  }
132+  return text.substring(start - 1, start + number - 1);
133+};
134+
135 export {
136   ARABIC,
137   CHAR,
138@@ -915,5 +941,6 @@ export {
139   SEARCH,
140   REPT,
141   VALUE,
142-  CLEAN
143+  CLEAN,
144+  MID
145 }
146\ No newline at end of file
147diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
148index f8073f9..e46266a 100644
149--- a/tests/Formulas/TextTest.ts
150+++ b/tests/Formulas/TextTest.ts
151@@ -19,7 +19,8 @@ import {
152   SEARCH,
153   REPT,
154   VALUE,
155-  CLEAN
156+  CLEAN,
157+  MID
158 } from "../../src/Formulas/Text";
159 import * as ERRORS from "../../src/Errors";
160 import {
161@@ -518,3 +519,22 @@ test("CLEAN", function(){
162     CLEAN.apply(this, [1, 2]);
163   }, ERRORS.NA_ERROR);
164 });
165+
166+test("MID", function(){
167+  assertEquals(MID("hey there", 5, 4), "ther");
168+  assertEquals(MID("hey there", 5, 1), "t");
169+  assertEquals(MID("hey there", 5, 0), "");
170+  assertEquals(MID("hey there", 50, 10), "");
171+  catchAndAssertEquals(function () {
172+    MID("", 1, -1);
173+  }, ERRORS.VALUE_ERROR);
174+  catchAndAssertEquals(function () {
175+    MID("", 0, 1);
176+  }, ERRORS.NUM_ERROR);
177+  catchAndAssertEquals(function () {
178+    MID.apply(this, [1, 2, 3, 4]);
179+  }, ERRORS.NA_ERROR);
180+  catchAndAssertEquals(function () {
181+    MID.apply(this, [1, 2]);
182+  }, ERRORS.NA_ERROR);
183+});
184diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
185index 001e768..899d77c 100644
186--- a/tests/SheetFormulaTest.ts
187+++ b/tests/SheetFormulaTest.ts
188@@ -1081,6 +1081,10 @@ test("Sheet CLEAN", function(){
189   assertFormulaEquals('=CLEAN("hello"&CHAR(31))', "hello");
190 });
191 
192+test("Sheet MID", function(){
193+  assertFormulaEquals('=MID("hey there", 5, 4)', "ther");
194+});
195+
196 test("Sheet parsing error", function(){
197   assertFormulaEqualsError('= 10e', PARSE_ERROR);
198   assertFormulaEqualsError('= SUM(', PARSE_ERROR);