Skip to content

Instantly share code, notes, and snippets.

@guipn
Last active December 23, 2015 18:59
Show Gist options
  • Save guipn/6679709 to your computer and use it in GitHub Desktop.
Save guipn/6679709 to your computer and use it in GitHub Desktop.
Indent ugly Excel formulas...
<html>
<head>
<title>Excellent</title>
<style type="text/css">
body {
text-align: center;
}
</style>
</head>
<body>
<h1>Excellent</h1>
(abrir no Google Chrome!)
<p/>
<textarea id="input" cols="100" rows="30">
SE(C2=G2;I2-D2;SE(C2=G3;I3-D2;se(c2=g4;l4-d2;se(C2=G5;L5-D2;SE(C2=G6;L6-D2;SE(C2
=G7;L7-D2;SE(C2=G8;L8-D2;SE(C2=G9;L9-D2;SE(C2=G10;L10-D2;SE(C2=G11;L11-D2;SE(C2=
G12;L12-D2;SE(C2=G13;L13-D2;SE(C2=G14;L14-D2)))))))))))))</textarea>
<p/>
<input type="button" id="go" value="Indentar"/>
<input type="button" id="reset" value="Apagar"/>
<script src="excellent.js"></script>
<script>
function get(id) {
return document.getElementById(id);
}
get('go').addEventListener('click', function () {
get('input').value = parser().parse(get('input').value);
})
get('reset').addEventListener('click', function () {
get('input').value = '';
});
</script>
</body>
</html>
function parser(separator, line, tab) {
separator = separator || ';';
line = line || '\n';
tab = tab || ' ';
function peek(str, n) {
return str.substring(0, n || 1);
}
function next(str, n) {
return str.slice(n || 1);
}
function indent(str, indentation) {
return indentation + str;
}
function unindent(str, indentation) {
return str.replace(indentation, '');
}
function parse(formula, indentation) {
var newline = line + (indentation = indentation || '');
if (formula === '') {
return '';
}
if (peek(formula) === '(') {
return peek(formula) +
newline +
indent(parse(next(formula), indent(indentation, tab)), tab);
}
if (peek(formula) === ')') {
indentation = unindent(indentation, tab);
return line +
indentation +
')' +
parse(next(formula), indentation);
}
if (peek(formula) === separator) {
return peek(formula) +
newline +
parse(next(formula), indentation);
}
return peek(formula) +
parse(next(formula), indentation);
}
function prettify(mess) {
return mess.replace(/(<=|>=|!=|<>|[*=+\-\/><^])/g, ' $1 ');
}
function removeNoise(str) {
return str.replace(/\n/g, '');
}
return {
parse: function (excelFormula) {
return prettify(parse(removeNoise(excelFormula)));
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment