Home > Software design >  XLSX Angular export sheet from nested array in one sheet
XLSX Angular export sheet from nested array in one sheet

Time:05-11

I am using this package enter image description here

CodePudding user response:

I am not sure if there is a more elegant way to do this with built-in xlsx library methods, but you can transform data to get desired output.

Array you pass into json_to_sheet function has to be structured like this:

[
  { "name": "Child Product 1", "id": 11 },
  { "name": "Child Product 2", "id": 12 },
  { "name": "--", "id": "--" },
  { "name": "name", "id": "id" },
  { "name": "Child Product 1", "id": 13 },
  { "name": "Child Product 2", "id": 14 }
]

Transform data into new array for export:

const exportData = data.map((item, i) => {
  if (data.length !== i 1) {
    item.childProduct.push({"id": "--", "name": "--"});
    item.childProduct.push({"id": "id", "name": "name"});
  }
  return item.childProduct;
}).flat();

Export data using library:

xlsx.utils.json_to_sheet(exportData);

CodePudding user response:

let dummyArray =[]
    data.map((dat)=>{
      dat.childProduct.forEach((x)=>{
        dummyArray.push(x)
      })
    });
xlsx.utils.json_to_sheet(dummyArray)

This is for simple format.

  • Related