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.

Change header´s title when using json_to_sheet

See original GitHub issue

Hello,

I would like to know, if there is any way how to change header´s title when using json_to_sheet function. Now I am gettings object´s keys and I would like to change it to something more readable.

Thank you very much.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

46reactions
SheetJSDevcommented, Oct 27, 2020

There’s no json_to_xlsx function, you probably mean json_to_sheet.

That being said, the easiest way is to change the worksheet after the fact. We’ll start from a simple example:

> var data = [{name:"Sheet", age: 12}, {name:"JS", age: 24}]
> var ws = XLSX.utils.json_to_sheet();
> ws
{ A2: { t: 's', v: 'Sheet' },
  B2: { t: 'n', v: 12 },
  A3: { t: 's', v: 'JS' },
  B3: { t: 'n', v: 24 },
  A1: { t: 's', v: 'name' },
  B1: { t: 's', v: 'age' },
  '!ref': 'A1:B3' }

The headers are automatically inserted in the first row (the A1/B1 from above). You can directly change the values. Let’s say “name” -> “Name”:

ws.A1.v = "Name";

If you want to loop through every header and make them uppercase:

var range = XLSX.utils.decode_range(ws['!ref']);
for(var C = range.s.c; C <= range.e.c; ++C) {
  var address = XLSX.utils.encode_col(C) + "1"; // <-- first row, column number C
  if(!ws[address]) continue;
  ws[address].v = ws[address].v.toUpperCase();
}
11reactions
jdhinescommented, Feb 22, 2019

I don’t think that kind of formatting is possible, but to address your earlier question, the way I did it, since I also needed to change the order of the columns sometimes (although I know you can use the headers prop to do it), was to just map the keys of the JSON object to new keys:

newJson = oldJson.map(rec => {
  return {
    'Last Name': rec.lastName,
    'First Name': rec.firstName,
    ...
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

SheetJS json_to_sheet renaming headers - Stack Overflow
I'm sharing the solution that I found // Using same variables as the above answer var Heading = [ ["FirstName", "Last Name", "Email"],...
Read more >
XLSX Json to Sheet with custom headers - StackBlitz
A angular-cli project based on rxjs, xlsx, core-js, zone.js, ... import { Component } from '@angular/core'; ... XLSX.utils.sheet_add_aoa(ws, Heading);.
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 >
Example: Pandas Excel output with user defined header format
An example of converting a Pandas dataframe to an Excel file with a user ... 'Longer heading that should be wrapped' : data})...
Read more >
Create Excel from JSON in Angular 9/8 using Exceljs Tutorial ...
This is an awesome library to convert JSON data in formatted and ... We can easily create XLSX files with formatted headers, footers,...
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