1

Problem: I have attached my code for copy data and format of a html table into clipboard like below. This code work as I expected on chrome but does not on microsoft edge. I tried but cannot found any good way to make my code working on edge exactly like chrome. Is there any solution for these case?

Additional Infomation:

Chrome version: 78.0.3904.108

Edge version: Microsoft Edge 44.17763.831.0 --- Microsoft EdgeHTML 18.17763

This drive link included result of my code for chrome and edge browser (paste to text, word and excel file): https://drive.google.com/open?id=155DWin26afHumem0tYircVsNojKPEl5M

CODE: https://jsfiddle.net/bj13nu9s/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table id="table">
    <thead>
        <tr>
            <th>From</th>
            <th>To </th>
            <th>School</th>
            <th>Class</th>
            <th>Student Name</th>
            <th>Student Code</th>
        </tr>
    </thead>
    <tbody class="scroll-content">
        <tr>
            <td>2011/02/18</td>
            <td>2011/02/19</td>
            <td>Diet University</td>
            <td>Classical Medley</td>
            <td>John Wick</td>
            <td>305179</td>
        </tr>
        <tr>
            <td>2011/02/18</td>
            <td>2011/02/19</td>
            <td>Diet University</td>
            <td>Classical Medley</td>
            <td>John Wick</td>
            <td>305179</td>
        </tr>
        <tr>
            <td>2012/03/20</td>
            <td>2015/03/20</td>
            <td>Universal University</td>
            <td>Memento Guitar</td>
            <td>Daniel Johnson</td>
            <td>60708090</td>
        </tr>
        <tr>
            <td>2013/02/18</td>
            <td>2018/02/19</td>
            <td>Kurikuri University</td>
            <td>Magic Chaos</td>
            <td>Yugi Muto</td>
            <td>12356789</td>
        </tr>
        <tr>
            <td>2016/01/02</td>
            <td>2019/02/19</td>
            <td>Destroyer University</td>
            <td>...</td>
            <td>Santa Monica</td>
            <td>64562107</td>
        </tr>
    </tbody>
</table>
<button type="button" id="copy_btn" onclick="selectElementContents( document.getElementById('table') );">Copy Table</button>
</body>
</html>
<script type="text/javascript">
    function selectElementContents(el) {
        let body = document.body, range, sel;
        if (document.createRange && window.getSelection) {
            range = document.createRange();
            sel = window.getSelection();
            sel.removeAllRanges();
            try {
                range.selectNodeContents(el);
                sel.addRange(range);
            } catch (e) {
                range.selectNode(el);
                sel.addRange(range);
            }
        } else if (body.createTextRange) {
            range = body.createTextRange();
            range.moveToElementText(el);
            range.select();
        }
        document.execCommand("Copy");}

</script>
Megusta
  • 39
  • 1
  • 8

1 Answers1

1

I tried to test the issue and I found that I can produce the issue.

If your paste settings in Word and Excel are set as Keep Text only then you can see that table is not in format.

Generally when you paste using CTRL + V then it use Keep Text only formatting. I suggest you to paste the table using right click of mouse -> paste (Keep source formatting) option.

enter image description here

Notepad does not have any formatting option, so we cannot fix this issue for Notepad. It looks like this is by design in MS Edge browser.

For other work around, you can try to test the issue using MS Edge (Chromium) browser. Pasting the table will keep the formatting in Notepad, Word and Excel.

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Thank you for your help. This can be an acceptable solution for my problem, but I would prefer changing javascript code to solve this case if it possible :D – Megusta Dec 04 '19 at 08:14
  • I will try to find and if there is any solution using JS then I will inform you. Thanks for your understanding. – Deepak-MSFT Dec 04 '19 at 08:48
  • Sorry but can I know your excel and word version because when I copy data using MS Edge browser and paste them by rigt-click + keep source formatting in excel 2010 -> "cannot paste the data" and word 2010-> same result as notepad @Deepak-MSFT – Megusta Dec 05 '19 at 04:53
  • I made this test with MS Office 2016 version. So it is Word 2016 and Excel 2016. – Deepak-MSFT Dec 05 '19 at 05:09