-1

In the Office-JS library, it is possible to get all the worksheets in a workbook. It is also possible to get one specific worksheet. Is it possible to get the selected worksheets in a workbook?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Sam Diaz
  • 51
  • 4

1 Answers1

0

The following code sample gets the collection of worksheets, loads the name property of each worksheet, and writes a message to the console:

Excel.run(function (context) {
    var sheets = context.workbook.worksheets;
    sheets.load("items/name");

    return context.sync()
        .then(function () {
            if (sheets.items.length > 1) {
                console.log(`There are ${sheets.items.length} worksheets in the workbook:`);
            } else {
                console.log(`There is one worksheet in the workbook:`);
            }
            sheets.items.forEach(function (sheet) {
              console.log(sheet.name);
            });
        });
}).catch(errorHandlerFunction);

The following code sample gets the active worksheet, loads its name property, and writes a message to the console.

Excel.run(function (context) {
    var sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    return context.sync()
        .then(function () {
            console.log(`The active worksheet is "${sheet.name}"`);
        });
}).catch(errorHandlerFunction);

See Work with worksheets using the Excel JavaScript API for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45