Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Copy hyperlink text and plain text to the clipboard, then paste them into Excel using Javascript/JQuery in multiple cells of a row?

My goal is to make a function that, on a specific page, collects data and copies it to the clipboard, later pasted into a Google Sheet or Excel Spreadsheet.

The data appears in different places on a web-page and is presented as follows:

<!-- Somewhere throughout the web page -->
<div data-cy="a">
  <strong><a href="www.example.com">text</a></strong>
</div>

<!-- Another place throughout the web page -->
<div data-cy="b">
  <strong>another text</strong>
</div>

Moreover, I need to push some of my simple strings, such as var text = 'different text'.

In an Excel spreadsheet, the three values should be pasted in three matching fields in one row:

A B C
1 text another text different text

Here is my code so far:

 var a = $('*[data-cy="a"]').html().replaceAll('\n','').replaceAll('<strong>','').replaceAll('</strong>','') // extract 'a'.
 var b = $('*[data-cy="b"]').html().replaceAll('\n','').split(/[><]/)[2]; // extract 'b'.
 var c = 'different text';
 
 var allCells = a + '\t' + b + '\t' + c; // using Tab character to divide between cells.

function copyToClipboard(str) {
    
    function listener(e) {
        e.clipboardData.setData("text/html", str);
        e.clipboardData.setData("text/plain", str);
        e.preventDefault();
    }

    document.addEventListener("copy", listener);
    document.execCommand("copy");
    document.removeEventListener("copy", listener);
};
 
copyToClipboard(allCells);

I tried both the deprecated and more recent versions of navigator.clipboard.writeText(), but none of these seem to give me the option to paste the values in different cells in the same row.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This comment helped me to figure out what is actually happening. In order to work around this, unfortunately, I created a new table in the DOM and copied the elements from there. Pasting into the Excel sheet works like a charm.

// Extraction of variables
var a = $('*[data-cy="a"]').html().replaceAll('\n','').replaceAll('<strong>','').replaceAll('</strong>','') // extract 'a'.
var b = $('*[data-cy="b"]').html().replaceAll('\n','').split(/[><]/)[2]; // extract 'b'.
var c = 'different text';

let allCells = [$(clientName), locationCompany, treatedBy];

// Table creation
var table = $('<table>').addClass('table').attr('id', 'excel-data');
var row = $('<tr>');

for(i=0; i<allCells.length; i++) {
    row.append($('<td>').html(allCells[i]));
}
table.append(row);

$('[data="element-data"]').append(table);
// Select & copy table function
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);
        }
        document.execCommand("copy");

    } else if (body.createTextRange) {
        range = body.createTextRange();
        range.moveToElementText(el);
        range.select();
        range.execCommand("Copy");
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda