natergj / excel4node

Node module to allow for easy Excel file creation
MIT License
1.38k stars 215 forks source link

writeToBuffer fails with UnhandledPromiseRejectionWarning for trivial workbook #307

Open pineapplemachine opened 4 years ago

pineapplemachine commented 4 years ago

Describe the bug

Calling workbook.writeToBuffer on a trivial workbook causes an opaque unhandled promise rejection error.

(node:34096) UnhandledPromiseRejectionWarning: Error: TypeError: Cannot read property 'toUpperCase' of undefined
    at toUpperCase (/my/project/directory/node_modules/excel4node/source/lib/utils.js:125:21)
    at getExcelRowCol (/my/project/directory/node_modules/excel4node/source/lib/utils.js:163:16)
    at Array.sort (<anonymous>)
    at sort (/my/project/directory/node_modules/excel4node/source/lib/worksheet/builder.js:152:34)
    at processRows (/my/project/directory/node_modules/excel4node/source/lib/worksheet/builder.js:186:13)
    at processNextRows (/my/project/directory/node_modules/excel4node/source/lib/worksheet/builder.js:189:9)
    at new Promise (<anonymous>)
    at _addSheetData (/my/project/directory/node_modules/excel4node/source/lib/worksheet/builder.js:144:12)
    at /my/project/directory/node_modules/excel4node/source/lib/worksheet/builder.js:548:19

To Reproduce

Repro case. Error does not occur if commenting out the worksheet.cell line.

const xl = require("excel4node");

async function main() {
    const workbook = new xl.Workbook();
    const worksheet = workbook.addWorksheet("Test");
    const headers = ["hello", "world", "etc"];
    for(let i = 0; i < headers.length; i++) {
        worksheet.cell(i, 0).string(headers[i]);
    }
    const buffer = await workbook.writeToBuffer();
    return buffer;
}

main().then(() => console.log("ok")).catch(console.error);

Expected behavior

Function should succeed in writing data to a buffer.

Environment:

pineapplemachine commented 4 years ago

So the cause of the error seems to be that the top-left cell is not (0, 0) but (1, 1); giving 0 for a cell coordinate causes the error. I imagine that this must be a common mistake - this really ought to produce a more informative error.

shortstuffsushi commented 3 years ago

Appears to be a duplicate of #139