在OfficeJS中,可以检索绑定对象的范围吗? [英] In OfficeJS, can you retrieve the range of a binding object?

查看:75
本文介绍了在OfficeJS中,可以检索绑定对象的范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对Word Online,但是Excel/PPT的任何指针也将有所帮助.

Targeted towards Word Online, but any pointers for Excel/PPT would be helpful as well.

从本质上讲,可以将绑定对象中的文本视为一个范围吗?因此,能够选择所有内容并将插入符号移动到开始/结束.

Essentially, is it possible to treat the text within a binding object as a range? Thus, being able to select it all as well as move the caret to the beginning/end.

我正在设想该代码具有以下作用:

I was envisioning the code to have something to the effect of:

Office.select("myBindingID", function error(){}).getAsRange().select("End");

推荐答案

特定于主机的Office 2016+ API浪潮与通用"(2013)API有所不同.

There is a difference between the host-specific Office 2016+ wave of APIs and the "common" (2013) APIs.

在Excel中,绑定(以及公共API集中的所有内容)公开给新的OM,并与OM的其他方面(例如Ranges)连接.因此,在那里,您绝对可以做到:

In Excel, bindings (and everything from the common API set) is exposed to the new OM, and connect with other aspects of the OM (e.g., Ranges). So there, you could absolutely do it:

    await Excel.run(async (context) => {
        let binding = context.workbook.bindings.getItem("TestBinding");
        let range = binding.getRange();
        range.load("address");
        range.select();

        await context.sync();

        OfficeHelpers.UI.notify("Binding range address is " + range.address);
    });

但是,在特定于Word的API中,Word似乎不提供绑定支持.如果您想双重确定,则可能要问一个单独的Stackoverflow问题,例如"WordApi(Office 2016+)是否支持绑定?".

It appears that Word does not offer binding support, however, in the Word-specific APIs. If you want to be doubly-sure, you may want to ask a separate Stackoverflow question, something like "Does WordApi (Office 2016+) support Bindings?".

对于Excel,您可以通过最近启动的脚本实验室工具(实际上是 https://gist.github.com/Zlatkovsky/7701ceddae360ad3883ca867f3831a6f .请参阅有关将代码段导入到脚本实验室的更多信息.

For Excel, you can try an expanded version of the above snippet live in literally five clicks via the recently-launched Script Lab tool (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/Zlatkovsky/7701ceddae360ad3883ca867f3831a6f. See more info about importing snippets to Script Lab.

更新:

关于@codex有关是否可以将Office 2013方法(addFromPromptAsync)与新的Office 2016 API组合的问题:是的.您可以将调用嵌套到2013回调中,尽管我个人更喜欢将其包装在Promise中,如下所示(请参见代码的上半部分),然后对新的API使用Excel.run(与我所用的相同)之前使用过):

Regarding @codex's question about whether it's possible to combine an Office 2013 method (addFromPromptAsync) with the new Office 2016 wave of APIs: yes it is. You can nest the call into the 2013 callback, though I personally prefer to wrap it in a Promise instead, as follows (see top half of the code), followed by then using an Excel.run for the new APIs (identical to what I was using before):

    await new Promise((resolve, reject) => {
        Office.context.document.bindings.addFromPromptAsync(
            Office.BindingType.Matrix,
            { id: "TestBinding" },
            (result) => {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                    resolve();
                } else {
                    reject();
                }
            }
        )
    })

    await Excel.run(async (context) => {
        let binding = context.workbook.bindings.getItem("TestBinding");
        let range = binding.getRange();
        range.load("address");
        range.select();

        await context.sync();

        OfficeHelpers.UI.notify("Binding range address is " + range.address);
    });

您可以在 https://gist上,按照脚本实验室的相同说明进行尝试. github.com/Zlatkovsky/24f2297cecea181edcc165c6c0df6da0

PS:如果您不熟悉使用Promises包装回调,那么在"Building Office Add- ins using Office.js"( https://leanpub.com/buildingofficeaddins ).免责声明,我是上述书籍的作者;但我确实认为,无论是从JS/TS/Promise概念入门到本书的实质",还是从构成Office 2016浪潮的核心概念上,读者都会从中发现很多价值. API.

PS: If you're new to wrapping callbacks with Promises, there is a chapter devoted to a JS/TS and Promises Primer -- including a section specifically about creating a new Promise -- in the book "Building Office Add-ins using Office.js" (https://leanpub.com/buildingofficeaddins). Disclaimer, I am the author of said book; but I do think that readers will find a lot of value in it, both for getting started with JS/TS/Promise concepts, and for the "meat" of the book -- on the core concepts that make up the Office 2016 wave of APIs.

这篇关于在OfficeJS中,可以检索绑定对象的范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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