Search code examples
exceloffice-jsexcel-addins

hide rows in excel workbook using office.js


We are building an excel addin using office.js. We want to hide a range of rows. I checked the Excel Javascript API Documentation. I did not find any solution for that.

Could someone please help on this.


Solution

  • Using Office.js, you can programmatically hide and unhide rows by updating the rowHidden property on the range object. The following example shows how to hide rows 2-5 in Sheet1.

    Excel.run(function (context) {
    
        // Hide rows 2-5 in 'Sheet1'
        var range = context.workbook.worksheets.getItem("Sheet1").getRange("2:5");
        range.rowHidden = true;
    
        return context.sync()
            .then(function() {
                console.log("Rows 2-5 have been hidden.");
            });
    }).catch(function (error) {
            OfficeHelpers.UI.notify(error);
            OfficeHelpers.Utilities.log(error);
    });
    

    To unhide rows in a range, set the rowHidden property to false. You can find documentation of the rowHidden property (and the columnHidden property, for hiding/unhiding columns) here in the Excel API Reference docs: https://dev.office.com/reference/add-ins/excel/range.