Skip to content

Commit

Permalink
merged rangeHandler, ranges broken for now
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbtn committed Oct 4, 2012
2 parents 8b93a98 + 4e6a72c commit cdffc4f
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 83 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -1 +1,5 @@
<<<<<<< HEAD
node_modules
=======
.c9revisions
>>>>>>> rangeHandler
85 changes: 85 additions & 0 deletions beautifier.js
@@ -0,0 +1,85 @@
//Beautifier.js
//
//Copywrite 2011 Josh Benentt
//License - https://raw.github.com/joshatjben/excelFormulaUtilitiesJS/master/LICENSE.txt
//[on github](https://github.com/joshatjben/excelFormulaUtilitiesJS/tree/master/examples/basic_example1 "github")
//
(function (window, undefiend) {
"use strict";

//Check and setup name spaces.
window.excelFormulaBeautifier = window.excelFormulaBeautifier || {};
window.excelFormulaBeautifier.examples = window.excelFormulaBeautifier.examples || {};

//Configuration
//-------------------------------
var config = {
//The ID for the formula Input input/textarea
INPUT_ID: 'formula_input',

//The ID for the formula title area. in this example it spits out the function call;
FORMULA_TITLE_ID: 'fomatFormula_2',

//THE ID for the area to contain the beautified excel formula.
FORMULA_BODY_ID:'fomatFormula_2_out',

//Use this to set the inital textare/input text area.
DEFAULT_FORMULA: ''
},


//Beautifier Page functionality
//-------------------------------
beautifier = window.excelFormulaBeautifier.examples.beautifier =
(function () {
var oldFormula;

return {
formula: '=IF(SUM( If(FOO = BAR, 10, 0), 10 ) = 20 , "FOO", "BAR")',
input: null,
formulaTitle: null,
formulaBody: null,
mode: "beautify",
changeMode: function(mode){
window.excelFormulaBeautifier.examples.beautifier.mode = mode;
window.excelFormulaBeautifier.examples.beautifier.update.call(window.excelFormulaBeautifier.examples.beautifier);
},
update: function () {
this.formula = this.input.value;

//Test to see if the formula has changed, if it hasn't don't do anything
if (oldFormula === this.formula) {
return;
}

// Check to see which mode we're in, render appropriately
try{

switch( this.mode ) {
case "beautify":
this.formulaBody.innerHTML = window.excelFormulaUtilities.formatFormulaHTML(this.formula);
break;
case "js":
this.formulaBody.innerHTML = window.excelFormulaUtilities.formula2JavaScript(this.formula);
break;
}
}catch(exception){
//Do nothing, This should throw an error when the formula is improperly formed, which shouldn't blow things up.
}
}
};
}());

//On Page Load
//-------------------
window.onload = function () {
beautifier.input = document.getElementById(config.INPUT_ID);
//beautifier.formulaTitle = document.getElementById(config.FORMULA_TITLE_ID);
beautifier.formulaBody = document.getElementById(config.FORMULA_BODY_ID);

beautifier.input.value = beautifier.formula;
beautifier.update();
//add beautifier.update(); here if if you have set an inital DEFAULT_FORMULA and would like it to render on page load.
};

}(window));
162 changes: 89 additions & 73 deletions index.html
@@ -1,95 +1,111 @@
<!doctype html>
<head>
<meta charset="utf-8">
<title>ExcelFormulaUtilities.js - Online Excel Formula Beautifier</title>

<meta name="description" content="Online Excel Formula Beautifier">
<meta name="author" content="Josh Bennett">
<title>Test - Online Excel Formula Beautifier</title>

