Sheetjs: Need to export only selected columns from the JSON, how?

Created on 29 May 2018  路  3Comments  路  Source: SheetJS/sheetjs

Hi,

I have the json with 5 columns A,B,C,D,E.
A B C D E
1 1 1 1 1
1 1 1 1 1

In export, i want to export only 2 columns and the value. Like below,
A B
1 1
1 1
Can anyone please help me for these.

Thanks in Advance.
Sudhakar

Most helpful comment

There's no option to filter headers, and since it's such a nuanced use case we are unlikely to add it, but you can perform a map on the resulting array to pick the individual headers you want:

var data = [{A:1,B:2,C:3,D:4,E:5}, {A:1,B:1,C:1,D:1,E:1}]; // data from `sheet_to_json`

// ES7
var filtered = data.map(({A,B}) => ({A,B}));
// [ { A: 1, B: 2 }, { A: 1, B: 1 } ]

// ES5
var filtered = data.map(function(x) { return {A:x.A, B:x.B}; })
// [ { A: 1, B: 2 }, { A: 1, B: 1 } ]

All 3 comments

There's no option to filter headers, and since it's such a nuanced use case we are unlikely to add it, but you can perform a map on the resulting array to pick the individual headers you want:

var data = [{A:1,B:2,C:3,D:4,E:5}, {A:1,B:1,C:1,D:1,E:1}]; // data from `sheet_to_json`

// ES7
var filtered = data.map(({A,B}) => ({A,B}));
// [ { A: 1, B: 2 }, { A: 1, B: 1 } ]

// ES5
var filtered = data.map(function(x) { return {A:x.A, B:x.B}; })
// [ { A: 1, B: 2 }, { A: 1, B: 1 } ]

@SheetJSDev Thanks for the update.

Now It's working fine.

hi,

I can't filter
Because I can't get rid of double quotes ("first_name") in the header
$('#input-excel').change(function (e) {
var reader = new FileReader();
reader.readAsArrayBuffer(e.target.files[0]);
reader.onload = function (e) {
var data = new Uint8Array(reader.result);

    var wb = XLSX.read(data, { type: 'array' });
    var select = document.getElementById("rec_mode");
    let sheetnames = wb.SheetNames

    for (index in sheetnames) {
      var x = select.options[select.options.length] = new Option(sheetnames[index], index);
    }

    select.addEventListener('change', function () {


      var box_select = document.getElementById("select_box")
      let page_name = select.options[select.selectedIndex].text
      var jsondata = wb.Sheets[`${page_name}`]

      var headers = [];
      var range = XLSX.utils.decode_range(jsondata['!ref']);

      var C, R = range.s.r
      for (C = range.s.c; C <= range.e.c; ++C) {
        var cell = jsondata[XLSX.utils.encode_cell({ c: C, r: R })]
        var hdr = "UNKNOWN " + C;
        if (cell && cell.t) hdr = XLSX.utils.format_cell(cell);
        headers.push(hdr);


      }

      for (index in headers) {

        var options_data = box_select.options[box_select.options.length] = new Option(headers[index], index)

      }



      box_select.addEventListener('change', function () {

        var data_i = box_select.options[box_select.selectedIndex].text
        console.log(data_i);
        var dd = XLSX.utils.sheet_to_json(jsondata, { defval: "" });




        var filtered = dd.map(({ data_i }) => ({ data_i }))

        console.log(filtered);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DannyRyman picture DannyRyman  路  3Comments

magtuan picture magtuan  路  3Comments

nishthasb picture nishthasb  路  4Comments

seanmcilvenna picture seanmcilvenna  路  3Comments

mmancosu picture mmancosu  路  3Comments