json_to_sheet should have an option to write down only columns, specified in headers option #2455

Open
opened 2021-11-17 17:10:42 +00:00 by Lonli-Lokli · 4 comments
Lonli-Lokli commented 2021-11-17 17:10:42 +00:00 (Migrated from github.com)
  const worksheet = XLSX.utils.json_to_sheet(datasource, {
          header: ['A']
        });

I was going to see only A column in the table, now it's just written as first and all others are listed later.
Can you introduce an option to exclude columns which are not listed in headers option?

``` const worksheet = XLSX.utils.json_to_sheet(datasource, { header: ['A'] }); ``` I was going to see only A column in the table, now it's just written as first and all others are listed later. Can you introduce an option to exclude columns which are not listed in headers option?
reviewher commented 2021-11-21 03:57:43 +00:00 (Migrated from github.com)

You can preprocess the data source to remove unnecessary data:

const headers = ["A"];
const aoo = datasource.map(obj => Object.fromEntries(headers.map(h => ([h, obj[h]])));
const worksheet = XLSX.utils.json_to_sheet(aoo, {
  header: headers
});

For broader compatibility, this can be done with simple array ops:

var aoo = datasource.map(function(obj) { 
  return headers.reduce(function(acc, key) {
    acc[key] = obj[key];
    return acc;
  }, ({}));
});
You can preprocess the data source to remove unnecessary data: ```js const headers = ["A"]; const aoo = datasource.map(obj => Object.fromEntries(headers.map(h => ([h, obj[h]]))); const worksheet = XLSX.utils.json_to_sheet(aoo, { header: headers }); ``` For broader compatibility, this can be done with simple array ops: ```js var aoo = datasource.map(function(obj) { return headers.reduce(function(acc, key) { acc[key] = obj[key]; return acc; }, ({})); }); ```
Lonli-Lokli commented 2021-11-21 11:27:04 +00:00 (Migrated from github.com)

@reviewher I don't agree with closing issue. From my point of view it should be part of feature libs, of course I can workaroind it. But it will take more time & memory to process.

@reviewher I don't agree with closing issue. From my point of view it should be part of feature libs, of course I can workaroind it. But it will take more time & memory to process.
reviewher commented 2021-11-21 19:12:41 +00:00 (Migrated from github.com)

What should this option be called?

How would it work if headers are not specified? Will it merely pull the headers from the first row?

What should this option be called? How would it work if headers are not specified? Will it merely pull the headers from the first row?
Lonli-Lokli commented 2021-11-21 20:14:52 +00:00 (Migrated from github.com)

For the simplicity

  1. As a proposal, it can be named 'whitelist': boolean
  2. It should be ignored if no headers specified
  3. Default value for backward compatibility should be false
For the simplicity 1. As a proposal, it can be named 'whitelist': boolean 2. It should be ignored if no headers specified 3. Default value for backward compatibility should be false
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#2455
No description provided.