title: How do you include external JavaScript for use in Excel custom functions?
body: <p>EDIT: Added simplified scenario and steps to reproduce at the end of the question. Please note that the objective is to include <em>external</em> script on Excel-Desktop, and not to get the simplified scenario working by some other means.</p>
  
  <hr>
  
  <p>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':</p>
  
  <p><a href="https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows#create-a-custom-function-that-requests-data-from-the-web" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows#create-a-custom-function-that-requests-data-from-the-web</a></p>
  
  <p>If there was an external API to get stock prices, could that also be used? Like so:</p>
  
  <pre><code>                   Stock Price Source
                             |  &lt;----------- API knows how to get the stock price
             External Stock Price API Javascript Library
                             |  &lt;----------- javascript function calls here
                      customfunctions.js
                             |
                           Excel
  </code></pre>
  
  <p>Or in other words, can the customfunctions JavaScript project include external script like</p>
  
  <pre><code>&lt;script type="text/javascript" src="https://example.com/stock-price-api.js /&gt;
  </code></pre>
  
  <p>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). </p>
  
  <p>Can it be done?</p>
  
  <hr>
  
  <p>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?</p>
  
  <hr>
  
  <p>To reproduce the issue:</p>
  
  <ol>
  <li><p>Follow the tutorial to create the basic custom functions project (using the typescript option) and ensure that they work (<a href="https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows</a>)</p></li>
  <li><p>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:</p>
  
  <pre><code>var someGlobalString = 'This is a global string';
  </code></pre></li>
  <li><p>Include the 'globalapi.js' script in the 'index.html' file; all the scripts included are then:</p>
  
  <pre><code>&lt;script src="https://officedev.github.io/custom-functions/lib/custom-functions-runtime.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="dist/win32/ship/globalapi.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="dist/win32/ship/index.win32.bundle" type="text/javascript"&gt;&lt;/script&gt;
  </code></pre></li>
  <li><p>Modify the 'add' function in 'customfunctions.ts' file so that it returns something different  if 'someGlobalString' is defined:</p>
  
  <pre><code>declare var someGlobalString: any;
  
  function add(first: number, second: number): number {
    if (typeof someGlobalString !== 'undefined') {
       return 99999;
    }
    return first + second;
  }
  </code></pre></li>
  </ol>
  
  <p>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'.</p>
  
  <p>How can the difference be explained?</p>