How to identify the Excel Sheet changed event in office js api?

只谈情不闲聊 提交于 2019-12-02 09:01:35

问题


We have an Excel office js add-in written on angular. It has different functionalities based on the sheet the user is in. When users switch the sheets of excel, how can the application know it, so it can change the UI to match the sheet's functionalities?


回答1:


There isn't a sheet-changed event. However, one workaround would be to subscribe to DocumentSelectionChanged events, then check the user's active sheet each time to see if it changed.




回答2:


UPDATE May 18 2017: With ExcelApi 1.2+, you can use a new syntax

The new syntax is as follows: context.workbook.onSelectionChanged.add(yourHandler);

You can find a full sample by using Script Lab, a free add-in for trying out Office Add-in snippets. One of the samples has a "Selection Changed" event snippet:

==================

Original answer:

As Michael Saunders said, you can use the selection change event. See the code below. Note that in this case I'm mixing the "Office 2013" syntax with the newer host-specific ("Excel." namespace) syntax of Office 2016. In the "ExcelApi 1.3" release that's coming in a couple months, we actually have a way for you to do this entirely using the new syntax, but that is currently only on the preview CDN, and may not work on your machine, depending on how recent your version of Ofice 2016 is. The code below, meanwhile, is going to work on any 2016 installation, RTM included.

Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, function() {
    Excel.run(function(context) {
        var sheet = context.workbook.worksheets.getActiveWorksheet();
        sheet.load("name")
        return context.sync().then(function() {
            console.log('You are now on sheet "' + sheet.name + '"');
        })
    }).catch(function(error) {
        console.log(error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });    
});



回答3:


You can add handler for context.workbook.worksheets.onActivated

Excel.run((context) => {
  context.workbook.worksheets.onActivated.add(({ worksheetId }) => {
    console.log('Selected worksheet', worksheetId)
  })

  return context.sync()
    .then(function () {
        console.log("Event handler successfully registered");
    });
}).catch(errorHandlerFunction);

More info here



来源:https://stackoverflow.com/questions/39756720/how-to-identify-the-excel-sheet-changed-event-in-office-js-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!