Created
June 28, 2022 08:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use the delete function:
const workSheetName = 'Users';
const filePath = './outputFiles/excel-from-js.xlsx';
deleteWorksheet(filePath, workSheetName);