<style type="text/css">/*<![CDATA[*/
html, body{font-size:12px; font-family:"Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif;}
body{margin:0.618em;}
footer{display:none;}
#main .formula{
margin:0;
padding:0;
margin-bottom:0.61em;
}
#main .formula pre, #main .formula div{
margin:0;
padding:0.61em;
margin-bottom:1.618em;
background: #ffffff; /* Old browsers */
background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#e5e5e5',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* W3C */
}
#main .formula h2{
font-weight:700;
margin:0;
padding:0.61em;
font-size:12px;
color:#fff;
background: #b8e1fc; /* Old browsers */
background: -moz-linear-gradient(top, #b8e1fc 0%, #a9d2f3 10%, #90bae4 25%, #90bcea 37%, #90bff0 50%, #6ba8e5 51%, #a2daf5 83%, #bdf3fd 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#b8e1fc), color-stop(10%,#a9d2f3), color-stop(25%,#90bae4), color-stop(37%,#90bcea), color-stop(50%,#90bff0), color-stop(51%,#6ba8e5), color-stop(83%,#a2daf5), color-stop(100%,#bdf3fd)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #b8e1fc 0%,#a9d2f3 10%,#90bae4 25%,#90bcea 37%,#90bff0 50%,#6ba8e5 51%,#a2daf5 83%,#bdf3fd 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #b8e1fc 0%,#a9d2f3 10%,#90bae4 25%,#90bcea 37%,#90bff0 50%,#6ba8e5 51%,#a2daf5 83%,#bdf3fd 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #b8e1fc 0%,#a9d2f3 10%,#90bae4 25%,#90bcea 37%,#90bff0 50%,#6ba8e5 51%,#a2daf5 83%,#bdf3fd 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b8e1fc', endColorstr='#bdf3fd',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #b8e1fc 0%,#a9d2f3 10%,#90bae4 25%,#90bcea 37%,#90bff0 50%,#6ba8e5 51%,#a2daf5 83%,#bdf3fd 100%); /* W3C */
}
#main .formula h2 span{
font-weight:normal;
}
#inputArea{
margin:1.618em 0;
}
#formula_input{width:500px;}
/*-----------FORMULA STYLES------------*/
.function{
color:#069;
}
.function_start, .function_stop{
color:#ff0000;
}
.tabs{border-left:dotted 1px #a0a0a0}
.quote_mark, .text{color:#0000ff}
/*-----------FORMULA STYLES------------*/
#formula_input{width:90%}
/*]]>*/</style>


<!--[if gte IE 9]>
<style type="text/css">
.gradient {
filter: none;
}
</style>
<![endif]-->
</head>

<body>

<div id="container">
<header>
<h1>Online Excel Formula Beautifier (Beta)</h1>
</header>

<div id="main" role="main">
<!-- Side Content -->
<div id="sideContent" class="span-6 last">
<p>
<label class="span-2" for="mode">Mode: </label>
<div class="span-4 last">
<select id="mode" class="span-4 last" onchange="window.beautifier.changeMode_update(this.value)">
<option value="beautify">Beautify</option>
<option value="js" selected="selected">Formula to JavaScript</option>
</select>
</div>
</p>
</div>

<!-- Main Content -->
<div id="container" class="span-12 colborder">

<div id="main" role="main" class="span-12 last">
<div id="inputArea">
<label for="formula_input">Start Typing formula here.</label><br />
<input type="text" id="formula_input" onkeyup="window.excelFormulaBeautifier.examples.beautifier.update()" />
<label for="formula_input">Enter your formula here. The leading = is optional.</label><br />
<input type="text" id="formula_input" onkeyup="window.beautifier.txtFormula_Update()" />
</div>
<div class="formula">
<h2>excelFormulaUtilities.formatFormulaHTML( "<span id="fomatFormula_2"></span>" );</h2>
<code id="fomatFormula_2_out"></code>
<!--h2>excelFormulaUtilities.formatFormulaHTML( "<span id="fomatFormula_2"></span>" );</h2-->
<pre id="fomatFormula_2_out"></pre>
</div>

</div>

<footer>
&copy; copyright 2011 Josh Bennett
</footer>
</div>



<script src="js/excelFormulaUtilities-0.9.1.min.js"></script>
<script src="js/beautifier.js"></script>
<script src="src/core.js"></script>
<script src="src/ExcelFormulaUtilities.js"></script>

<script type="text/javascript">
/*<![CDATA[*/
(function(){
var MODE_BEAUTIFY = "beautify"
,MODE_JS = "js"
, txtFormula
, formulaOut
, selMode
, trim = window.excelFormulaUtilities.string.trim;

window.beautifier = {}

// Convert the input fields text to the out
function convertFormula( inputElement, outputElement, mode ) {
switch(mode){
case MODE_BEAUTIFY:
try{
outputElement.innerHTML = excelFormulaUtilities.formatFormula( trim(inputElement.value) );
outputElement.style.background = "transparent";
} catch (ex){
outputElement.style.background = "red";
}
break;
case MODE_JS:
try{
outputElement.innerHTML = excelFormulaUtilities.formula2JavaScript( trim(inputElement.value) );
outputElement.style.background = "transparent";
} catch (ex){
outputElement.style.background = "red";
}
break;
}
}

window.onload = function(){

txtFormula = document.getElementById('formula_input'),
formulaOut = document.getElementById('fomatFormula_2_out');
selMode = document.getElementById('mode');

// Handle when the formula input changes value
window.beautifier.txtFormula_Update = function(){
convertFormula(txtFormula, formulaOut, selMode.value);
}

// Handle when the mode input changes value
window.beautifier.changeMode_update = function(){

}
}
}());
/*]]>*/
</script>

</body>
</html>
22 changes: 22 additions & 0 deletions src/ExcelFormulaUtilities.js
Expand Up @@ -16,6 +16,7 @@
if (typeof window === 'undefined') {
window = root;
}

var excelFormulaUtilities = window.excelFormulaUtilities = window.excelFormulaUtilities || {},
core = window.excelFormulaUtilities.core,
formatStr = window.excelFormulaUtilities.string.formatStr,
Expand Down Expand Up @@ -634,6 +635,7 @@

//str.charCodeAt(n)
//String.fromCharCode();
return "";
}

//Modified from function at http://en.wikipedia.org/wiki/Hexavigesimal
Expand Down Expand Up @@ -772,10 +774,14 @@
default:

break;
<<<<<<< HEAD

}


