spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Text.REPLACE] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-12-01 04:15:41
stats
9 file(s) changed, 101 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/MathTest.ts
tests/Formulas/TextTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 6c92509..5777d37 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2499,3 +2499,14 @@
  6 @returns {string} 
  7 @constructor
  8 ```
  9+
 10+### REPLACE 
 11+
 12+```
 13+  Replaces part of a text string with a different text string. This function can be used to replace both characters and numbers (which are automatically converted to text). The result of the function is always displayed as text. 
 14+@param text - The text of which a part will be replaced. 
 15+@param position - The position within the text where the replacement will begin. 
 16+@param length - The number of characters in text to be replaced. 
 17+@param newText - The text which replaces text. 
 18+@constructor
 19+```
 20diff --git a/TODO.md b/TODO.md
 21index d130719..c608205 100644
 22--- a/TODO.md
 23+++ b/TODO.md
 24@@ -46,7 +46,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 25 * REGEXEXTRACT - May be difficult considering language differences.
 26 * REGEXMATCH - May be difficult considering language differences.
 27 * REGEXREPLACE - May be difficult considering language differences.
 28-* REPLACE
 29 * SUBSTITUTE
 30 
 31 ### Other formulas to write
 32diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 33index c45d66e..0a7ac0d 100644
 34--- a/dist/Formulas/AllFormulas.js
 35+++ b/dist/Formulas/AllFormulas.js
 36@@ -238,6 +238,7 @@ exports.VALUE = Text_1.VALUE;
 37 exports.CLEAN = Text_1.CLEAN;
 38 exports.MID = Text_1.MID;
 39 exports.PROPER = Text_1.PROPER;
 40+exports.REPLACE = Text_1.REPLACE;
 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 e45322b..d1401d0 100644
 46--- a/dist/Formulas/Text.js
 47+++ b/dist/Formulas/Text.js
 48@@ -896,3 +896,29 @@ var PROPER = function (text) {
 49     });
 50 };
 51 exports.PROPER = PROPER;
 52+/**
 53+ * Replaces part of a text string with a different text string. This function can be used to replace both characters and
 54+ * numbers (which are automatically converted to text). The result of the function is always displayed as text.
 55+ * @param text - The text of which a part will be replaced.
 56+ * @param position - The position within the text where the replacement will begin.
 57+ * @param length - The number of characters in text to be replaced.
 58+ * @param newText - The text which replaces text.
 59+ * @constructor
 60+ */
 61+var REPLACE = function (text, position, length, newText) {
 62+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 4, "REPLACE");
 63+    text = TypeConverter_1.TypeConverter.firstValueAsString(text);
 64+    position = TypeConverter_1.TypeConverter.firstValueAsNumber(position);
 65+    if (position < 1) {
 66+        throw new Errors_1.ValueError("Function REPLACE parameter 2 value is " + position
 67+            + ", but should be greater than or equal to 1.");
 68+    }
 69+    length = TypeConverter_1.TypeConverter.firstValueAsNumber(length);
 70+    if (length < 0) {
 71+        throw new Errors_1.ValueError("Function REPLACE parameter 3 value is " + length
 72+            + ", but should be greater than or equal to 1.");
 73+    }
 74+    newText = TypeConverter_1.TypeConverter.firstValueAsString(newText);
 75+    return text.substr(0, position - 1) + newText + text.substr(position - 1 + length);
 76+};
 77+exports.REPLACE = REPLACE;
 78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 79index e266c8a..cdc6fc1 100644
 80--- a/src/Formulas/AllFormulas.ts
 81+++ b/src/Formulas/AllFormulas.ts
 82@@ -245,7 +245,8 @@ import {
 83   VALUE,
 84   CLEAN,
 85   MID,
 86-  PROPER
 87+  PROPER,
 88+  REPLACE
 89 } from "./Text"
 90 import {
 91   DATE,
 92@@ -551,5 +552,6 @@ export {
 93   VALUE,
 94   CLEAN,
 95   MID,
 96-  PROPER
 97+  PROPER,
 98+  REPLACE
 99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
102index 957d506..a1f1e96 100644
103--- a/src/Formulas/Text.ts
104+++ b/src/Formulas/Text.ts
105@@ -929,6 +929,33 @@ let PROPER = function (text) {
106   });
107 };
108 
109+
110+/**
111+ * Replaces part of a text string with a different text string. This function can be used to replace both characters and
112+ * numbers (which are automatically converted to text). The result of the function is always displayed as text.
113+ * @param text - The text of which a part will be replaced.
114+ * @param position - The position within the text where the replacement will begin.
115+ * @param length - The number of characters in text to be replaced.
116+ * @param newText - The text which replaces text.
117+ * @constructor
118+ */
119+let REPLACE = function (text, position, length, newText) {
120+  ArgsChecker.checkLength(arguments, 4, "REPLACE");
121+  text = TypeConverter.firstValueAsString(text);
122+  position = TypeConverter.firstValueAsNumber(position);
123+  if (position < 1) {
124+    throw new ValueError("Function REPLACE parameter 2 value is " + position
125+        + ", but should be greater than or equal to 1.");
126+  }
127+  length = TypeConverter.firstValueAsNumber(length);
128+  if (length < 0) {
129+    throw new ValueError("Function REPLACE parameter 3 value is " + length
130+      + ", but should be greater than or equal to 1.");
131+  }
132+  newText = TypeConverter.firstValueAsString(newText);
133+  return text.substr(0, position - 1) + newText + text.substr(position - 1 + length);
134+};
135+
136 export {
137   ARABIC,
138   CHAR,
139@@ -952,5 +979,6 @@ export {
140   VALUE,
141   CLEAN,
142   MID,
143-  PROPER
144+  PROPER,
145+  REPLACE
146 }
147\ No newline at end of file
148diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
149index a76f85b..ce89520 100644
150--- a/tests/Formulas/MathTest.ts
151+++ b/tests/Formulas/MathTest.ts
152@@ -1264,7 +1264,7 @@ test("TANH", function(){
153   assertEquals(TANH(0), 0);
154   assertEquals(TANH(1), 0.7615941559557649);
155   assertEquals(TANH(PI() / 2), 0.9171523356672744);
156-  assertEquals(TANH(PI()), 0.9962720762207501);
157+  assertEquals(TANH(PI()), 0.99627207622075);
158   assertEquals(TANH(false), 0);
159   assertEquals(TANH(true), 0.7615941559557649);
160   assertEquals(TANH(""), 0);
161diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
162index f5c98e5..dc8ebc1 100644
163--- a/tests/Formulas/TextTest.ts
164+++ b/tests/Formulas/TextTest.ts
165@@ -21,7 +21,8 @@ import {
166   VALUE,
167   CLEAN,
168   MID,
169-  PROPER
170+  PROPER,
171+  REPLACE
172 } from "../../src/Formulas/Text";
173 import * as ERRORS from "../../src/Errors";
174 import {
175@@ -554,3 +555,22 @@ test("PROPER", function(){
176     PROPER.apply(this, [1, 2]);
177   }, ERRORS.NA_ERROR);
178 });
179+
180+test("PROPER", function(){
181+  assertEquals(REPLACE("Hey there", 1, 3, "Hello"), "Hello there");
182+  assertEquals(REPLACE("Hey there", 2, 1, "Hello"), "HHelloy there");
183+  assertEquals(REPLACE("Hey there", 1, 1100, "Hello"), "Hello");
184+  assertEquals(REPLACE("Hey", 10, 11, "Hello"), "HeyHello");
185+  catchAndAssertEquals(function () {
186+    REPLACE.apply(this, ["Hey there", 1, 1]);
187+  }, ERRORS.NA_ERROR);
188+  catchAndAssertEquals(function () {
189+    REPLACE.apply(this, ["Hey there", 1, 1, "replace me", "me too!"]);
190+  }, ERRORS.NA_ERROR);
191+  catchAndAssertEquals(function () {
192+    REPLACE("Hey there", 0, 3, "Hello")
193+  }, ERRORS.VALUE_ERROR);
194+  catchAndAssertEquals(function () {
195+    REPLACE("Hey there", 1, -1, "Hello")
196+  }, ERRORS.VALUE_ERROR);
197+});
198diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
199index 65d1f1d..1d943f0 100644
200--- a/tests/SheetFormulaTest.ts
201+++ b/tests/SheetFormulaTest.ts
202@@ -152,7 +152,7 @@ test("Sheet COT", function(){
203 });
204 
205 test("Sheet COTH", function(){
206-  assertFormulaEquals('=COTH(2)', 1.037314720727548);
207+  assertFormulaEquals('=COTH(2)', 1.0373147207275482);
208 });
209 
210 test("Sheet COUNT", function(){
211@@ -548,7 +548,7 @@ test("Sheet TAN", function(){
212 });
213 
214 test("Sheet TANH", function(){
215-  assertFormulaEquals('=TANH(PI())', 0.9962720762207501);
216+  assertFormulaEquals('=TANH(PI())', 0.99627207622075);
217 });
218 
219 test("Sheet TRUE", function(){
220@@ -1089,6 +1089,10 @@ test("Sheet PROPER", function(){
221   assertFormulaEquals('=PROPER("hey there")', "Hey There");
222 });
223 
224+test("Sheet PROPER", function(){
225+  assertFormulaEquals('=REPLACE("Hey there", 1, 3, "Hello")', "Hello there");
226+});
227+
228 test("Sheet parsing error", function(){
229   assertFormulaEqualsError('= 10e', PARSE_ERROR);
230   assertFormulaEqualsError('= SUM(', PARSE_ERROR);