Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing excel using Javascript

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.

like image 975
Prashant Dewan Avatar asked Dec 24 '22 23:12

Prashant Dewan


1 Answers

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.

like image 71
Prashant Dewan Avatar answered Jan 09 '23 03:01

Prashant Dewan