Skip to content

Instantly share code, notes, and snippets.

@davidcsejtei
Created June 28, 2022 08:58
Show Gist options
  • Save davidcsejtei/dde51ecd8eee9908961e1d54d55e2fa5 to your computer and use it in GitHub Desktop.
Save davidcsejtei/dde51ecd8eee9908961e1d54d55e2fa5 to your computer and use it in GitHub Desktop.
Delete a selected worksheet (or tab) from an excel file in Node.js
const xlsx = require('xlsx');
const deleteWorksheet = (filePath, workSheetName) => {
const workBook = xlsx.readFile(filePath);
const workSheetNames = Object.keys(workBook.Sheets);
if (workSheetNames.includes(workSheetName)) {
delete workBook.Sheets[workSheetName];
delete workBook.SheetNames[workSheetName];
indexToDelete = workBook.SheetNames.indexOf(workSheetName);
workBook.SheetNames.splice(indexToDelete, 1);
xlsx.writeFile(workBook, filePath);
}
}
module.exports = deleteWorksheet;
@davidcsejtei
Copy link
Author

davidcsejtei commented Jun 28, 2022

How to use the delete function:

const workSheetName = 'Users';
const filePath = './outputFiles/excel-from-js.xlsx';
deleteWorksheet(filePath, workSheetName);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment