如何在Office JS API中识别Excel工作表更改事件? [英] How to identify the Excel Sheet changed event in office js api?

查看:67
本文介绍了如何在Office JS API中识别Excel工作表更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用angular写的Excel office js加载项.根据用户所在的工作表,它具有不同的功能.当用户切换excel工作表时,应用程序如何知道它,因此它可以更改UI以匹配工作表的功能?

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?

推荐答案

更新2017年5月18日:借助ExcelApi 1.2+,您可以使用新的语法

新语法如下:context.workbook.onSelectionChanged.add(yourHandler);

您可以使用脚本实验室查找完整的示例,这是一个免费的Office加载项试用版-在摘要中.其中一个示例具有选择更改"事件代码段:

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:

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

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

原始答案:

正如迈克尔·桑德斯(Michael Saunders)所说,您可以使用选择更改事件.请参见下面的代码.请注意,在这种情况下,我将"Office 2013"​​语法与Office 2016的较新的主机特定("Excel."命名空间)语法混合在一起.在几个月后的"ExcelApi 1.3"版本中,我们实际上有一种方法可以让您完全使用新语法来完成此操作,但这仅在预览CDN上有效,并且可能无法在您的计算机上使用,具体取决于您的Ofice 2016版本是多少.同时,下面的代码将在包括RTM在内的任何2016年安装中都可以使用.

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));
        }
    });    
});

这篇关于如何在Office JS API中识别Excel工作表更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