Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy table with link to clipboard to paste it into excel

I create data on a website, which is table like. I want to copy that data to excel. I do that using Tabs to go to the next cell in the row and newlines to go to the next row:

let clipboard = 'My link' + "\t" + secondCell + "\t" + thirdCell + "\n" + 
firstCellSecondRow + "\t" + secondCellSecondRow + "\t" + thirdCellSecondRow;
navigator.clipboard.writeText(clipboard);

Copying that text into an excel table works well:

Data in excel

Now my challenge is, that I want to add a link to text of one cell. My intuitive approach was to add the following text to the clipboard:

let clipboard = '<a href="https://www.my-url.com">My link</a>' + "\t" + secondCell + "\t" + thirdCell + "\n" + 
firstCellSecondRow + "\t" + secondCellSecondRow + "\t" + thirdCellSecondRow;
navigator.clipboard.writeText(clipboard);

But that doesn't work, it will actually display the text with the html in it:

Data in excel

What I want is only the text "My link" and an actual link that is clickable:

Data in excel

Since copying of text with links is possible in excel, I feel that it should somehow work. Can I do that with javascript?

like image 705
Mathias Bader Avatar asked Mar 11 '21 15:03

Mathias Bader


People also ask

Can you paste link a table in Excel?

In Excel, select the cell that will be in the upper left corner of the pasted data. Go to Home and select the Paste dropdown arrow to display a list of Paste Options. Choose a Link option. The linked data appears in the destination file.

How do I copy from clipboard to Excel?

3. Locate the "Clipboard" area of the ribbon, located on the far-left end. Click the small arrow to the right of the word "Clipboard" and the clipboard will appear on the left edge of the Excel spreadsheet. Click on any item in the clipboard to paste it into whatever cell you currently have selected.

How do you copy a table to clipboard?

Here's how to copy your table to clipboard: Click to select the Table on Piktochart. With your keyboard shortcut, use CTRL+C to copy the table.

How to copy and paste data from the clipboard in Excel?

This method is based on the shortcut key CTRL+V which is used to paste data. With a VBA code, you can use this command to paste data from the clipboard. First, ➤ Right-click on the sheet name from the Project panel of the VBA window. A dropdown menu will appear.

How do I copy and paste data from a text file?

Each cell is separated by a tab stop, and each record exists on a separate line in the text file. Select all of the text you want to copy to Excel and copy it to your clipboard. 2. Select the cell in Excel that you want to paste into. Select the upper-leftmost cell that you want your pasted data to appear in.

How to copy and paste a hyperlink in Excel?

To copy a hyperlink in excel, the use of a mouse click is the easiest and most convenient way. In the following dataset, we will copy the hyperlink of the company “Exceldemy”. After that, we will paste it to cell C12.

How to automatically copy a cell to clipboard with single click?

How to automatically copy a cell to clipboard with single click in Excel? Normally, when copying a cell to clipboard, you need to select the cell firstly, and then press the Ctrl + C key to copy it. How to automatically copy a cell by just clicking on it without pressing the hotkeys?


1 Answers

Here is how you achieve it in JavaScript:

let html = '<table><tr><td><a href="https://www.my-url.com">My link</a></td><td>secondCell</td></tr><tr><td>firstCellSecondRow</td><td>secondCellSecondRow</td></tr></table>';

const blob = new Blob([html], { type: "text/html" });

navigator.clipboard.write([new ClipboardItem({ [blob.type]: blob })]);

I didn't write it as a snippet in the answer because StackOverflow has security limitations that disallows the clipboard in snippets.

If you try it in your console, this error may occur:

Uncaught (in promise) DOMException: Document is not focused.

Here is a console-testable version of the code, it implies that you click on the page during the 3s timeout:

let html = '<table><tr><td><a href="https://www.my-url.com">My link</a></td><td>secondCell</td></tr><tr><td>firstCellSecondRow</td><td>secondCellSecondRow</td></tr></table>';

const blob = new Blob([html], { type: "text/html" });

setTimeout(() => navigator.clipboard.write([new ClipboardItem({ [blob.type]: blob })]), 3000);

Result in Excel:

enter image description here

like image 188
Guerric P Avatar answered Oct 21 '22 14:10

Guerric P