In OfficeJS, can you get the range of an anchor object?

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

Essentially, is it possible to treat the text inside the anchor object as a range? Thus, the ability to select everything and also move the cursor to the beginning / end.

I foresaw that the code has something like:

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

      

+3


source to share


1 answer


There is a distinction between the host specific API 2016+ and the (common) (2013) API.

In Excel, bindings (and all of the common set of APIs) are exposed to the new OM and are connected to other aspects of the OM (such as ranges). So, you can do it absolutely free:

    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 looks like Word doesn't support binding, however, in the Word APIs. If you want to be doubly sure, you can ask a separate question on Stackoverflow, for example, "Does WordApi (Office 2016+) support Bindings?"

For Excel, you can try an extended version of the above snippet live with literally five clicks using the newly launched Script Lab tool ( https://aka.ms/getscriptlab ). Just install the Script Lab add-on (free), then select Import from the navigation menu and use the following GIST URL: https://gist.github.com/Zlatkovsky/7701ceddae360ad3883ca867f3831a6f . See more information on importing snippets into Script Lab .



UPDATE:

Regarding @codex's question about the possibility of combining the Office 2013 ( addFromPromptAsync

) method with the new version of the Office 2016 APIs: yes it does. You can nest the call in a 2013 callback, although I personally prefer to wrap it in a Promise, then like below (see the top of the code) and then using Excel.run

for newer APIs (identical to what I've used 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 with Script Lab, same instructions as above at https://gist.github.com/Zlatkovsky/24f2297cecea181edcc165c6c0df6da0

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

+1


source







All Articles