Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add filter in excel generated using js-xlsx plugin

I want to add filter on excel column which is created from array of data using js-xlsx javascript plugin but I didn't find any way to add filter to table column.

Please help me if anyone knows how to add filter to columns in excel

I have written this code to create worksheet object

function createSheet(data, opts) {
        console.log(data, opts);
        var ws = {};
        var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}};
        for (var R = 0; R != data.length; ++R) {
            for (var C = 0; C != data[R].length; ++C) {
                if (range.s.r > R) range.s.r = R;
                if (range.s.c > C) range.s.c = C;
                if (range.e.r < R) range.e.r = R;
                if (range.e.c < C) range.e.c = C;
                var cell = {v: data[R][C]};
                if (cell.v == null) continue;
                var cell_ref = XLSX.utils.encode_cell({c: C, r: R});

                console.log(cell_ref);
                if (typeof cell.v === 'number') cell.t = 'n';
                else if (typeof cell.v === 'boolean') cell.t = 'b';
                else if (cell.v instanceof Date) {
                    cell.t = 'n';
                    cell.z = XLSX.SSF._table[14];
                    cell.v = dateNum(cell.v);
                }
                else cell.t = 's';

                ws[cell_ref] = cell;
                console.log(cell);
            }
        }
        console.log(range.s.c);
        if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
        return ws;
    }

and my data in array is like bellow

[["Rank","Country","Population","% of world population","Date"],["1","India","1,273,140,000","17.6%","June 24, 2015"],["2","Pakistan","190,156,000","2.62%","June 24, 2015"],["3","Nigeria","183,523,000","2.53%","July 1, 2015"],["4","Bangladesh","126,880,000","2.19%","June 24, 2015"]]
like image 226
Shree29 Avatar asked Jun 07 '16 13:06

Shree29


People also ask

Can JavaScript read XLSX file?

js is installed on your system. You can parse the excel files with the . xls and xlsx extensions using this library.


1 Answers

You can add the following line just before the return statement in your createSheet function.

ws['!autofilter']={ref:"A1:E1"};

You can extend these range as per your requirement.

like image 61
Karpak Avatar answered Oct 26 '22 06:10

Karpak