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
Give it a try. Select a bunch of cells in Excel, and hit
Then come to Google Spreadsheets, and hit
Ctrl+V. The cells are copied over!
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:
- When the
Ctrlkey 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
- Listen for the
textInputevent on the hidden TextField. If any data is pasted, it'll be available here and can be parsed and fed into the DataGrid.
- When the
Ctrlkey is released, remove the hidden TextField.
Check out the sample code, an implementation of this technique. It works (TM).
PS: The hidden TextField thing was Rishi's idea.