Snippets - Worksheets

link - docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-worksheets


Worksheet - Exists

Excel.run(function (context) { 
   var _worksheet = context.workbook.worksheets.getItemOrNullObject("Sheet 1");
   if (_worksheet.IsNullObject) {
      // does not exist, needs to be created
   }
});

Worksheet - From Range

Excel.run(function (context) { 
    var selectedRangeWorksheet = context.workbook.getSelectedRange().worksheet.load("name");
    return context.sync()
        .then(function () { console.log(selectedRangeWorksheet.name); })
        .catch(function (error) { console.log(error); });
});

Worksheet - create worksheet

Excel.run(function (context) { 
                context.workbook.worksheets.add("Sheet" + Math.floor(Math.random()*100000).toString());
                return context.sync();
}).catch(function (error) {
   console.log(error);
});

Worksheet - get worksheets in workbook

Excel.run(function (context) { 
                var worksheets = context.workbook.worksheets.load("name");
                return context.sync().then(function () {
                                for (var i = 0; i < worksheets.items.length; i++) {
                                                console.log(worksheets.items[i].name);
                                }
                                console.log("done");
                });
}).catch(function (error) {
   console.log(error);
});

Worksheet - get active worksheet

Excel.run(function (context) { 
                var activeWorksheet = context.workbook.worksheets.getActiveWorksheet().load("name");
                return context.sync().then(function () {
                                console.log(activeWorksheet.name);
                });
}).catch(function (error) {
   console.log(error);
});

Worksheet - delete active worksheet

Excel.run(function (context) { 
                context.workbook.worksheets.getActiveWorksheet().delete();
                return context.sync();
}).catch(function (error) {
   console.log(error);
});

Worksheet - Get a List of all Worksheets

var sheetNames = []; 
for (var sheetindex in context.workbook.worksheets.items) {
   var sheet = context.workbook.worksheets.items[sheetindex];
   sheetNames.push(sheet.name);
}


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext