Last active
December 15, 2015 14:08
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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