7

I have tried creating an excel, but getting a commit error. Whats wrong in my code. If i remove the sheet.commit() and workbook.commit() program works fine.

const Excel = require('exceljs');
const fs = require('fs');

const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet("MySheet");
const writeToExcel = fs.createWriteStream("./test/testfile.xlsx");

sheet.columns = [
    { header: 'Id', key: 'id', width: 10 },
    { header: 'Name', key: 'name', width: 40 },
    { header: 'DOB', key: 'dob', width: 10, outlineLevel: 1}
];


let names = ['Windows', 'Mac Os', 'Ubuntu', 'B OS'];
let i = 2;

names.forEach( (singleName) => {
    let row = sheet.getRow(i);
    row.values = {
        id: i-1,
        name: singleName,
        dob: new Date()
    };
    row.commit();
    i++;
});

sheet.commit();
workbook.commit();

workbook.xlsx.write(writeToExcel)
 .then( (response) => {
    console.log("Excel file is created with data.");
 })
 .catch( (error) => {
    console.log("Some problem in creating an excel file. Please check for errors...");
 });

below is the error.

λ node excel.js
c:\Personal\node_projects\node-js-playlist-master\excel.js:29
sheet.commit();
      ^

TypeError: sheet.commit is not a function
    at Object.<anonymous> (c:\Personal\node_projects\node-js-playlist-master\excel.js:29:7)
    at Module._compile (module.js:624:30)

if i remove the sheet commit, error is thrown on workbook commit line. Any help is useful here..

2
  • any help is appreciable. Commented Oct 10, 2017 at 14:56
  • any help on this please.... Commented Nov 30, 2017 at 16:31

2 Answers 2

5

don't know if you found your answer.

The only objects that have a commit function are rows and XLSX streams.

You already commit every row with

  row.commit();

Sheets by themselves have no commit function, rows do. What you probably found in examples was something like:

  sheet.addRow([...]).commit();

However, that is not commit on the sheet but on the row.

As for the workbook commit. You've probably seen this example of a commit, but again that is a commit on a XLSX stream, not the workbook itself.

Sign up to request clarification or add additional context in comments.

1 Comment

But documentation suggests otherwise As each worksheet is completed, it must also be committed: // Finished adding data. Commit the worksheet worksheet.commit();
3

You don't need the commit() call.

Just remove below lines. and it it work

sheet.commit();
workbook.commit();
row.commit();

The commit() function is only needed for a streaming writer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.