Manish Jethani (mannu) wrote,
Manish Jethani
mannu

Copy 'n paste between Excel and Flex

I was looking at Google Spreadsheets recently. One of the neat features of this RIA is that it supports copy 'n paste to and from other spreadsheet applications like Microsoft Excel using standard key combinations like Ctrl+C and Ctrl+V.

Give it a try. Select a bunch of cells in Excel, and hit Ctrl+C.

[screenshot]

Then come to Google Spreadsheets, and hit Ctrl+V. The cells are copied over!

[image: screenshot]

How does it work? It's really simple: when cells are copied in Excel, the data is stored in the system clipboard as tab-separated values (TSV); when pasted into Google Spreadsheets, the data is parsed and distributed among the cells accordingly.

So the next question is, how can such a feature be added to a Flex application? Let's say you want the user to be able to copy cells from Excel into a Flex DataGrid and vice versa.

Here's one solution that involves a hidden TextField:

  1. When the Ctrl key is pressed on the DataGrid, create an hidden TextField and give it player-level focus. This way we're ready to accept any paste operation using Ctrl+V. Also copy the DataGrid's selected rows into the TextField in TSV format, and make all the text selected, so that a copy operation can be performed using Ctrl+C.
  2. Listen for the textInput event on the hidden TextField. If any data is pasted, it'll be available here and can be parsed and fed into the DataGrid.
  3. When the Ctrl key is released, remove the hidden TextField.

Check out the sample code, an implementation of this technique. It works (TM).

[image: screenshot]

PS: The hidden TextField thing was Rishi's idea.

Tags: flex
Subscribe

  • Expose-like window navigation in Flex

    What's the point in buying a Mac if you're not going to copy some of its cool features? In my spare time, I've been working on an implementation…

  • Globals

    You have a main application and two sub-applications loaded dynamically using the SWFLoader component. <Application…

  • Post-Flex 2.0 layout enhancements (thoughts)

    These are some of my ongoing thoughts on how we can improve the layout support post Flex 2.0. Percentage values for left, right,…

Comments for this post were disabled by the author

  • Expose-like window navigation in Flex

    What's the point in buying a Mac if you're not going to copy some of its cool features? In my spare time, I've been working on an implementation…

  • Globals

    You have a main application and two sub-applications loaded dynamically using the SWFLoader component. <Application…

  • Post-Flex 2.0 layout enhancements (thoughts)

    These are some of my ongoing thoughts on how we can improve the layout support post Flex 2.0. Percentage values for left, right,…