Filip M.
Filip M.

Reputation: 89

How to call code at add-in load in Excel by js

I'm creating my first Excel Add-in using the office.js. At this moment I'm trying to figure out how to call some initial code when using Add-in Commands.

When I don't Add-in Commands, the home page is loaded when the add-in is loaded, and I can do some initialization like defining named items, bindings and so on. But in the case the Add-In commands, I'm not able to find any way how to do this.

Is it true that every single button click loads the specified "functions.html", calls Office.initialize, then calls the method specified in the manifest and then everything is unloaded again? Is there any persistence available?

We'd also need to let the user login via ADAL and the Add-in should only work if that login was successful.

So my questions in short:

When using Add-in Commands

  1. How can I call any functionality before the user click the first button/menu item or so?

  2. How can I keep some js variables persistent over the add-in session?

Upvotes: 1

Views: 820

Answers (2)

Alex
Alex

Reputation: 59

Here is the official documentation to your 2 Question.

To do that, you can:

  • Use members of the JavaScript API for Office that store data as name/value pairs in a property bag stored in a location that depends on add-in type.
  • Use techniques provided by the underlying browser control: browser cookies, or HTML5 web storage (localStorage or sessionStorage).

Upvotes: 1

Marc LaFleur
Marc LaFleur

Reputation: 33094

You've got a couple of questions here. Let's break them down:

Add-in Life Cycle

Add-ins are not initialized/loaded until the user launches/adds them to the document. Prior to this the only items surfaced are the commands defined in your manifest file. Note that things can operate bit differently depending on your configuration (more on this below).

The life-cycle remains the same regardless of how your add-in is launched (function, showing a task pane, etc.). Office opens the URL specified in your manifest and wires up communication between the add-in and the host application. Once this is complete it executes the function you defined for Office.initialize.

While there is certainly some overhead in this, it is pretty unavoidable. We need to wire up communication between two heavily sandboxed applications before the API can operate across the boundary. Your page (or function) needs to wait for this to happen before it can begin taking actions (i.e. Office.initialize). We also need to ensure the page that is loaded is responsive which is why we call Office.initialize with a 5 second timeout.

Even with the setup overhead, the process is exceptionally fast. Generally the bottleneck is the web app loading more far resources than necessary. This is one of the reasons for using a separate functions.html, it allows you to toss out everything but the bare minimum (a reference to office.js and your functions.js).

Add-in Commands & Auto-Loading

The first being that using Add-in Commands affects how add-ins are loaded when a document is opened. Without an Add-in Command defined, Excel will automatically re-load any add-ins that were previously opened when that document was saved.

This auto-load process no longer happens if you have an Add-in Command defined. Excel will load your Add-in Command but it will not automatically launch the Add-in itself.

This can be controlled using the new Office.AutoShowTaskpaneWithDocument feature. There is a walkthrough available here: Automatically open a task pane with a document. This feature isn't widely available across Office editions yet so it effectively still in Preview. That said, you can certainly use the functionality, it will simply be ignored until your users receive a version that supports it.

Pre-Authentication

Authentication needs to be handled entirely within your add-in. There unfortunately isn't a way to pre-authenticate a user before loading your add-in. As far as Office is concerned, if they've loaded your manifest then you're Add-in Commands will added to the ribbon. You'll need to handle authentication within the add-in and using displayDialogAsync to kick off an OAUTH workflow whenever you need the user to provide credentials.

Upvotes: 2

Related Questions