How do you edit a specific cell in an excel file using JavaScript?
I am trying to do it using ExcelJS but having a hard time in writing to my excel file.
How do you edit a specific cell in an excel file using JavaScript?
I am trying to do it using ExcelJS but having a hard time in writing to my excel file.
Thanks for the reply I got it working and here is my code.
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
workbook.xlsx.readFile('file.xlsx')//Change file name here or give file path
.then(function() {
var worksheet = workbook.getWorksheet('sheet');
var i=1;
worksheet.eachRow({ includeEmpty: false }, function(row, rowNumber) {
r=worksheet.getRow(i).values;
r1=r[2];// Indexing a column
console.log(r1);
i++;
});
worksheet.getCell('B3').value = "abc";//Change the cell number here
return workbook.xlsx.writeFile('file.xlsx')//Change file name here or give file path
});
I was missing this return statement that saves my edited file.
Here are a couple examples found in the exceljs documentation that may be helpful. https://www.npmjs.com/package/exceljs
// Modify/Add individual cell
worksheet.getCell('C3').value = new Date(1968, 5, 1);
// write to a file
var workbook = createAndFillWorkbook();
workbook.xlsx.writeFile(filename)
.then(function() {
// done
});