exceljs / exceljs

Excel Workbook Manager
MIT License
13.77k stars 1.76k forks source link

worksheet.commit is not a function #860

Closed anstosa closed 4 years ago

anstosa commented 5 years ago

I'm sure I'm being an idiot and it's too late to be programming but I can't see it. plz help.

   //...
    const workbook = new Excel.Workbook();
    const worksheet = workbook.addWorksheet(period, {
        views: [{ xSplit: 1 }],
        properties: { defaultRowHeight: 32 },
        pageSetup: { verticalCentered: true },
    });
    worksheet
        .addRow(['Date', 'Initials', 'Name', 'Period', 'Wines', 'Notes'])
        .commit();
    worksheet.getRow(0).font = { bold: true };
    _.each(invoices, (invoice) => {
        let notes = [];
        if (invoice.status !== 'paid') {
            notes.push('Not Paid!');
            if (_.get(invoice.customer.metadata, 'pays_on_pickup') !== 'true') {
                notes.push('Update CC#');
            }
        }
        worksheet
            .addRow([
                '',
                '',
                invoice.customer.name,
                period,
                invoice.metadata.hold_code,
                notes.join(', '),
            ])
            .commit();
    });
    worksheet.commit(); // <== ERROR HERE
    response.setHeader(
        'Content-Type',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    );
    response.setHeader(
        'Content-Disposition',
        `attachment; filename=Holds Pickup List ${period}.xlsx`
    );
    response.end(await workbook.xlsx.writeBuffer());
SimantoR commented 4 years ago

I am facing the exact same problem:

  saveToExcel = (path: string, data: IItem[]) => {
    let workbook = new Excel.Workbook();
    workbook.creator = 'StorePro';
    workbook.lastModifiedBy = 'Her';
    workbook.created = Date.today();
    workbook.modified = null;
    workbook.lastPrinted = new Date(2016, 9, 27);

    workbook.views = [{
      x: 0, y: 0, width: 10000, height: 20000,
      firstSheet: 0, activeTab: 1, visibility: 'visible'
    }];

    let ws1 = workbook.addWorksheet(
      'Datasheet',
      { pageSetup: { paperSize: 9, orientation: 'landscape' } }
    );

    data.map(x => {
      ws1.addRow(x).commit();
    });

    ws1.commit(); // PROBLEM HERE

    const writeStream = fs.createWriteStream(path);

    workbook.xlsx.writeBuffer({ stream: writeStream, useStyles: true, useSharedStrings: true })

    writeStream.close();
  }

I get the following error:

TypeError: ws1.commit is not a function
    at ViewInventory._this.saveToExcel (webpack:///./src/components/Inventory/ViewInventory.tsx?:122)
    at eval (webpack:///./src/components/Inventory/ViewInventory.tsx?:95)
anstosa commented 4 years ago

Oh I figured this out and forgot to close. commit() was not necessary for my workflow after all. I remove it from the rows and the worksheet and writing the buffer to the return stream worked just fine 🤷‍♂️

b-asaf commented 4 years ago

Hey @anstosa, similar issue happen to me but with the error: TypeError: T.createWriteStream is not a function. I am using : "exceljs": "^3.4.0" My code is:

import Excel from 'exceljs';
// OR
import * as Excel from 'exceljs';

export default () => {
    const workbook = new Excel.Workbook();
    const worksheet = workbook.addWorksheet('My Sheet');

    worksheet.columns = [
        { header: 'Id', key: 'id', width: 10 },
        { header: 'Name', key: 'name', width: 32 },
        { header: 'D.O.B.', key: 'dob', width: 15 },
    ];

    worksheet.addRow({ id: 1, name: 'John Doe', dob: new Date(1970, 1, 1) });
    worksheet.addRow({ id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7) });

    // save under export.xlsx
    workbook.xlsx.writeFile('export.xlsx');

Maybe you encountered in a similar error?


UPDATE After additional research I understood that workbook.xlsx.writeFile is not support currently from client side so I am using writeBuffer and saving it with file-saver package instead.