question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

while using header option with XLSX.utils.json_to_sheet , headers not overriding

See original GitHub issue

While I try to change header titles by passing array of titles to options like below it does not override the headers. Instead it writes new headers first and original data with old headers again from next cell.

Here is my code

const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json, {header: headerColumns});
    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Transactions');
    const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
    this.saveAsExcelFile(excelBuffer, excelFileName);

Also see attached exported file. sample.xlsx

why is it happening? I just want to rename the headers , everything else is working file.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SheetJSDevcommented, Oct 11, 2020

@paustint that’s an unrelated issue and should be tracked separately.

@SandeshSarfare @dottodot This is intended behavior. If you want to overwrite the values in the header cells, use aoa_to_sheet and directly modify the header names.

As described in the docs, header option is “Use specified column order (default Object.keys)”.

This option exists because of a fundamental ambiguity. Suppose you were trying to write the following to a worksheet:

[
  {Price: 1, Name: "a"},
  {Name: "b", Price: 2},
]

In what order should fields be written? If you applied the naive approach of using Object.keys order, you’d get a table like

1  |  a
b  |  2

That probably doesn’t make sense. So header lets you control the order of the output columns. Each value in the header array is a key that is checked against each object. So if you pass the header ["Price", "Name"] you will see

1  |  a
2  |  b

But if you pass ["Name", "Price"] you will see

a  |  1
b  |  2

When a key is present in an object but missing from the header array, it is added to the end

1reaction
dottodotcommented, Apr 23, 2019

I’m having the same issue as above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

while using header option with XLSX.utils.json_to_sheet ...
The basic job of "header" option is not to override, rather just shift the starting option of the columns. i.e. any value passed...
Read more >
XLSX Json to Sheet with custom headers - StackBlitz
A angular-cli project based on rxjs, xlsx, core-js, zone.js, @angular/core ... XLSX.utils.sheet_add_aoa(ws, Heading);. //Starting in the second row to avoid.
Read more >
Utility Functions - SheetJS Community Edition
XLSX.utils.json_to_sheet takes an array of objects and returns a worksheet with automatically-generated "headers" based on the keys of the objects.
Read more >
Process Excel to json - Laracasts
I have this code that processes an excel file to a json array in order to import it ... mine so I'm not...
Read more >
docbits/82_util.md | sheetjs@v0.18.3 - Deno
XLSX.utils.aoa_to_sheet takes an array of arrays of JS values and returns a worksheet ... skipHeader, false, If true, do not include header row...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found