=======
}
>>>>>>> rangeHandler
return tokenString;
};

Expand Down Expand Up @@ -1021,10 +1027,17 @@
switch (currentFunctionOnStack.argumentNumber) {
case 0:
outstr = "?";
<<<<<<< HEAD
break;
case 1:
outstr = ":";
break;
=======
break;
case 1:
outstr = ":";
break;
>>>>>>> rangeHandler
}
break;
case "sum":
Expand All @@ -1049,7 +1062,12 @@
switch (currentFunctionOnStack.name.toLowerCase()) {
// If in the sum function break aout cell names and add
case "sum":
<<<<<<< HEAD
console.log(tokenString);
=======
outstr = breakOutRanges(tokenString, "+");
>>>>>>> rangeHandler
debugger;
//TODO loop through and add ranges together
break;
Expand Down Expand Up @@ -1114,4 +1132,8 @@
}

excelFormulaUtilities.getTokens = getTokens;
<<<<<<< HEAD
}());
=======
}());
>>>>>>> rangeHandler
10 changes: 0 additions & 10 deletions test/dev.test.html
Expand Up @@ -33,16 +33,6 @@ <h2 id="qunit-userAgent"></h2>
</footer>
</div>

<!-- Forumla Checker from http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html -->
<p>This test is taken from<a href="http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html" target="_blank">http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html</a></p>
<p><input type="text" id="formula" style="width: 500px">
<input type="submit" value="parse it" onclick="window.excelFormulaUtilities.parseFormula('formula', 'tokens'); return false;"></p>
<div id="tokens" style="margin-top: 5px; margin-bottom: 5px">&nbsp;</div>
<p><strong>Here are some sample formulas, if you want to try it out:</strong></p>
<p>=IF("a"={"a","b";"c",#N/A;-1,TRUE}, "yes", "no") &amp;&nbsp;&nbsp; " &nbsp;more ""test"" text"</p>
<p>=+ AName- (-+-+-2^6) = {"A","B"} + @SUM(R1C1) + (@ERROR.TYPE(#VALUE!) = 2)</p>
<p>=IF(R13C3&gt;DATE(2002,1,6),0,IF(ISERROR(R[41]C[2]),0,IF(R13C3&gt;=R[41]C[2],0, IF(AND(R[23]C[11]&gt;=55,R[24]C[11]&gt;=20),R53C3,0))))</p>
<p>=IF(R[39]C[11]&gt;65,R[25]C[42],ROUND((R[11]C[11]*IF(OR(AND(R[39]C[11]&gt;=55, R[40]C[11]&gt;=20),AND(R[40]C[11]&gt;=20,R11C3="YES")),R[44]C[11],R[43]C[11]))+(R[14]C[11] *IF(OR(AND(R[39]C[11]&gt;=55,R[40]C[11]&gt;=20),AND(R[40]C[11]&gt;=20,R11C3="YES")), R[45]C[11],R[43]C[11])),0))</p>

<!-- -->
<script src="../src/core.js" ></script>
Expand Down

0 comments on commit cdffc4f

Please sign in to comment.