Skip to content

Instantly share code, notes, and snippets.

@paduc
Last active December 15, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paduc/5272052 to your computer and use it in GitHub Desktop.
Save paduc/5272052 to your computer and use it in GitHub Desktop.
This demonstrates how to handle the paste of some portion of a spreadsheet (google spreadsheet, excel, ...). Listens for a paste event on a textarea and converts the pasted data into an matrix of cells.
var field = $('textarea.myTextarea');
var cells;
// listen to keydown on text field
field.on('keydown', function(e){
// detect if it's a copy-paste action
if(e.keyCode === 86){
// It's a keydown event only, so wait 100ms for the field value to be updated
setTimeout(function(){
var data = field.val();
// split line by line
var lines = data.split('\n');
// split each line by the tab character (charCode = 9)
cells = _.map(lines, function(line){
return line.split(String.fromCharCode(9));
});
}, 100);
// cells contains an array of arrays
// cells[line][row] gives the value of a cell
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment