Live data from Hacker News

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

jsfiddle.net

81–90 of 274 posts

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

#81
post #76

Earlier quoted context omitted.

I am a bit lost as to why cycles are handled the way they are. Part of the problem, is cycles seem to crash chrome's developer tools. If you put =A1 in A1, and =A2 in A2, it crashes the web inspector for me. I am lost as to why putting =A1 in A1 doesn't cause an infinite loop. Why doesn't eval (DATA.A1) not trigger the getter and hence an infinite loop?

The getter impl should prevent this. Firefox, for instance, throws an exception...

I think I figured it out. It does cause an infinite loop, but Chrome throws a "Range Error: Max Call Stack Exceeded", which then gets trapped by the try/catch block.

Firefox does a similar thing, by throwing a "too much recursion" error.

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

#82
post #33

Earlier quoted context omitted.

Personally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed with (DATA) now that's just genius. I hate writing token parsing.

with (context) + eval = user-space lexical environment ?

Yep, I'm using something very like this in a Node project I'm working on. However, with(context) has some pretty nasty side effects that I just couldn't sidestep, so I used the "contextify" module.

If you're needing with and have access to npm, use contextify, It'll save you a lot of effort!

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

#83
post #79

The fact that you can put Javascript code which interacts with the cells is awesome. For example you can put 10 in A1 and then put "=sum=0;for(i=0;i<A1;i++)sum+=i" in A2 to get the sum from 0 to 10. I really like this.

You may like, but it is an obvious security flaw. A 'real' product couldn't have this feature, at least not the way it is implemented here.

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?

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

#84
post #33

Earlier quoted context omitted.

Personally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed with (DATA) now that's just genius. I hate writing token parsing.

I'm a bit slow this morning...would anyone mind explaining how that and the defineProperties stuff makes a parse happen?

[deleted]

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

#85
post #33

Earlier quoted context omitted.

Personally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed with (DATA) now that's just genius. I hate writing token parsing.

I'm a bit slow this morning...would anyone mind explaining how that and the defineProperties stuff makes a parse happen?

defineProperty is being used to set a property On DATA, named corresponding to the cell's id (e.g., A3), to a descriptor. In this case the descriptor only provides a getter function, inside of which is an eval done with DATA as the context. That means all evaluated variable references will assume DATA as the implicit 'this' (whereas normally that would be 'window')

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

#86
post #33

I am impressed. Its a neat hack, it actually is a real spreadsheet in 30 lines of code, with references fully working (ie =A3+B4 gives expected output. As does =alert("foo") but then nothing is perfect). I think it says something about the browser as a platform - these thirty lines simply assume an enormous amount that excel and visicalc could not - visicalc had to write their own screen refresh routines. It is usefu…

Personally I was confused as to how the fellow got around the basics of having to parse tokens until I noticed with (DATA) now that's just genius. I hate writing token parsing.

I can safely say its one of the only 'appropriate' uses of a with-block I have ever seen.

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

#87
post #79

The fact that you can put Javascript code which interacts with the cells is awesome. For example you can put 10 in A1 and then put "=sum=0;for(i=0;i<A1;i++)sum+=i" in A2 to get the sum from 0 to 10. I really like this.

You may like, but it is an obvious security flaw. A 'real' product couldn't have this feature, at least not the way it is implemented here.

Yeah! Better replace it with VB and you've got no security probl... oh wait!

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

#90
Just yesterday there was that Google email from 2010 talking about how JS can not evolve any further. It made me feel a bit wasteful about my time spent with the likes of node and angular.

Seeing things like this reminds me how much JS is capable of in today's browsers and also shows me how much more I need to understand about the language.

Awesome stuff.

Post reply on HN