Is it possible to have custom column on json_to_sheet? #771

Closed
opened 2017-08-08 19:53:37 +00:00 by enniob · 5 comments
enniob commented 2017-08-08 19:53:37 +00:00 (Migrated from github.com)

I want to add custom columns and not use the default object column name. Is this possible to do it?

I want to add custom columns and not use the default object column name. Is this possible to do it?
SheetJSDev commented 2017-08-08 20:14:42 +00:00 (Migrated from github.com)

If you want to change the order, set the header option to an array of the keys:

var data = [{abc:1,def:2}];
/* force the order def, abc */
var ws = XLSX.utils.json_to_sheet(data, {header:['def','abc']});
/* force the order abc, def */
var ws = XLSX.utils.json_to_sheet(data, {header:['abc','def']});

The keys are written to the first row of the resulting worksheet, so if the order is correct you can set the first row values. For example, to rename the first column to be "ABC":

var data = [{abc:1,def:2}];
/* force the order abc, def */
var ws = XLSX.utils.json_to_sheet(data, {header:['abc','def']});
/* The first column header is stored in cell A1  */
var cell = ws['A1'];
/* To assign a new value, set the `v` key and clobber the old formatted text */
delete ws['A1'].w; ws['A1'].v = "ABC";

Just noticed the documentation does not describe the options, will update shortly.

If you want to change the order, set the `header` option to an array of the keys: ```js var data = [{abc:1,def:2}]; /* force the order def, abc */ var ws = XLSX.utils.json_to_sheet(data, {header:['def','abc']}); /* force the order abc, def */ var ws = XLSX.utils.json_to_sheet(data, {header:['abc','def']}); ``` The keys are written to the first row of the resulting worksheet, so if the order is correct you can set the first row values. For example, to rename the first column to be "ABC": ```js var data = [{abc:1,def:2}]; /* force the order abc, def */ var ws = XLSX.utils.json_to_sheet(data, {header:['abc','def']}); /* The first column header is stored in cell A1 */ var cell = ws['A1']; /* To assign a new value, set the `v` key and clobber the old formatted text */ delete ws['A1'].w; ws['A1'].v = "ABC"; ``` Just noticed [the documentation does not describe the options](https://docs.sheetjs.com/#array-of-objects-input), will update shortly.
enniob commented 2017-08-09 15:10:48 +00:00 (Migrated from github.com)

When I do this I get the following error
var ws = XLSX.utils.json_to_sheet(data, {header:['def','abc']});

ERROR in C:/myapp/angular/src/app/topics-view/topics-view.component.ts (590,64): Argument of type '{ header: string[]
; }' is not assignable to parameter of type 'JSON2SheetOpts'.
Object literal may only specify known properties, and 'header' does not exist in type 'JSON2SheetOpts'

When I do this I get the following error `var ws = XLSX.utils.json_to_sheet(data, {header:['def','abc']});` ERROR in C:/myapp/angular/src/app/topics-view/topics-view.component.ts (590,64): Argument of type '{ header: string[] ; }' is not assignable to parameter of type 'JSON2SheetOpts'. Object literal may only specify known properties, and 'header' does not exist in type 'JSON2SheetOpts'
SheetJSDev commented 2017-08-09 19:30:56 +00:00 (Migrated from github.com)

The type definition is missing, it will show up in the next update

The type definition is missing, it will show up in the next update
rajuyadav-orchestratec commented 2018-01-07 05:02:41 +00:00 (Migrated from github.com)

Hi all,
How I can get data in the following format

Employee Details
Emp Name Emp Sal
xyz 1000
abc 2000

Any help would be appreciated.

Hi all, How I can get data in the following format **Employee Details** Emp Name Emp Sal xyz 1000 abc 2000 Any help would be appreciated.
sunny7899 commented 2019-02-06 13:26:09 +00:00 (Migrated from github.com)

I also want to know how to format the above data @rajuyadav-orchestratec

I also want to know how to format the above data @rajuyadav-orchestratec
Sign in to join this conversation.
No Milestone
No Assignees
1 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#771
No description provided.