EDIT: Added simplified scenario and steps to reproduce at the end of the question. Please note that the objective is to include external script on Excel-Desktop, and not to get the simplified scenario working by some other means.
We all know and love the stock price example for custom functions where an up-to-date stock price is requested from the source using 'fetch':
If there was an external API to get stock prices, could that also be used? Like so:
Stock Price Source
| <----------- API knows how to get the stock price
External Stock Price API Javascript Library
| <----------- javascript function calls here
customfunctions.js
|
Excel
Or in other words, can the customfunctions JavaScript project include external script like
<script type="text/javascript" src="https://example.com/stock-price-api.js />
I have tried to include such a reference in the custom function project's index html file, included the script URL in the manifest under AllFormFactors/ExtensionPoint for the 'xsi:type=CustomFunctions', added a script reference under 'Resources/bt:Urls', and various combinations thereof, but the script does not seem to be 'known' to the custom functions themselves (the index html file, when loaded into the browser, knows about the script, so I know that the reference is correct). What I have tried was all guess work because I have not found any documentation regarding the use of external APIs. I'm trying to get this to work for Excel on Windows desktop (64-bit).
Can it be done?
EDIT/UPDATE: Just confirmed that it works fine for Excel Online by simply including the external script in the sample project's 'index.html' file. Is this intended to work for Excel Desktop?
To reproduce the issue:
Follow the tutorial to create the basic custom functions project (using the typescript option) and ensure that they work (https://learn.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows)
Create a javascript file 'globalapi.js' and place it into the 'dist/win32/ship' folder (the same folder that contains the custom function javascript). Simply declare a global variable in the 'globalapi.js' file like so:
var someGlobalString = 'This is a global string';
Include the 'globalapi.js' script in the 'index.html' file; all the scripts included are then:
<script src="https://officedev.github.io/custom-functions/lib/custom-functions-runtime.js" type="text/javascript"></script> <script src="dist/win32/ship/globalapi.js" type="text/javascript"></script> <script src="dist/win32/ship/index.win32.bundle" type="text/javascript"></script>
Modify the 'add' function in 'customfunctions.ts' file so that it returns something different if 'someGlobalString' is defined:
declare var someGlobalString: any; function add(first: number, second: number): number { if (typeof someGlobalString !== 'undefined') { return 99999; } return first + second; }
Now enter the custom function '=CONTOSO.ADD(1,2)' into a cell in Excel. For Excel Desktop (started with 'npm run sideload') the result displayed in the cell is '3', for Excel Online (after loading the manifest) the result is '99999'.
How can the difference be explained?