PrefaceThis time I recorded an idea I had last night: copy the Excel content to the clipboard and convert it into the JSON format you want. The core is to convert the Excel content to JSON. This part mainly depends on how the Excel format and JSON are mapped to the business, so I won’t expand on it. However, through practice, I gained some knowledge points about the clipboard. Note: Because it is only for my own gadget implementation, I don't consider compatibility, and practice it under Chrome The whole steps are:
There are three main points here: 1. Paste Events and Clipboarddocument.addEventListener('paste', event => { //clipboardData object in event console.log(event.clipboardData) }) When the paste event is triggered, you can get the clipboardData from the event However, window.clipboardData is also used in it. I tried it in chrome and codepen, but couldn't get the content. 2. The content format in the clipboardWhen the code in the previous part is printed to the console, there will be a puzzlement. A DataTransfer object is printed out in the console, but in fact, when this object is expanded in the console, the attributes either have no value or are empty arrays, which is very confusing. It wasn't until I looked into the console property content that I found it. In this object, getData is its common method, which is used to obtain data content. It needs to accept a DOMString parameter. The commonly used method is to paste plain text, which can be obtained by calling getData('text'). But what I want is the Excel format. At first I didn’t know what Excel format was, but when I copied from Excel and pasted it back into Excel, the format was still retained, so I thought the clipboard should still retain the format of the original content, so I gave it a try. By traversing and printing out the types attribute of the DataTransfer object, you can know document.addEventListener('paste', event => { event.clipboardData.types.map(type=>{console.log(type)}) }) Type has three values: text/plain, text/html, Files So I used type 'text/html' and tried getData, and sure enough, I got the formatted content, which is actually a string of HTML code as follows <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=utf-8"> <style> ... <table> ... Through observation, we know that the desired content is the table in the HTML code. Then it will be easy to convert the format. Just parse the HTML string and use the selector to get the cell content. 3. How to parse HTML stringThis really took me a lot of time, and later I found DOMParser, which originally supports parsing HTML strings>> Convert a string to DOM using (new DOMParser()).parseFromString const html = event.clipboardData.getData('text/html'); const $doc = new DOMParser().parseFromString(html,'text/html'); // Load all rows const $trs = Array.from($doc.querySelectorAll('table tr')); Then you can querySelectorAll happily. The above is the details of how to use JS to parse the Excel content in the clipboard. For more information about JS parsing the Excel content in the clipboard, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed tutorial for installing mysql5.7.21 under Windows
>>: Understanding Nginx Current Limitation in One Article (Simple Implementation)
Table of contents Preface know Practice makes per...
If you have developed DApps on Ethereum, you may ...
Fault description percona5.6, mysqldump full back...
1. Introduction This article does not have screen...
Core SQL statements MySQL query statement that do...
Table of contents Preface React Functional Compon...
Table of contents 1. Browser local storage techno...
Table of contents Overview 1. Compositon API 1. W...
Table of contents 1. Ternary operator judgment 2....
Table of contents Abstraction and reuse Serial Se...
<br />This tag is used to create a multi-lin...
XML/HTML CodeCopy content to clipboard <!DOCTY...
How do I download MySQL from the official website...
Whitelist rule syntax: BasicRule wl:ID [negative]...
I recently wrote a script for uploading multiple ...