protobi / js-xlsx

XLSX / XLSM / XLSB (Excel 2007+ Spreadsheet) / ODS parser and writer
http://oss.sheetjs.com/js-xlsx
Other
817 stars 417 forks source link

Freezing the panes #86

Open gtskaushik opened 6 years ago

gtskaushik commented 6 years ago

Hi I am trying the freeze the panes. But it is not working. Please help me to achieve this.

I am using this module to download the file on client. Please find the code below:-

var wb = new Workbook() var ws = {}; var range = {s: {c:0, r:0}, 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], s: {} }; if(cell.v === null) continue; var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

                    //This is for the Header and timestamps cells
                    if(R==0 || C==0 || C==1){
                        cell.s = {
                            "fill": {
                                "patternType": "solid",
                                "bgColor": {rgb:'003d79'},
                                "fgColor": {rgb:'003d79'}
                            },
                            "font": {
                                "color": {rgb:'ffffff'},
                                "bold": true
                            }
                        }
                    }

                    if(typeof cell.v === 'number') {
                        cell.t = 'n';
                        cell['s']['numFmt'] = "0.00"//Included Number Format
                    }

                    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;
                }
            }
            ws['!ref'] = XLSX.utils.encode_range(range)//Setting the reference range of the sheet

            ws['!cols'] = []
            data[0].forEach(function(header){ ws['!cols'].push({"wch": header.length}) })//Setting Dynamic column width

            ws['!freeze'] = { xSplit: "1", ySplit: "1", topLeftCell: "B2", activePane: "bottomRight", state: "frozen" }//Freezing the columns

           wb.SheetNames.push(ws_name);
            wb.Sheets[ws_name] = ws;

            var wopts = { bookType:'xlsx', bookSST:false, type: "binary"};

            return XLSX.write(wb,wopts);
st-sloth commented 6 years ago

It doesn't seem that js-xlsx currently have that feature. I did the PR for that - #50 - but there is no response. You can use it from my branch like npm install st-sloth/js-xlsx#feature/pane-element (I won't delete it until a couple months after it gets merged) or fork it into your repository so you can ensure it stays the same as long as you need it. Usage is pretty much the same as in your code but with the !viewPane key. For more, one can look into modified docs.

gtskaushik commented 6 years ago

Thanks a lot @st-sloth. If possible could you let me know when your branch will be merged with the master? This will help me to communicate and plan accordingly within my team.

st-sloth commented 6 years ago

You can subscribe to the PR itself - #50. But sure, I'll comment here in #86 and in #51 once it's merged.

gtskaushik commented 6 years ago

Sure Thanks :-)