excel4node icon indicating copy to clipboard operation
excel4node copied to clipboard

writeToBuffer fails with UnhandledPromiseRejectionWarning for trivial workbook

Open pineapplemachine opened this issue 5 years ago • 2 comments

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:

  • OS: OSX 10.9
  • Node Version: 12.10.0
  • excel4node Version: 1.7.2

pineapplemachine avatar Jan 30 '20 12:01 pineapplemachine

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.

pineapplemachine avatar Jan 30 '20 13:01 pineapplemachine

Appears to be a duplicate of #139

shortstuffsushi avatar Feb 01 '21 17:02 shortstuffsushi