10

我想在使用 js-xlsx javascript 插件从数据数组创建的 excel 列上添加过滤器,但我没有找到任何方法将过滤器添加到表列。

如果有人知道如何将过滤器添加到excel中的列,请帮助我

我已经编写了这段代码来创建工作表对象

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;
    }

我的数组中的数据如下所示

[["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"]]
4

1 回答 1

2

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.

于 2017-06-18T01:41:03.667 回答