mmar
mmar

Reputation: 2020

commit error on exceljs

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..

Upvotes: 7

Views: 8113

Answers (2)

sankesh
sankesh

Reputation: 31

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.

Upvotes: 3

vjancic
vjancic

Reputation: 91

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.

Upvotes: 5

Related Questions