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

192
Visualizações
How to skip some key-value pairs when converting JSON to Excel Worksheet using XLSX in JS

I have an array of objects (JSON) in JS and want to convert it to an Excel worksheet. In the JSON data, there are some key-value pairs I do not want convert to Excel, for example Excel should not contain year information like below (PLEASE RUN HTML CODE TO SEE EXCEL OUTPUT REQUIRED):

JSON data:

[
  {
    car: "Range Rover",
    color: "silver",
    year: "2021"
  },
  
  {
    car: "Benz",
    color: "black",
    year: "2019"
  },
  
  {
    car: "Toyota",
    color: "silver",
    year: "2020"
  }
]

Excel view (Run the snippet):

<html>
  <body>
    <table>
      <tr>
        <th>car</th>
        <th>color</th>
      </tr>

      <tr>
        <td>Range Rover</td>
        <td>silver</td>
      </tr>

      <tr>
        <td>Benz</td>
        <td>black</td>
      </tr>

      <tr>
        <td>Toyota</td>
        <td>silver</td>
      </tr>
    </table>
  </body>
</html>

How can I achieve this using XLSX in JS?

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

0

There are few things you can do here.

  1. After parsing your JSON data, you can save it in an xlsx worksheet, change the worksheet's !ref property to the desired range, then add that sheet to the xlsx book.
let file = fs.readFileSync('test.json', 'utf-8');
let data = JSON.parse(file);

var wb = xlsx.utils.book_new();
var ws = xlsx.utils.json_to_sheet(data);

ws['!ref'] = 'A1:B4';
xlsx.utils.book_append_sheet(wb, ws, 'Sheet 1');
xlsx.writeFile(wb, 'test1.xlsx');
  1. Filter out your the properties that you want from your JSON data into a separate array and then add them to the file.
let file = fs.readFileSync('test.json', 'utf-8');
let data = JSON.parse(file);

var wb = xlsx.utils.book_new();

let dataArray = [];
data.forEach(d => {
  dataArray.push({"car": d.car, "color": d.color});
});

var ws = xlsx.utils.json_to_sheet(dataArray);
console.log(ws);
xlsx.utils.book_append_sheet(wb, ws, 'Sheet 1');
xlsx.writeFile(wb, 'test2.xlsx');

Checking the utility functions documentation might be helpful too.

https://www.npmjs.com/package/xlsx#utility-functions

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