Sheetjs: Excel: Found a problem with some content in file

Created on 30 Nov 2020  路  2Comments  路  Source: SheetJS/sheetjs

I haven't been able to find a good way to debug this error. The file seems to be malformed, but I can't find anything wrong with the data being input.
I'm using the following function to abstract it.

function excel (data, options= {excel:{WorkBook:{}}}) {
    const {excel} = options;
    const wb = utils.book_new();
    wb.Props = {Company: 'My Company', CreatedDate:Date.now(), ...excel.WorkBook.Props};
    wb.Custprops = excel.WorkBook.Custprops || {};
    wb.Workbook = excel.WorkBook.Workbook || {};

    for (const key in data) {
        const element = data[key];
        utils.book_append_sheet(wb,utils.json_to_sheet(element,element?.sheetOptions || {}),key);
    }

    const now = new Date().toLocaleString([],{hour12:false,dateStyle:'short',timeStyle:'medium'}).replace(', ','_').replace(/[/ :]/g,'-');
    writeFile(wb,excel.fileName || `${excel.title}_${now}.xlsx`,{bookType:'xlsx'});
}

And for testing I've just been calling it with excel({'My Sheet':[{'Column 1':'hello','Column 2': 'hello world'}]}) and still been getting the error.
Here's an example file if it helps:
undefined_11-30-20_10-32-26.xlsx

Most helpful comment

Thanks for updating the file! The issue is with

CreatedDate:Date.now()

Date.now() is a number. When we write the properties, we don't check that the created date is a Date -- the value is directly written. The XML has something like

<dcterms:created>1606750346235</dcterms:created>

If you want to mark the created date when you do the write, pass a date object. You can either use new Date() or new Date(Date.now()) to create the object.

fiddle: https://jsfiddle.net/jot943mz/

All 2 comments

Google Drive seems to be messing with the file, can you upload it here? If you open the issue page in your web browser, you should be able to drag-and-drop the file into the text input

Thanks for updating the file! The issue is with

CreatedDate:Date.now()

Date.now() is a number. When we write the properties, we don't check that the created date is a Date -- the value is directly written. The XML has something like

<dcterms:created>1606750346235</dcterms:created>

If you want to mark the created date when you do the write, pass a date object. You can either use new Date() or new Date(Date.now()) to create the object.

fiddle: https://jsfiddle.net/jot943mz/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sudhakar-sekar picture sudhakar-sekar  路  3Comments

Alex0007 picture Alex0007  路  3Comments

gustavosimil picture gustavosimil  路  3Comments

happy0088 picture happy0088  路  3Comments

m-ketan picture m-ketan  路  3Comments