1

We are attempting to determine the environment of execution dynamically within the Custom Functions runtime in order to set a group of environment variables. We have multiple environments where our custom functions execute:

  • local
  • staging
  • qa
  • production

We would like to avoid generating a separate bundle per environment. Instead we would like to generate a single bundle and determine which environment we are executing in at runtime. This would allow us to determine the appropriate URL for the backend API server that supports our functions for instance.

For our taskpane add-in, we have simply used the value of window.location.hostname to determine the current environment and select the approprate API server URL. This also seems to work as expected while executing in the Custom Functions runtime within Excel on the Mac. Unfortunately, when executing within Excel on Windows, the window object is undefined in the Custom Functions runtime.

  1. Is it possible to get the hostname that would generally be accessible via window.location.hostname within the Custom Functions runtime?
  2. If not, we maintain a manifest per environment (e.g. manifest-local.xml, manifest-staging.xml, etc). Is it possible to retrieve values from the application manifest within my custom function logic? For example, if I have the Resources section below in my manifest, could I retrieve the DefaultValue of the Taskpane.Url in my custom function logic?
<Resources>
  <bt:Urls>
    <bt:Url id="Taskpane.Url" DefaultValue="https://localhost:3000" />
    ... ...
  </bt:Urls>
</Resources>
  1. If not, is there another way you would recommend to pass a value that would indicate the current environment to a custom function?

Thanks in advance.

jorshali
  • 11
  • 2

2 Answers2

0

You shouldn't pivot on the platform (win32, mac, online, ...) or host (Excel, Word, ...). You should only pivot on the fact whether an API version is supported, i.e. you should only use this API to pivot your logic: https://learn.microsoft.com/en-us/javascript/api/office-runtime/officeruntime.apiinformation?view=common-js.

If you find a discrepancy between the same versions of Mac and Win32 implementations, please report it through Send a Smile/Frown on any Office product (Excel, Word, ...).

Regarding your observation of the window object - on Win32, Custom Functions are executed in a lighterweight, ReactNative, runtime, which (I am not surprised) doesn't have a window object. On Mac, Custom Functions are executed in a browser control, where you naturally find all browser capabilities.

There is a Preview feature in Excel that, among other benefits, allows you to execute Custom Functions in a browser control. See this article: https://learn.microsoft.com/en-us/office/dev/add-ins/excel/configure-your-add-in-to-use-a-shared-runtime

  • 1
    Just to clarify, we have multiple environments (local, staging, QA, and production). What I am looking for is a way to determine which environment we are executing within. Is there a way to dynamically pass something to a custom function per environment, that would indicate which environment we are currently executing within. That is what we are using the window.location.hostname for, to determine the environment of execution so we can determine the URL for the backend API we should be invoking for instance. Does that help to clarify what we are looking for? – jorshali May 07 '20 at 19:36
  • I have also added some clarification to the original question. If you need further information, please let me know. – jorshali May 07 '20 at 19:45
  • If this all for your own in-house testing purposes, use different manifests. – Zlatko Michailov - MSFT May 07 '20 at 20:42
  • 1
    Yes, as mentioned in the post we are using different manifests. Is it possible to retrieve values from the manifest in a custom function? This would allow us to determine which environment we are in. For example, is it possible through the Office JS API to retrieve the value of Resources > Urls > Taskpane.Url. – jorshali May 07 '20 at 21:33
  • I am not aware of any way to query properties of the manifest. It doesn't make sense - if the referenced resource is part of the add-in, it should know everything about the add-in. Just make each manifest reference a different resource. If it is too difficult to generate different JavaScript files, consider using Shared Runtime, which I referenced in my answer. It uses HTML pages that can include multiple different JavaScript files. – Zlatko Michailov - MSFT May 07 '20 at 23:10
  • @jorshali Did you find a solution for this? We're trying to do the exact same thing you described here in the comments. – Lee Nov 24 '21 at 15:54
  • 1
    @Lee I ended up adding an env.json file specific to each environment into the deployment's static assets. This config file includes the hostname of the environment. I retrieve that file when initializing the custom function through something simple like: Axios.get('env.json'). Seems like it would be much easier to simply retrieve properties from the manifest, but this workaround has met my needs. – jorshali Nov 29 '21 at 14:25
0

It looks like Office.context.mailbox.initialData.extensionId returns the GUID from the manifest file. If you have a different manifest/GUID per environment, that should give you the environment-awareness you're looking for.

NB: I don't see the initialData property documented anywhere, so proceed with caution.

Lee
  • 922
  • 2
  • 11
  • 19