Search code examples
node.jsexceljs

ExcelJS - How to set active the first sheet


i am trying to write an excel file with multiple sheets. but somehow i struggle to find the syntax to set the excel file active on the first sheet when we open the file.

Currently it is always active on the last sheet. here is snippet of my code

let workbook = new Excel.Workbook();
workbook.creator = 'PT. ABCDE';
workbook.lastModifiedBy = 'PT. ABCDE';
workbook.created = new Date();
workbook.modified = new Date();
workbook.lastPrinted = new Date();
workbook.properties.date1904 = true;

let worksheetFarmer = workbook.addWorksheet('Farmer');
dataFarmer.forEach(function(item, index) {
    worksheetFarmer.addRow(item);
}

let worksheetFamily = workbook.addWorksheet('Family');
dataFamily.forEach(function(item, index) {
    worksheetFamily.addRow(item);
}

workbook.views = [
    {
        x: 0, y: 0, width: 10000, height: 20000,
        firstSheet: 0, activeTab: 1, visibility: 'visible'
    }
];
const PathFilename = config.get('palmWritePath')+ExcelName+'.xlsx';
workbook.xlsx.writeFile(PathFilename).then(function() {
    const returnJson = {
        success: true,
        message: "Excel exported",
        filename: ExcelName+'.xlsx'
    };

    res.json(returnJson);
    res.end();
    console.log('File write done');
});

Solution

  • I believe you need to set the workbook.view object like so:

    workbook.views = [
        {
            x: 0, y: 0, width: 10000, height: 20000,
            firstSheet: 0, activeTab: 0, visibility: 'visible' // Set activeTab to 0
        }
    ];
    

    We're just changing the activeTab property from 1 to 0, this should fix your problem!