Save Excel document with duplicated headers #2902

Closed
opened 2023-03-16 08:36:54 +00:00 by asde56egesr · 1 comment

Hello. I wanna save excel document with duplicated headers. For example I use this headers in my code.
use
I expect this
expect
But I get this
get

Because "xlsx" lib removed duplicated headers. How to fix it?

Hello. I wanna save excel document with duplicated headers. For example I use this headers in my code. ![use](https://i.ibb.co/5RTYXZk/8f072a0aed89.png) I expect this ![expect](https://i.ibb.co/GJJ4Rp9/dde64ba3f283.png) But I get this ![get](https://i.ibb.co/tqRMLp2/dc6c45fc7e39.png) Because "xlsx" lib removed duplicated headers. How to fix it?
Owner

Assuming you have data like:

const data = [
  { FirstName: "Alex", SecondName: "Bob", Age: 20, Country: "USA" },
  { FirstName: "Josh", SecondName: "Joe", Age: 37, Country: "Canada" },
];
const columns = [
  { label: "Name", value: "FirstName" },
  { label: "Name", value: "SecondName" },
  { label: "Age", value: "Age" },
  { label: "Country", value: "Country" }
];

You can generate the worksheet as follows:

const ws = XLSX.utils.aoa_to_sheet(data.map(row => columns.map(col => row[col.value])), { origin: 1 });
XLSX.utils.sheet_add_aoa(ws, [columns.map(col => col.label)])
Assuming you have data like: ```js const data = [ { FirstName: "Alex", SecondName: "Bob", Age: 20, Country: "USA" }, { FirstName: "Josh", SecondName: "Joe", Age: 37, Country: "Canada" }, ]; const columns = [ { label: "Name", value: "FirstName" }, { label: "Name", value: "SecondName" }, { label: "Age", value: "Age" }, { label: "Country", value: "Country" } ]; ``` You can generate the worksheet as follows: ```js const ws = XLSX.utils.aoa_to_sheet(data.map(row => columns.map(col => row[col.value])), { origin: 1 }); XLSX.utils.sheet_add_aoa(ws, [columns.map(col => col.label)]) ```
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#2902
No description provided.