Is it Visual Studio 2017
Visual Studio Code with Yo-man generator?
Why can't you have both?
I'm new to JS/Office Add-Ins, so take this with a grain of salt, but I spent way more time then I'd care to admit working on testing my dev environment.
Here is where I ended and some notes:
- I prefer editing code in VSCode
- VS 2017 has best debugging which is imperative
- VS 2017 files are outdated and proj files are "flat"
- Yeoman templates use polyfill, which enables more, but adds complexity.
- Yeoman uses NPM/NodeJS which has its pros/cons. Easy to add extra libraries is a nice feature.
- I hate webpack...
My final Dev Environment was made like this:
- Installed VS 2022 and make an Office Add-In template
- Copied everything? or just Scripts/Content? (I forget) to a VS 2017 template.
I remember one issue being I had to use the 2017 compiler in
packages.config
but I was able to use the newer office.js
.
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="jQuery" version="3.6.0" targetFramework="net472" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Office.js" version="1.1.0.18" targetFramework="net472" />
<package id="OfficeUIFabricCore" version="9.6.0" targetFramework="net472" />
</packages>
In VS you will have to update 2x Schemas on your local dev computer for TaskPaneAppVersionOverridesV1_0.xsd
and OfficeAppBasicTypesV1_0.xsd
. Basically you need to replace/update the files under --> C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Xml\Schemas\1033
with the ones from here --> https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-owemxml/82e93ec5-de22-42a8-86e3-353c8336aa40
I then used yo office
to generate a JS Taskpane and CustomFunction template. I combined the node_modules
folder and the packages.json
and some of the HTML/JS files.
I then deleted all the Home./FunctionFile stuff from the VS template and added the yo
folder to the ExcelWebAddIn1Web
folder. (Making sure the yo
folder exists).
In the manifest for VS2017 I pointed everything to the dist
folder that npm run build
output. <SourceLocation DefaultValue="~remoteAppUrl/yo/dist/taskpane.html"/>
So each time I need to test I have to open a cmd prompt and run npm run build
then hit start in VS. I'd love to automate it so I could
launch/hit stat from the CLI if anyone knows how. (update below)
Finally, on a side note about WebPack. Originally I stayed w/ Yo until I tried adding files to taskpane.html
with relative references. NPM would output a taskpane.html
with hashed files names like ABC123.js
but the file name would be XYZ456.js
. It seemed if I used http
in src
attribute then NPM wouldn't hash the file name, but if I used relative paths, it made a hash w/ the relative path included in the hash output.
Long story short, to overcome this I made a taskpane.npm
which was just a copy of the first taskpane.html
that npm run build
output.
And then in yo_build.cmd
this is what I have (I added sendkeys.bat
to switch back to VS and start solution)
@ECHO OFF
setlocal ENABLEEXTENSIONS
pushd %~dp0
cls
npm run build && xcopy /y /s .\src\taskpane\taskpane.npm .\dist\taskpane.html && call sendkeys.bat "ExcelWebAddIn1 - Microsoft Visual Studio (Administrator)" "{F5}"
This enables me to keep my own taskpane.html
and gives me the added functionality of adding on extra whatever to the end of my npm build. I have a feeling I'll add more to that script at some point.
In the end, I have a Yo Office
w/ TaskPane/Custom Function
running under VS 2017 w/ files from VS 2022 injected. I tend to use VS 2017 for coding more, as it has to be open for debugging, but I have a feeling I'll open VSCode before production to check/prettier etc.
Notes: I was able to get all 3 things working w/ Excel in the same Add-In via this method, taskpane, command-add-in and custom function!
I wasn't able to get this to work w/ TypeScript
templates from yo office
. I'd be tempted to try again, but by the time I got it working after re-doing it X amount of times, I wasn't going to re-start and I do remember trying at least once and it failed.
I also added this to my commands.js
to get the macro to load upon Excel starting to streamline things:
Office.onReady(() => {
// If needed, Office.js is ready to be called
Office.addin.setStartupBehavior(Office.StartupBehavior.load);
//Office.addin.showAsTaskpane();
//Office.addin.hide();
/*
let sheets = context.workbook.worksheets;
sheets[onEventName].add(async (eventData: any) => {
// EXECUTE YOUR EVENT HANDLER HERE
});
Excel.run(async context => {
let sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.onChanged.add(onChange);
await context.sync();
console.log("A handler has been registered for the onChanged event.");
});
*/
Excel.run(async context => {
helloworld();
});
});
and this to helloworld
func to avoid console errors about event
being unable to complete on first run.
try { event.completed() } catch (error) { }