3

I have an issue where inside of my xlsx file I have applied styles like this: enter image description here

But inside of my object where I read the content from an xlsx file I only see this inside of my styles object:

   [
    {
        "patternType": "solid",
        "fgColor": {
            "rgb": "FFFF00"
        },
        "bgColor": {
            "rgb": "FFFF00"
        }
    }
]

Does anyone know why font styles are not being read, below you can see how I am parsing the xlsx data, do I need to add some more arguments or something else? Before styles were not being read at all, but when I added cellStyles to be true, then they were being read as you can see. I have read the docs, but I can't seem to find the reason why I can't read font styles.

This is how I am reading xlsx:

export function parseXLSXColumns(sheet) {
    return new Promise((resolve, reject) => {
        const rawColumns = [];
        // refer to Sheet JS for more in depth documentation
        // range is a an object with the following structure {s:{c:(some_number), r:(some_number)}, e:{c:(some_number), r:(some_number)}},
        // s stands for start of the range, e stands for end of the range
        // c stands for column index, r stands for row index
        const range = XLSX.utils.decode_range(sheet['!ref']);

        if (range.e.c > ALLOWED_COLUMN_COUNT_FOR_IMPORT) {
            reject([`Maximum number of columns is ${ALLOWED_COLUMN_COUNT_FOR_IMPORT}`]);
        }

        let rawColumnsIndex = 0;
        for (let xlsColumnIndex = range.s.c; xlsColumnIndex <= range.e.c; xlsColumnIndex += 1) {
            if (!rawColumns[rawColumnsIndex]) {
                // convert cell from column and row index to xlsx style of mapping ex: (c:0, r:0) => "A1"
                const headerCell = sheet[XLSX.utils.encode_cell({ c: xlsColumnIndex, r: 0 })] || '';
                rawColumns[rawColumnsIndex] = {
                    id: uuid(),
                    title: headerCell.w,
                    data: [],
                    styles: [],
                };
            }

            // start from the second item in the list, since the first item is used as column title
            for (let rowIndex = range.s.r + 1; rowIndex <= range.e.r; rowIndex += 1) {
                const cell = sheet[XLSX.utils.encode_cell({ c: xlsColumnIndex, r: rowIndex })] || { v: '' };
                rawColumns[rawColumnsIndex].data[rowIndex - 1] = cell.w;
                rawColumns[rawColumnsIndex].styles[rowIndex - 1] = cell.s;
            }

            rawColumnsIndex += 1;
        }

        rawColumns.forEach(column => {
            Object.assign(column, ColumnHelper.getColumnAnalysis(column));
            Object.assign(column, { type: column.possibleDataTypes[0] });
        });
        console.log(rawColumns);
        resolve(rawColumns);
    });
}

export function parseXLSX(data) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();

        reader.onload = e => {
            const fileData = e.target.result;
            const fileName = data.name.split('.')[0];
            const workBook = XLSX.read(fileData, { type: 'binary', cellStyles: true });

            resolve({ workBook, fileName });
        };

        reader.onerror = e => {
            reject(e);
        };

        reader.readAsBinaryString(data);
    });
}
ahmedskulj10
  • 381
  • 1
  • 15

0 Answers0