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

181
Visualizações
Function starts when page charges and outerHTML error

I'm trying to make an Excel Sheet with a javascript function in React, but when I try to enter to the page, and the button which has the onClick is not working.


export default function DescargaExcelHTML() {
  const descargaExcel = (tableID, filename = "") => {
    console.log(tableID);
    let downloadLink;
    let datatype = "application/vnd.ms-excel";
    let tableSelect = document.getElementById(tableID);
    let tableHTML = tableSelect.outerHTML.replace(/ /g, "%20");
    filename = filename ? filename + ".xlsx" : "excel_data.xlsx";
    downloadLink = document.createElement("a");
    document.body.appendChild(downloadLink);

    if (navigator.msSaveOrOpenBlob) {
      let blob = new Blob(["\ufeff", tableHTML], { type: datatype });
      navigator.msSaveOrOpenBlob(blob, filename);
    } else {
      downloadLink.href = "data:" + datatype + "," + tableHTML;
      downloadLink.download = filename;
      downloadLink.click();
    }
  };

  return (
    <>
      <div className="overflow-x-auto">
        <table id="prueba" className="table w-full">
          <thead>
            <tr>
              <th></th>
              <th>Name</th>
              <th>Job</th>
              <th>Favorite Color</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th>1</th>
              <td>Cy Ganderton</td>
              <td>Quality Control Specialist</td>
              <td>Blue</td>
            </tr>
          </tbody>
        </table>
      </div>
      <button
        className="btn btn-primary"
        onClick={descargaExcel("prueba", "pruebaDescarga")}
      >
        Download Excel
      </button>
    </>
  );
}

Also when I refresh the page I'm getting an outerHTML error:

Uncaught TypeError: Cannot read properties of null (reading 'outerHTML') at line let tableHTML = tableSelect.outerHTML.replace(/ /g, "%20");

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

0

Try changing:
onClick={descargaExcel("prueba", "pruebaDescarga")}
By:
onClick={() => descargaExcel("prueba", "pruebaDescarga")}

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