Archive for May, 2006

Array Extras for Actionscript

Late last fall, with the advent of Firefox 1.5, Mozilla added a handful of new Array methods with the update of the Gecko browser engine to JavaScript 1.6. Apparently, these have also found their way into ActionScript. Sharp eyed Daniel Hai noted last month that these were silently added into Flex 2.0 Beta 2 as undocumented methods, and this past week, they’ve finally made their debut in the Beta 3’s Array documentation.

These new additions to the Array class bring a couple of item locator methods, indexOf() and lastIndexOf(), which behave much like their corresponding String analogues, and a set of more interesting iterative methods: every(), filter( ) forEach(), map(), and some(). Each of these latter methods take a callback function to be invoked on each item in the array, and collectively, allow arrays to be used much like lists in functional programming.

While these new methods don’t add any new functionality that couldn’t be done in the past via relatively simple loops, they do make working with arrays quite a bit easier and help simplify the idioms used in array operations. In the last few days, I’ve taken to them so much that I even threw together a quick ArrayExtras backport implementing these as mixin methods for ActionScript 2.0.

With the significant retooling and polish that’s been going into ActionScript 3.0, it’s great to see that Adobe is leading the way with E4x support and the incorporation of Mozilla’s Array extras as we move towards the 4th edition of ECMAScript.

Comments

Parsing Excel Files with ActionScript 3.0

Back in January, I had started learning ActionScript 3.0 by picking a particularly interesting problem that had been dogging me since I first started building web-based applications in the late 1990s. That problem is, how to build client-side programs that can parse and render binary file formats within a browser without the need to download separate plugins or ActiveX controls for each one. To do that, you need to able to parse binary data on the client. And when I saw the ByteArray class in Flex 2.0, I smiled.

I decided to go after Excel files. Yes, many machines have Excel or comparable spreadsheet application already installed on them. And Microsoft does offer a free downloadable viewer for Excel files. But wouldn’t it be just neat-o to be able to view Excel documents directly in the browser without needing anything but the Flash Player? Or even better yet, import data straight from .xls files into a Flash RIA?

After a few nights of work, I managed to import some basic .xls files and correctly parse out column and row data for strings and numbers. That was January, and then a maelstrom of client work happened (including a particularly painful stretch of 12 consecutive 16-hour workdays at a remote site). And the Flex 2.0 APIs changed. And changed again with each new beta. Not that I blame Adobe–each time it’s been for the better. Seriously, does anyone like having to import trace in every single class that you’re debugging? It was just that I hardly had time to sleep, let alone code anything fun. Thankfully, my good friend Darron Schall had been keeping my code updated to the latest versions of the Flex 2.0 beta in preparation for his recent presentation at Flash in the Can.

However, I’ve finally managed to get back to working on it, updating it yet again to Beta 3, adding a couple new features, and preparing it for public consumption. Next up is allowing for the file path to the .xls file to be specified programmatically (it’s currently hard-coded), and exposing the parser as a library that can be used by other projects.

So, without further ado, here’s a couple of demos: A very basic test, as both the Excel file and loaded into my Flash-based viewer. And a slightly more complex example, with the data available as the Excel file and loaded into the viewer (requires Flash Player 9).

Comments (4)