Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

javascript - how to delete a worksheet from a workbook in Node.js? // TypeError: Cannot read property 'delete' of undefined

I need to delete a worksheet from a workbook of an Excel in Node.js. I use the SheetJs (I would preferable need an answer using this one, but I highly appreciate all of the other possibilities, I can change). Having searching for days, but could not find the solution.

So I have this (if you need code for test):

const xlsx = require('xlsx');
workBook = xlsx.readFile("todo-list.xlsx", {cellDates:true});
const headerData = ["Id", "Name", "Description", "Due Date", "Priority", "Status", "Notes"];
const workSheet = xlsx.utils.aoa_to_sheet([headerData]);
xlsx.utils.book_append_sheet(workBook, workSheet,"TO-DEL");

and I tried this but it does not work:

workSheet.delete();

or this...

workBook.Sheets["TO-DEL"].delete();

this is how it ends:

xlsx.writeFile(workBook, 'todo-list.xlsx');

The error is: TypeError: Cannot read property 'delete' of undefined

Do you have any idea or experience with this so you can help me please? Highly appreciate any help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this works perfect:

const Excel = require('exceljs');

const workbook = new Excel.Workbook();
workbook.xlsx.readFile("todo-list.xlsx").then(function() {
        const worksheet = workbook.getWorksheet("Sheet1");
        console.log(worksheet);
        workbook.removeWorksheet(worksheet.id);
        return workbook.xlsx.writeFile( "todo-list.xlsx");
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...