Office.onReady

Office.onReady() is an asynchronous method that can return a Promise object while the Office.js library is loading.
When the Office.js library is loaded, the Promise is resolved.
The Promise resolves immediately if the Office.js library is already loaded when Office.onReady() is called.


With No Arguments

Using a CallBack Function
One way to call Office.onReady() is to pass it a callback function.

Office.onReady(function () { 
    // Office is ready
});

// with arrow function
Office.onReady(() => {
    if (!Office.context.requirements.isSetSupported('ExcelApi', '1.7')) {
        console.log("Sorry, this add-in only works with newer versions of Excel.");
    }
});

Using a Promise
Instead of using a callback you could chain a then() method to the Office.onReady().
If the library is already loaded when Office.onReady() is called, then the Promise resolves immediately.

Office.onReady() 
    .then(function () {
        if (!Office.context.requirements.isSetSupported('ExcelApi', '1.7')) {
            console.log("Sorry, this add-in only works with newer versions of Excel.");
        }
    });

With Info Argument

Using a CallBack Function
One way to call Office.onReady() is to pass it a callback function.

Office.onReady(function (info) { 
    console.log("Office.js is now ready in " + info.host + " on " + info.platform});
});

// with arrow function
Office.onReady((info) => {
    console.log("Office.js is now ready in " + info.host + " on " + info.platform});
});

Using a Promise
Instead of using a callback you could chain a then() method to the Office.onReady()
The object returned from the Promise specifies the Office host application and the platform.

Office.onReady() 
    .then((info: { host: Office.HostType; platform: Office.PlatformType }) => {
        console.log("Office.js is now ready in " + info.host + " on " + info.platform});
    });

With JQuery

If you are using additional JavaScript frameworks that include their own initialization handler or tests, these should be usually be placed within the response to Office.onReady().
For example, JQuery's $(document).ready() function would be referenced as follows:

Office.onReady(function (info) { 
    $(document).ready(function () {
       console.log("Office.js is now ready in " + info.host + " on " + info.platform});
    });
});

Exceptions

There are exceptions to this practice.
For example, suppose you want to open your add-in in a browser (instead of sideload it in an Office host) in order to debug your UI with browser tools. Since Office.js won't load in the browser, onReady won't run and the $(document).ready won't run if it's called inside the Office onReady.
Another exception: you want a progress indicator to appear in the task pane while the add-in is loading. In this scenario, your code should call the jQuery ready and use it's callback to render the progress indicator. Then the Office onReady's callback can replace the progress indicator with the final UI.


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext