Working with Books and Worksheets — Add, Delete, Move, Rename

level: basic / verified: 2026-10-01 / tabs: Node.js・Python・Java・WebAssembly / 日本語

What you want to do

Add, delete, move and rename worksheets within a single book, walk sheets by index, and control which sheet is shown when the book opens (the active tab).

Methods covered

MethodBelongs toDescription
addWorkSheet(sheetName, position?)WorkBookAdds a worksheet.
deleteWorkSheet(sheetName)WorkBookDeletes a worksheet.
moveSheet(sheetName, position)WorkBookMoves a worksheet.
openWorkSheet(sheetName)WorkBookOpens a worksheet by name.
openWorkSheetByIndex(sheetIndex)WorkBookOpens a worksheet by index.
getSheetCount()WorkBookGets the number of worksheets.
getSheetNumber(sheetName)WorkBookGets the sheet number of a worksheet.
getName() / setName(name)WorkSheetGets / sets the sheet name.
isActive() / activate(isActive)WorkSheetGets / sets the active tab (the sheet shown when the book opens).

Code

Output:

// sheets.js — add, move, rename and delete sheets, plus index access
const { nodeosbxl } = require("nodeosbxl");

const app = new nodeosbxl.App();
const wb = app.createWorkBook("sheets.xlsx");

// Indexes are 1-based. Open 1..getSheetCount() in order and list the names
const show = (label) => {
    const names = [];
    for (let i = 1; i <= wb.getSheetCount(); i++) {
        names.push(wb.openWorkSheetByIndex(i).getName());
    }
    console.log(`${label} count=${wb.getSheetCount()} [${names.join(", ")}]`);
};

show("[1] after creation:");

wb.addWorkSheet("Summary");                     // position omitted → appended at the end
show("[2] addWorkSheet(Summary):");

wb.addWorkSheet("Orders", 1);                   // position=1 → inserted at the front
show("[3] addWorkSheet(Orders,1):");

wb.moveSheet("Orders", 3);                      // move to position 3
show("[4] moveSheet(Orders,3):");

const ws = wb.openWorkSheet("Orders");
console.log("[5] getName() =", ws.getName(), "/ getSheetNumber(Orders) =", wb.getSheetNumber("Orders"));
ws.getRange("A1:A1").setValue("order data");

wb.openWorkSheet("Sheet1").setName("Details");  // rename
show("[6] setName(Details):");

// --- active tab (the sheet shown in front when the book is opened) ---
console.log("[7] isActive: Summary =", wb.openWorkSheet("Summary").isActive(),
            "/ Details =", wb.openWorkSheet("Details").isActive());
wb.openWorkSheet("Summary").activate(true);  // the other sheets are deactivated automatically
console.log("    after activate: Summary =", wb.openWorkSheet("Summary").isActive(),
            "/ Details =", wb.openWorkSheet("Details").isActive());

// --- delete ---
wb.addWorkSheet("Temp");
wb.deleteWorkSheet("Temp");
show("[8] deleteWorkSheet(Temp):");

wb.save();
wb.close();

// --- reopen and verify ---
const wb2 = app.openWorkBook("sheets.xlsx");
const names = [];
for (let i = 1; i <= wb2.getSheetCount(); i++) names.push(wb2.openWorkSheetByIndex(i).getName());
console.log("[End] sheets:", names.join(", "));
console.log("     Orders!A1 =", wb2.openWorkSheet("Orders").getRange("A1:A1").getValue()["A1"]);
wb2.close();
[1] after creation: count=1 [Sheet1]
[2] addWorkSheet(Summary): count=2 [Sheet1, Summary]
[3] addWorkSheet(Orders,1): count=3 [Orders, Sheet1, Summary]
[4] moveSheet(Orders,3): count=3 [Sheet1, Summary, Orders]
[5] getName() = Orders / getSheetNumber(Orders) = 3
[6] setName(Details): count=3 [Details, Summary, Orders]
[7] isActive: Summary = false / Details = true
    after activate: Summary = true / Details = false
[8] deleteWorkSheet(Temp): count=3 [Details, Summary, Orders]
[End] sheets: Details, Summary, Orders
     Orders!A1 = order data

Notes

Caveats

See also