sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Formula use with cell object key f

Open satya-dash opened this issue 11 years ago • 5 comments

Hi Everyone.Can you please tell me how to put a formula in a cell of my excel like SUM(E2:E26).

I have some numbers in a column and want to add them with a SUM formula at the end of the data.So I tried to put the the formula in that particular cell but i couldn't.I am not able to use the cell object key "f" for the formula.So can anyone tell me how to use the formula in the excel so that my created Excel sheets will work more intelligently.

satya-dash avatar Jun 18 '15 09:06 satya-dash

Unfortunately, there isn't an official patch for writing formulas yet. There was a brief discussion in #152 about how best to implement formulas. Although there isn't an official fix from master, @mcsf made a patch that would solve the problem of formulae export: mcsf@d439645. I've used his patch in the past and it works pretty well so hopefully you won't have any issues with it. Good luck!

aravindkoneru avatar Jun 18 '15 17:06 aravindkoneru

@aravindkoneru Hey..i got the changes and override my file also accordingly but i don't know how to write the code for a formula.If you have done it previously can you please share a small code snippet of that so i can refer that one to write my own. Thanks

satya-dash avatar Jun 18 '15 18:06 satya-dash

Yeah, sure. I'm not at my computer right now, but I will post an example later tonight.

aravindkoneru avatar Jun 18 '15 20:06 aravindkoneru

Here's the example that you asked for. If everything is alright, A1 should have the formula A2+A3.

var xlsx = require('xlsx');

//workBook class
function Workbook() {
    if(!(this instanceof Workbook)) return new Workbook();
    this.SheetNames = [];
    this.Sheets = {};
}

var exportBook = new Workbook();

var worksheet = {};

var cell = {f: 'A2+A3'};

var cellRef = xlsx.utils.encode_cell({r:0, c:0});

var range = {s:{r: 0, c: 0},
            e: {r: 10, c: 10}};



worksheet[cellRef] = cell;
worksheet['!ref'] = xlsx.utils.encode_range(range);

exportBook.SheetNames.push('test');
exportBook.Sheets.test = worksheet;


xlsx.writeFile(exportBook, 'formula sample.xlsx');

aravindkoneru avatar Jun 18 '15 23:06 aravindkoneru

@aravindkoneru Thanks for the snippet.Now i got to know how to apply formula.Now i will play with this and ask you more questions when i will be in doubt.

satya-dash avatar Jun 19 '15 02:06 satya-dash