Home >Web Front-end >JS Tutorial >JS cleverly obtains clipboard data and pastes Excel data_javascript skills

JS cleverly obtains clipboard data and pastes Excel data_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:50:331175browse

Another method is to use flash, which can avoid browser security restrictions, but there is only a set method in the system class, and it can be accessed in the other clipboard class, but it seems to require the support of the flash10 version.

I searched online and found nothing. I finally thought of checking out how Google Sheets works.
I found that Google Sheet implements Excel's plain text multi-data grid pasting without the need for security restrictions and without using flash. Maybe many people know this method. Anyway, I was surprised at first, but I had no choice but to do it. Google’s js files have been processed and it looks too tiring. Then I went to find the ZOHO sheet. Its js file was simply processed, and I could debug and read it.
It turns out that the onkeydown event is registered on the body. When the user presses the ctrl v key, js shifts the focus to a textarea label, so that the next keyPress and keyUp events occur on the textarea. Naturally, it is equivalent to the user copying it in the textarea, and then js shifts the focus and gets the value from the textarea. In this way, the text data in the clipboard is obtained.
The following is a small example that imitates the implementation of ZOHO sheet.

Copy code The code is as follows:




Untitled Document
< script type="text/javascript">


You can then separate the grid and row information. But this can only get simple plain text excel values, and the grid style cannot be obtained. But I found a method. When copying to the document of an iframe (which is how the online editor implements it), some style information will appear. It is particularly comprehensive in IE. It is probably because my own product can parse it by itself. FF and Chrome can only get some simple attributes such as line number and column width. I don’t know if anyone has a way to get more complete information.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn