Sheetjs: Setting a table or cell style does not work

Created on 9 Mar 2017  路  16Comments  路  Source: SheetJS/sheetjs

Hi, The table style has been revised in accordance with xlsx-style , But it doesn't work . i dont know how to do.

This is my full code:

const saveAs = require('file-saver').saveAs
const XLSX = require('xlsx')

/**
 * export xlsx demo
 * @param  {[type]} data [description]
 * @return {[type]}       [description]
 */
module.exports = function (data) {
    // iterator data
    function sheet_from_array_of_arrays (data, options) {
        const ws = {} 
        const range = {
            s: {
                c: 1000000,
                r: 1000000
            },
            e: {
                c: 0,
                r: 0
            }
        }
        for ( let R = 0; R != data.length; ++R ) {
            for ( let 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

                // cell
                const cell = {
                    v: data[R][C]
                }

                // cell_ref
                const cell_ref = XLSX.utils.encode_cell({
                    c: C,
                    r: R
                })

                // type check
                if ( typeof cell.v === 'number') {
                    cell.t = 'n'
                    cell.s = {
                        numFmt: "0.00%"
                    }
                }
                else if (typeof cell.v === 'boolean') cell.t = 'b'
                else cell.t = 's'

                ws[cell_ref] = cell 
            }
        }

        if ( range.s.c < 1000000 ) ws['!ref'] = XLSX.utils.encode_range(range);
        return ws;
    }

    const sheet_name = 'sheetName' // sheet name

    // create xlsx object
    function Workbook () {
        if ( !( this instanceof Workbook ) ) return new Workbook()
        this.SheetNames = []
        this.Sheets = {}
    }

    // install wb object
    const wb = new Workbook()
    wb.SheetNames.push(sheet_name)
    wb.Sheets[sheet_name] = sheet_from_array_of_arrays(data)

    // write xlsx
    const wxbot = XLSX.write(wb, {
        type: 'binary',
        bookSST: true,
        bookType: 'xlsx',
        cellStyles: true
    })

    // to blob
    function sheet_to_blob (s) {
        const buf = new ArrayBuffer(s.length)
        const view = new Uint8Array(buf)
        for ( let i = 0; i != s.length; ++i ) view[i] = s.charCodeAt(i) & 0xFF
        return buf 
    }

    // export xlsx with file-saver
    saveAs(new Blob([sheet_to_blob(wxbot)], {
        type: 'application/octet-stream'
    }), 'test.xlsx')

}
Style

Most helpful comment

This is an issue with https://github.com/protobi/js-xlsx, when we add the full style support we will revisit this issue.

All 16 comments

@SheetJSDev help me please

@gongph how are you using the code and what error are you getting?

// step1
npm i xlsx-style --save
// step2 then add style code in `s` prop
cell.s = {
    numFmt: "0.00%"
}

And last, Open the test.xlsx style not render ! @SheetJSDev Is it the reason to use file-saver ?

This is an issue with https://github.com/protobi/js-xlsx, when we add the full style support we will revisit this issue.

xlsx-style is not yet integrated into the js-xlsx project and is not available yet, is it? @SheetJSDev

When will this issue be resolved?

Look forward to

+1

+1

+1

+1

+1

+1

+1

We offer this in the Pro compendium. Since companies have paid for the features already, it would be unfair to them if we turned around and made it available as open source. We have a longer comment in a gist.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gustavosimil picture gustavosimil  路  3Comments

jamesbillinger picture jamesbillinger  路  4Comments

Alex0007 picture Alex0007  路  3Comments

HachimDev picture HachimDev  路  3Comments

thomasledoux1 picture thomasledoux1  路  3Comments