JS cleverly retrieves Clipboard data and pastes Excel Data

Source: Internet
Author: User

The other method is to use flash, which can avoid the security restrictions of the browser, but the system class only has the set method, and the clipboard class can access it, however, it seems that flash 10 is needed.

I found a circle on the Internet, but I didn't find it. Finally, I want to see how google sheet works.
It is found that google sheet implements multi-data format pasting of excel plain text without security restrictions and flash is not used. Many people may know this method. I was surprised at the beginning, but google's js files have been processed and seem too tired. Then I went to ZOHO sheet. Its js file was simply processed and can be debugged and viewed.
Originally, it registered the onkeydown event on the body. When the user presses the ctrl + v Key, js transfers the focus to a textarea tag. In this way, the next keyPress and keyUp events occur on textarea. Naturally, it is equivalent to copying them in textarea. Then, js transfers the focus and obtains the value from textarea. In this way, the text data in the clipboard is obtained.
The following is a small example of imitating the ZOHO sheet implementation.
Copy codeThe Code is as follows:
<Html>
<Head>
<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 for 50 milliseconds and then process the data after the keyPress event occurs
SetTimeout ("dealwithData ()", 50 );
}
}
Function dealwithData (event ){
Var ss = document. getElementById ("textArea ");
Alert (ss. value );
Ss. blur ();
}
// --> </Script>
</Head>
<Body onkeydown = "return cellkeydown (event)">
<Div>
<Textarea id = "textArea">

The obtained Excel text data can be divided by \ t, \ n to obtain the grid and row information. In this way, only the excel values of simple plain text can be obtained, and the style of the grid cannot be obtained. However, I found a method that is copied to the document of iframe (that is, the implementation method of the Online Editor), so that some style information can appear, which is particularly comprehensive in IE, it is estimated that the product can be parsed by itself. FF and Chrome can only obtain simple attributes such as row column width. I don't know who can get more comprehensive information.

Related Article

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.