Permalink
Browse files

range breakout function is working, still need to get to the bottom o…

…f why the results are not rendering.
  • Loading branch information...
1 parent ef86234 commit 7ca236557e71f09767b575ac752b1242744cc172 @joshbtn committed Nov 12, 2012
Showing with 48 additions and 15 deletions.
  1. +44 −15 src/ExcelFormulaUtilities.js
  2. +4 −0 test/ExcelFormulaUtilities.test.js
@@ -621,22 +621,42 @@
function breakOutRanges(rangeStr, delimStr){
//Quick Check to see if if rangeStr is a valid range
- if ( (/\w{1,4}\d+:\w{1,4}\d+/gi).test("A1:B2") ){
+ if ( !RegExp("[a-z]+[0-9]+:[a-z]+[0-9]+","gi").test(rangeStr) ){
throw "This is not a valid range: " + rangeStr;
}
//Make the rangeStr lowercase to deal with looping.
- ranges = rangeStr.split(":");
+ var range = rangeStr.split(":"),
- startRow = range[0].match(/[0-9]+/gi)[0];
- startCol = range[0].match(/[A-Z]+/gi)[0];
+ startRow = range[0].match(/[0-9]+/gi)[0],
+ startCol = range[0].match(/[A-Z]+/gi)[0],
- endRow = range[1].match(/[0-9]+/gi)[0];
- endCol = range[1].match(/[A-Z]+/gi)[0];
+ endRow = range[1].match(/[0-9]+/gi)[0],
+ endCol = range[1].match(/[A-Z]+/gi)[0],
+
+ // Total rows and cols
+ totalRows = endRow - startRow + 1,
+ totalCols = fromBase26(endCol) - fromBase26(startCol) + 1,
+
+ // Loop vars
+ curCol = 0,
+ curRow = 1,
+ curCell = "",
+
+ //Return String
+ retStr = "";
+
+ for(; curRow <= totalRows; curRow+=1){
+ for(; curCol < totalCols; curCol+=1){
+ // Get the current cell id
+ curCell = toBase26(curCol) + curRow ;
+ retStr += curCell + (curRow===totalRows && curCol===totalCols-1 ? "" : delimStr);
+ }
+ curCol=0;
+ }
+
+ return retStr;
- //str.charCodeAt(n)
- //String.fromCharCode();
- return "";
}
//Modified from function at http://en.wikipedia.org/wiki/Hexavigesimal
@@ -1064,15 +1084,24 @@
switch (currentFunctionOnStack.name.toLowerCase()) {
// If in the sum function break aout cell names and add
case "sum":
-
- outstr = breakOutRanges(tokenString, "+");
-
- debugger;
- //TODO loop through and add ranges together
+ //TODO make sure this is working
+ if(RegExp(":","gi").test(tokenString)){
+ debugger;
+ outstr = breakOutRanges(tokenString, "+");
+ } else {
+ outStr = tokenString;
+ }
+
break;
// By Default return an array containing all cell names in array
default:
- //TODO create array for ranges
+ // Create array for ranges
+ if(RegExp(":","gi").test(tokenString)){
+ outstr = "[" + breakOutRanges(tokenString, ",") +"]";
+ } else {
+ outstr = tokenString;
+ }
+
break;
}
@@ -81,4 +81,8 @@ test("formula2JavaScript", function() {
inputFormula = 'SUM(A1:C3)';
excpected = '(A1+A2+A3+B1+B2+B3+C1+C2+C3)';
equal(excelFormulaUtilities.formula2JavaScript(inputFormula), excpected, "Make sure the sum of ranges break out, See issue #6 https://github.com/joshatjben/excelFormulaUtilitiesJS/issues/6");
+
+ inputFormula = 'SUM(A1:B3)';
+ excpected = '[A1,B1,A2,B2,A3,B3]';
+ equal(excelFormulaUtilities.formula2JavaScript(inputFormula), excpected, "Make sure the sum of ranges break out, for non sum function");
});

0 comments on commit 7ca2365

Please sign in to comment.