0

I am creating an table for an website. The website returns its values in an table. i need to copy all values from the table td's, and paste them in excel. is there a way to make a "select all" button, so that i only need to press ctrl+c? Instead of dragging my mouse all the way down the table.

I want outcome to be like this (if I press the button)

Code of the table i used here:

<!DOCTYPE html>
<html>
<head>
    <style>
        table,
        th,
        td {
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <h2>Select all test</h2>
    <button type="button">Select all</button>
    <table style="width:80%">
        <tr>
          <th>Name</th>
          <th>Lastname</th>
          <th>Age</th>
        </tr>
        <tr>
            <td>Henk</td>
            <td>Bakker</td>
            <td>46</td>
        </tr>
        <tr>
            <td>Fedde</td>
            <td>hooghouts</td>
            <td>67</td>
        </tr>
    </table>
</body>
</html>
Dave
  • 5,108
  • 16
  • 30
  • 40
C. Lambers
  • 13
  • 3
  • Yes, it is possible. – Teemu Feb 26 '19 at 12:29
  • you try that with javascript or jquery [link](https://stackoverflow.com/questions/7012469/get-all-rows-in-the-current-table-and-not-from-child-tables) – kealaxshan Feb 26 '19 at 12:34
  • If it is a basic HTML table, you can directly import it into excel by using `menu - data - import from web`, then copy pasting the URL and selecting the correct table. If all you're doing is making shortcuts for manual operations, this was easier to teach my users than actually creating a script to select, then press ctrl-c, go to excel, paste the data, reformat the data. – Shilly Feb 26 '19 at 12:43

1 Answers1

0

JS:

function selectElementContents(el) {
        var 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();
        }
    }

HTML:

<table id="tableId" border="1">
    <thead>
        <tr><th>Heading 1</th><th>Heading 2</th></tr>
    </thead>
    <tbody>
        <tr><td>cell 1</td><td>cell 2</td></tr>
    </tbody>
</table>

<input type="button" value="select table" onclick="selectElementContents( document.getElementById('tableId') );">

It should work ;)

Link from: Select a complete table with Javascript

Zikux
  • 16
  • 1
  • is it also possible to only select and copy the tbody, but to keel the layout? so it also copies the border and stuff? because now if i paste it in excel it goed in 1 cell – C. Lambers Feb 26 '19 at 14:11