This is more a sveltekit question then a office-js question…
I have an office.js addin built in sveltekit that authenticates users to their domain or live.com. This works fine except for one thing. In the manifest.xml i specified 2 buttons, one to open the taskpane and one to run a function from the functionfile. the first one works fine but I can’t seem to get the function to run.
Here’s some codesnippets to show what I’ve tried:
In the manifest I have:
...
<FunctionFile resid="Commands.Url" />
...
<Control xsi:type="Button" id="TaskpaneButton2">
...
<Action xsi:type="ExecuteFunction">
<FunctionName>writeValue</FunctionName>
</Action>
...
<bt:Url id="Commands.Url" DefaultValue="https://localhost:5173/addin" />
...
in the sveltekit route src/routes/addin/+page.svelte i have this:
<script lang="ts">
function writeValue(event:Office.AddinCommands.Event) {
Office.context.document.setSelectedDataAsync('ExecuteFunction works',
(asyncResult) => {
...
});
event.completed();
}
</script>
I see the button appear in the Excel menubar and opening the taskpane button works fine but the writeValue doesn’t seem to exist or run. Also there’s no debugwindow when using ExecuteFunction to see any console.log output so I don’t know what, if any, the errormessages are. Is there a way to connect a console window to it?
When I put the function in a javascript file in /static/addin and create an html that uses and point the Commands.Url to that file, it runs fine but that would separate it completely from the app which I don’t want to do for obvious reasons.
Microsoft docs say the function that an action can run, should be defined in the head of the page that the Commands.Url points to. I expected the function in +page.svelte to be available like that but somehow it can’t find it or it won’t run like that.
So the question is, how does the +page.svelte make the function available for the office.js script to find it and run it? Any pointers to how to set this up would be greatly appreciated!
I believe that in order to make this work you have to update your manifest to use a shared runtime, so that both commands in ribbon and taskpane are able to share code/context?
https://learn.microsoft.com/en-us/office/dev/add-ins/develop/configure-your-add-in-to-use-a-shared-runtime
Add a new “requirements” section to your manifest, after hosts, like so:
<Hosts>
...
</Hosts>
<Requirements>
<Sets DefaultMinVersion="1.1">
<Set Name="SharedRuntime" MinVersion="1.1"/>
</Sets>
</Requirements>
1