1

I want to list all worksheets of the workbook in a task pane. This is an office-js add-in. Ultimately I'm trying to create a table of contents, which lists the worksheets (might add range names later), and allows users to click on the sheet and be taken there. I'm great with VBA but don't know how to go about this with an Office Add-In. After several days of web searching I'm hoping to find help here. I realize this is a fairly broad question but any assistance would be greatly appreciated.

Currently I'm using yeoman generator to create my project, which is using React framework with Typescript (I'm new with both but am a quick study), and editing with VSCode.

Zack Barresse
  • 239
  • 1
  • 11

2 Answers2

1

Basically, I think what you want to do is get a reference to the Workbook.worksheets property. Load the name property and call context.sync. After the sync, iterate through the worksheets.items property and add the name property of each as a <li> element in a parent <ul> parent in the task pane page. Something like the following, but replace the console.log with code that adds the <ul> and <li>s.

Excel.run(function (context) { 
    var worksheets = context.workbook.worksheets;
    worksheets.load('name');
    return context.sync()
    .then(function() {
        for (var i = 0; i < worksheets.items.length; i++)
        {
            console.log(worksheets.items[i].name);
        }
    });
})
Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
  • Thanks very much for the reply. It's the part of adding them as list items where I'm having an issue. I don't know React or HTML very well. – Zack Barresse Jun 05 '19 at 21:58
  • Then your question isn't really about `office-js`. Try asking a new question and tagging it html an javascript. Don't even mention that it's an Office add-in, because that's not really relevant to what you want to know. – Rick Kirkham Jun 06 '19 at 03:16
0

Zack, take a look at this SO question for how to add items to a HTML list element: How to add <li> in an existing <ul>? using JQuery. You can try this in Script Lab (http://aka.ms/scriptlab).