I have 1 excel contains multiple sheets like below screenshot:
I am trying to read excel file using ExcelJS module. https://www.npmjs.com/package/exceljs#exceljs
const Excel = require("exceljs");
const workbook = new Excel.Workbook();
const filePath = `./test/CustomerData.xlsx`
workbook.xlsx.readFile(filePath)
.then(function() {
let worksheet = workbook.getWorksheet(0);
console.log("Sheet name---", worksheet);
});
It gives output as Sheet name--- undefined
I want sheet names like:
Sheet name---CustomerData1
Sheet name---CustomerData2
getWorksheet uses an ID rather than an index. In the example below, I observed the ID field starting from 1. From the docs (https://www.npmjs.com/package/exceljs#access-worksheets), the ID isn't guaranteed to start at 1, so you may want to loop through it like below:
const Excel = require("exceljs");
const workbook = new Excel.Workbook();
const filePath = `./test/CustomerData.xlsx`
workbook.xlsx.readFile(filePath).then(() => {
console.log("Sheet name---", workbook.getWorksheet(1).name)
console.log("Sheet name---", workbook.getWorksheet(2).name)
workbook.worksheets.forEach((sheet, i) => {
console.log("Array index:", i)
console.log("exceljs id:", sheet.id)
console.log("Sheet name---", sheet.name)
})
});
My example produced the following:
Sheet name--- CustomerData1
Sheet name--- CustomerData2
Array index: 0
exceljs id: 1
Sheet name--- CustomerData1
Array index: 1
exceljs id: 2
Sheet name--- CustomerData2