JS cleverly get clipboard data paste _javascript tips for Excel data

Source: Internet
Author: User
Another method is to use flash, which avoids the security restrictions of the browser, but the system class has only set methods, and other clipboard classes are accessible, but it seems to need FLASH10 version support.

Find a lap on the internet, nothing to find. Finally thought of to see how Google sheet is done.
Found that Google sheet implemented the plain text of Excel by most of the grid paste, no security restrictions required, no use of flash. Probably a lot of people know this method, anyway, I was surprised at first, but helpless, Google's JS file is handled, looks too tired. Then go to find Zoho sheet, it's JS file only a simple deal, can debug, can see.
Originally, it registered in the body of the onkeydown event, when the user press CTRL + V button, JS will focus on a textarea label, so that the next keypress and KeyUp incident occurred in the textarea, Naturally, the equivalent of the user in the textarea copy a bit, and then, JS then shifted focus, from textarea to the value. This takes you to the Clipboard text data.
Here is a little example of a simulation of Zoho sheet implementations.
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script type= "Text/javascript" ><!--
function Cellkeydown (event) {
if (event.ctrlkey && event.keycode = 86) {
var ss = document.getElementById ("TextArea");
Ss.focus ();
Ss.select ();
Wait 50 milliseconds, KeyPress event occurs and then process the data.
SetTimeout ("Dealwithdata ()", 50);
}
}
function Dealwithdata (event) {
var ss = document.getElementById ("TextArea");
alert (Ss.value);
Ss.blur ();
}
--></script>
<body onkeydown= "return Cellkeydown (Event)" >
<div>
<textarea id= "textarea" >

The obtained Excel text data, according to the \t,\n can divide the lattice and the line information. But this can only be a simple plain text of the Excel value, the grid style can not get. But I found a way to copy the document to the IFRAME (the way the online editor is implemented), so that there are some style information, ie in particular, it is estimated that their own products can be resolved by the product itself, FF and Chrome can only get a few simple attributes, such as the column width of the line. I don't know who has the means to get a more complete message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.