spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[ROMAN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-09-24 16:56:15
stats
5 file(s) changed, 85 insertions(+), 3 deletions(-)
files
TODO.md
src/Formulas/AllFormulas.ts
src/Formulas/Text.ts
tests/Formulas/TextTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/TODO.md b/TODO.md
  2index 53c5ab5..e4cb46c 100644
  3--- a/TODO.md
  4+++ b/TODO.md
  5@@ -9,6 +9,10 @@ Instead of having non-primitives, (i.e. Date, DateTime, Time, Dollar), cells sho
  6 For example 64 tbs to a qt.
  7 
  8 
  9+### Range literals should be allow to follow commas
 10+Currently, this `=SERIESSUM([1], [0], [1], [4, 5, 6])` parses, but this `=SERIESSUM(1, 0, 1, [4, 5, 6])` does not.
 11+
 12+
 13 ### Parser/Sheet should be able to be initialized with js range notation (`[]`) or regular range notation (`{}`)
 14 
 15 
 16diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 17index c0d5276..45323ab 100644
 18--- a/src/Formulas/AllFormulas.ts
 19+++ b/src/Formulas/AllFormulas.ts
 20@@ -226,7 +226,8 @@ import {
 21   TRIM,
 22   LOWER,
 23   UPPER,
 24-  T
 25+  T,
 26+  ROMAN
 27 } from "./Text"
 28 import {
 29   DATE,
 30@@ -513,5 +514,6 @@ export {
 31   ADDRESS,
 32   COLUMNS,
 33   ROWS,
 34-  SERIESSUM
 35+  SERIESSUM,
 36+  ROMAN
 37 }
 38\ No newline at end of file
 39diff --git a/src/Formulas/Text.ts b/src/Formulas/Text.ts
 40index df822bb..d960e89 100644
 41--- a/src/Formulas/Text.ts
 42+++ b/src/Formulas/Text.ts
 43@@ -477,6 +477,61 @@ let T = function (value) {
 44   return "";
 45 };
 46 
 47+/**
 48+ * Converts a number into a Roman numeral.
 49+ * @param value - The value to convert. Must be between 0 and 3999.
 50+ * @constructor
 51+ * TODO: Second parameter should be 'rule_relaxation'
 52+ */
 53+let ROMAN = function (value) {
 54+  ArgsChecker.checkLength(arguments, 1, "ROMAN");
 55+  value = TypeConverter.firstValueAsNumber(value);
 56+  if (value < 1 || value > 3999) {
 57+    throw new ValueError("Function ROMAN parameter 1 value is " + value
 58+        + ", while valid values are between 1 and 3999 inclusive.");
 59+  }
 60+  // The MIT License
 61+  // Copyright (c) 2008 Steven Levithan
 62+  let digits = String(value).split('');
 63+  let key = ['',
 64+    'C',
 65+    'CC',
 66+    'CCC',
 67+    'CD',
 68+    'D',
 69+    'DC',
 70+    'DCC',
 71+    'DCCC',
 72+    'CM',
 73+    '',
 74+    'X',
 75+    'XX',
 76+    'XXX',
 77+    'XL',
 78+    'L',
 79+    'LX',
 80+    'LXX',
 81+    'LXXX',
 82+    'XC',
 83+    '',
 84+    'I',
 85+    'II',
 86+    'III',
 87+    'IV',
 88+    'V',
 89+    'VI',
 90+    'VII',
 91+    'VIII',
 92+    'IX'
 93+  ];
 94+  let roman = '';
 95+  let i = 3;
 96+  while (i--) {
 97+    roman = (key[+digits.pop() + (i * 10)] || '') + roman;
 98+  }
 99+  return new Array(+digits.join('') + 1).join('M') + roman;
100+};
101+
102 export {
103   ARABIC,
104   CHAR,
105@@ -487,5 +542,6 @@ export {
106   TRIM,
107   LOWER,
108   UPPER,
109-  T
110+  T,
111+  ROMAN
112 }
113\ No newline at end of file
114diff --git a/tests/Formulas/TextTest.ts b/tests/Formulas/TextTest.ts
115index de81d38..467a39a 100644
116--- a/tests/Formulas/TextTest.ts
117+++ b/tests/Formulas/TextTest.ts
118@@ -8,7 +8,8 @@ import {
119   TRIM,
120   LOWER,
121   UPPER,
122-  T
123+  T,
124+  ROMAN
125 } from "../../src/Formulas/Text";
126 import * as ERRORS from "../../src/Errors";
127 import {
128@@ -188,3 +189,13 @@ test("T", function(){
129     T.apply(this, []);
130   }, ERRORS.NA_ERROR);
131 });
132+
133+test("ROMAN", function(){
134+  assertEquals(ROMAN(419), "CDXIX");
135+  assertEquals(ROMAN(1), "I");
136+  assertEquals(ROMAN(3999), "MMMCMXCIX");
137+  assertEquals(ROMAN(300), "CCC");
138+  catchAndAssertEquals(function() {
139+    ROMAN(0);
140+  }, ERRORS.VALUE_ERROR);
141+});
142diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
143index 263ad75..600804f 100644
144--- a/tests/SheetFormulaTest.ts
145+++ b/tests/SheetFormulaTest.ts
146@@ -1009,8 +1009,12 @@ test("Sheet ROWS", function(){
147   assertFormulaEquals('=ROWS(B1:M44)', 44);
148 });
149 
150-test("Sheet SERIESSUM", function(){
151-  // assertFormulaEquals('=SERIESSUM(1, 0, 1, [4, 5, 6])', 15);
152+test("Sheet SERIESSUM", function() {
153+  assertFormulaEquals('=SERIESSUM([1], [0], [1], [4, 5, 6])', 15);
154+});
155+
156+test("Sheet ROMAN", function(){
157+  assertFormulaEquals('=ROMAN(3999)', "MMMCMXCIX");
158 });
159 
160 test("Sheet parsing error", function(){