In OfficeJS, can you retrieve the range of a binding object?

与世无争的帅哥 提交于 2019-12-22 08:39:34

问题


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

回答1:


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

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

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?".

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.

UPDATE:

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

You can try it out with Script Lab, same instructions as above, at https://gist.github.com/Zlatkovsky/24f2297cecea181edcc165c6c0df6da0

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.



来源:https://stackoverflow.com/questions/44421986/in-officejs-can-you-retrieve-the-range-of-a-binding-object

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