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.
![Copying 2 rows from Microsoft Excel [screenshot]](http://img.photobucket.com/albums/v102/mannu/e8e69a54.gif)
Then come to Google Spreadsheets, and hit Ctrl+V. The cells are copied over!
![Pasting 2 rows into Google Spreadsheet [image: screenshot]](http://img.photobucket.com/albums/v102/mannu/5240c3fb.gif)
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 usingCtrl+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 usingCtrl+C. - 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).
![Pasting 2 rows into a Flex application [image: screenshot]](http://img.photobucket.com/albums/v102/mannu/a333ddac.gif)
PS: The hidden TextField thing was Rishi's idea.
댓글 없음:
댓글 쓰기