none
Get All Sheet names using JavaScript API for Office RRS feed

  • Question

  • Hi,

    We are building the app, which should be able to grab the content of selected cells, as well as selected range, when selection changes on any of the sheets in the Excel document.

    We do not know the sheet names in advance.

    At this moment, as it seems, the only way to do it is, to create one binding per sheet, and then use the  Office.EventType.BindingSelectionChanged event.

    The problem is that we need the sheet names, in order to create bindings. Is there a way to grab all the sheet names using JavaScript API for Office?

    Regards,

    Vujadin

    Monday, April 4, 2016 12:59 AM

Answers

  • Hi Vujadin,

    What's version of Excel are you developing? As far as I know, the latest version JavaScript API for Office which support the Excel 2016 has support this feature.

    Here is an sample that retrieve all the worksheet for your reference:

       Excel.run(function (ctx) {
                var worksheets = ctx.workbook.worksheets;
                worksheets.load('items');
                return ctx.sync().then(function () {
                    for (var i = 0; i < worksheets.items.length; i++) {
    
                        var ws=worksheets.items[i];
                        ws.load("name");
    
                    }
                    ctx.sync().then(function () {
    
                        for (var i = 0; i < worksheets.items.length; i++) {
                            console.log(worksheets.items[i].name);
                        }
    
                    })
                });
            }).catch(function (error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 6, 2016 2:47 AM
    Moderator
  • Hi Vujadin,

    Yes, you are correct. It works only for Excel 2016 at present. If you want it also support other version of Office, you can try to submit the feedback from link below:
    https://officespdev.uservoice.com/

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 7, 2016 6:15 AM
    Moderator

All replies

  • Hi Vujadin,

    What's version of Excel are you developing? As far as I know, the latest version JavaScript API for Office which support the Excel 2016 has support this feature.

    Here is an sample that retrieve all the worksheet for your reference:

       Excel.run(function (ctx) {
                var worksheets = ctx.workbook.worksheets;
                worksheets.load('items');
                return ctx.sync().then(function () {
                    for (var i = 0; i < worksheets.items.length; i++) {
    
                        var ws=worksheets.items[i];
                        ws.load("name");
    
                    }
                    ctx.sync().then(function () {
    
                        for (var i = 0; i < worksheets.items.length; i++) {
                            console.log(worksheets.items[i].name);
                        }
    
                    })
                });
            }).catch(function (error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 6, 2016 2:47 AM
    Moderator
  • Hi,

    Thank you for your reply. It seems this exactly what we need. However it seems that it would require a change in our programming model.

    We are aiming to support older versions of Excel as well, currently we are developing on Excel 2013.

    We are using Javascript API for Office, Version: 15.0.4420.1017, as it can be found here:

    https://appsforoffice.microsoft.com/lib/1.0/hosted/office.js

    If I understand correctly this will work only on Excel 2016?

    Regards,

    Vujadin


    Wednesday, April 6, 2016 1:09 PM
  • Hi Vujadin,

    Yes, you are correct. It works only for Excel 2016 at present. If you want it also support other version of Office, you can try to submit the feedback from link below:
    https://officespdev.uservoice.com/

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 7, 2016 6:15 AM
    Moderator