How to add filter to excel created using js-xlsx plugin

I want to add a filter to an excel column that is created from a data array using the js-xlsx javascript plugin, but I have not found a way to add a filter to a table column.

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

I wrote this code to create a 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.sr > R) range.sr = R; if (range.sc > C) range.sc = C; if (range.er < R) range.er = R; if (range.ec < C) range.ec = 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.sc); if (range.sc < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); return ws; } 

and my data in the array is similar to below.

 [["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"]] 
+8
javascript excel js-xlsx
source share
1 answer

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

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

You can expand this range as per your requirement.

0
source share

All Articles