ExcelUtils
Excel read and write operations using ExcelJS: includes examples for reading data, finding cells by value, updating cells with column offsets, and dynamic search-and-replace functionality.
Overview
This project demonstrates how to work with Excel files using the ExcelJS library in Node.js. It includes multiple examples for common operations:
- Reading data from Excel sheets
- Finding cells by value
- Updating cells with row/column offsets
- Search and replace with dynamic offsets
Dependencies
exceljs: ^4.4.0 - Excel file processing library supporting XLSX, XLS, CSV, and JSON formats
Setup
Install dependencies:
npm installRunning Examples
Update the file paths in each script to match your Excel file location, then run:
# Read Excel file
node ExcelReadWriteLogic/excelDemo.js
# Write/Search with offset
node ExcelReadWriteLogic/ReadWriteDynamicSearch.js
# Search and replace (hardcoded value)
node ExcelReadWriteLogic/WriteDataExcelSenario2.js
# Search and replace (with offset)
node ExcelReadWriteLogic/demo.jsLine-by-Line Explanations
ReadWriteDynamicSearch.js
The main flow: load workbook -> find a value -> offset the cell -> update -> save.
const ExcelJS = require('exceljs');- Import ExcelJS libraryasync function writeExcelFile(searchText, replaceText, chnage, filePath)- Function to find and update a cell with optional offsetconst workbook = new ExcelJS.Workbook();- Create a new workbook instanceawait workbook.xlsx.readFile(filePath)- Load Excel file asynchronouslyconst worksheet = workbook.getWorksheet('Sheet1');- Get the worksheet named 'Sheet1'const output = await readExcel(worksheet, searchText)- Call helper function to locate the search textconst cell = worksheet.getCell(output.row, output.col+chnage.colChange)- Get cell at found coordinates, plus column offsetcell.value = replaceText- Set new value to the cellawait workbook.xlsx.writeFile(filePath)- Save workbook back to fileasync function readExcel(worksheet, searchText)- Helper function to scan and find a valueworksheet.eachRow((row, rowNumber) => { ... })- Iterate through each rowrow.eachCell((cell, colNumber) => { ... })- Iterate through each cell in rowif (cell.value === searchText)- Check if cell matches search textoutput.row = rowNumber; output.col = colNumber;- Store the coordinates when foundreturn output;- Return the found coordinates
excelDemo.js
Basic read example that prints all cell values.
const ExcelJS = require('exceljs');- Import ExcelJSasync function readExcelFile()- Async function to read sheetconst workbook = new ExcelJS.Workbook();- Create workbookawait workbook.xlsx.readFile(filePath)- Read file into memory (I/O is async)const worksheet = workbook.getWorksheet('Sheet1');- Get worksheetworksheet.eachRow((row, rowNumber) => { ... })- Iterate rowsrow.eachCell((cell, colNumber) => { ... })- Iterate cellsconsole.log(cell.value);- Print each cell valuereadExcelFile();- Execute the function
WriteDataExcelSenario2.js
Find a specific hardcoded value and update it with a new value.
let output = {row:-1, col:-1};- Initialize output object to track found cellasync function writeExcelFile()- Function to search and replaceconst workbook = new ExcelJS.Workbook();- Create workbookawait workbook.xlsx.readFile(filePath)- Load fileconst worksheet = workbook.getWorksheet('Sheet1');- Get worksheetworksheet.eachRow(...)- Loop through rowsif (cell.value === "Apple")- Check for hardcoded value "Apple"output.row = rowNumber; output.col = colNumber;- Store coordinatesconst cell = worksheet.getCell(output.row, output.col)- Get the found cellcell.value = ("Aayush Mishra")- Update with new valueawait workbook.xlsx.writeFile(filePath)- Save filewriteExcelFile();- Execute
demo.js
Similar to ReadWriteDynamicSearch.js but without comments. Demonstrates search with offset capability.
Ideas and Improvements
- Add error handling: Check if the searched value is found before updating (avoid updating -1,-1 coordinates)
- Support multiple sheets: Allow specifying sheet name as parameter instead of hardcoding 'Sheet1'
- Batch operations: Add ability to find and replace multiple values in one operation
- CSV/JSON support: ExcelJS also supports CSV and JSON, could add examples
- Fix typo in variable names: Rename
chnagetochangeandrowchagetorowChangefor clarity - Create a utility module: Extract common functions into a reusable module
- Add logging: Implement better logging for debugging and monitoring
- Support formulas: Add examples of reading/writing formulas, not just values
- Styling: Add examples of formatting cells (color, font, borders)
- Validation: Add input validation for file paths and sheet names
Notes
- All scripts hardcode file paths. Update these to match your system's paths before running.
- The library works with async/await for file I/O operations - all read/write operations are asynchronous.
- Make sure the Excel file has a sheet named 'Sheet1' or update the sheet name in the code.
- ExcelJS supports XLSX, XLS, CSV, and JSON formats automatically based on file extension.