how to set formula for cell data for (export to .xlsx) SheetJS js-xlsx: https://github.com/SheetJS/js-xlsx
Asked Answered
L

2

6

Refering this example https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js , it doesn't have any implementation for cell formula in xlsx spreadsheet I tried using cell.f = "=SUM(A1+B1)" for the cell C1 and cell.v as the summation value which was 3. But i didn't succeed. With the exported file, when opened in MS excel, the cell contained just the data and when selected, didn't show any formula which i assigned in f(x) field.

Can someone post me a example which actually uses the functions/property '.f' and 'cellFormula'

Will be very helpful. I just need a working example with static values.

Lp answered 8/4, 2015 at 16:44 Comment(0)
H
17

The cell object has the property f, which is the formula you want to use. Here you can see all the options: https://github.com/SheetJS/js-xlsx#cell-object.

And here is an example of the use of formula:

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');

Here, A1 should have the formula A2+A3.

Hope it helps :)

Halonna answered 3/10, 2015 at 23:42 Comment(1)
I dont really get the use of "!ref". Can you please explain? I did a similar code and worksheet[cellRef] = cell; seems to be sufficientVindication
D
3

Here is my solution

function download() {
    TheResourceService.get({ xId: $stateParams.xId }, function(result) {
        var sheetName = 'first_sheet';
        var wopts = { bookType: 'xlsx', bookSST: true, type: 'binary' };
        var fileName = "the_excel_file.xlsx";

        var columns = ['id', 'name', 'point'];
        var data = [
            [1, 'Kyle', 20],
            [2, 'Allen', 32],
            [3, 'Chris', 18],
            [4, 'Tim', 11]
        ];

        var wb = XLSX.utils.book_new();
        var ws = uigrid_to_sheet(data, columns);

        ws['!ref'] = XLSX.utils.encode_range({
            s: { c: 0, r: 0 },
            e: { c: 3, r: 1 + data.length + 1 }
        });
        ws['C6'] = { f: 'SUM(C2:C5)' };
        ws['B6'] = { v: 'Total' };

        XLSX.utils.book_append_sheet(wb, ws, sheetName);
        var wbout = XLSX.write(wb, wopts);
        saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), fileName);
    })

}


function uigrid_to_sheet(data, columns) {
    var o = [],
        oo = [],
        i = 0,
        j = 0;

    /* column headers */
    for (j = 0; j < columns.length; ++j) oo.push((columns[j]));
    o.push(oo);

    /* table data */
    for (i = 0; i < data.length; ++i) {
        oo = [];
        for (j = 0; j < data[i].length; ++j) oo.push((data[i][j]));
        o.push(oo);
    }
    /* aoa_to_sheet converts an array of arrays into a worksheet object */
    return XLSX.utils.aoa_to_sheet(o);
}

function s2ab(s) {
    if (typeof ArrayBuffer !== 'undefined') {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    } else {
        var buf = new Array(s.length);
        for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
}
Disafforest answered 3/8, 2017 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.