简体   繁体   English

如何包含用于Excel自定义函数的外部JavaScript?

[英]How do you include external JavaScript for use in Excel custom functions?

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. 请注意,这样做的目的是在Excel-Desktop上包括外部脚本,而不是通过其他某种方式来使简化方案工作。


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': 我们都知道并喜欢自定义功能的股票价格示例,该示例使用'fetch'从来源请求最新的股票价格:

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 https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows#create-a-custom-function-that-请求数据,来自该网络

If there was an external API to get stock prices, could that also be used? 如果有一个外部API可以获取股票价格,那还可以使用吗? 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 或者换句话说,customfunctions JavaScript项目是否可以包含外部脚本,例如

<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). 我试图在自定义函数项目的索引html文件中包含这样的引用,在清单中的脚本URL包含在“ xsi:type = CustomFunctions”的AllFormFactors / ExtensionPoint下,在“ Resources / bt:Urls”下添加了脚本引用。 ,以及它们的各种组合,但是脚本似乎不是自定义函数本身的“已知”(将html索引文件加载到浏览器中后,它就会知道该脚本,因此我知道引用是正确的)。 What I have tried was all guess work because I have not found any documentation regarding the use of external APIs. 我尝试的所有工作都是猜测,因为我没有找到任何有关使用外部API的文档。 I'm trying to get this to work for Excel on Windows desktop (64-bit). 我正在尝试使其在Windows桌面(64位)上适用于Excel。

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. 编辑/更新:只需将外部脚本包含在示例项目的“ index.html”文件中,即可确认它对于Excel Online可以正常工作。 Is this intended to work for Excel Desktop? 是否打算用于Excel Desktop?


To reproduce the issue: 重现此问题:

  1. Follow the tutorial to create the basic custom functions project (using the typescript option) and ensure that they work ( https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial-create-custom-functions?tabs=excel-windows ) 按照教程创建基本的自定义函数项目(使用typescript选项),并确保它们正常工作( https://docs.microsoft.com/zh-cn/office/dev/add/ins/tutorials/excel-tutorial- create-custom-functions?tabs = excel-windows

  2. Create a javascript file 'globalapi.js' and place it into the 'dist/win32/ship' folder (the same folder that contains the custom function javascript). 创建一个javascript文件“ globalapi.js”,并将其放入“ dist / win32 / ship”文件夹(包含自定义函数javascript的同一文件夹)。 Simply declare a global variable in the 'globalapi.js' file like so: 只需在“ globalapi.js”文件中声明一个全局变量,如下所示:

     var someGlobalString = 'This is a global string'; 
  3. Include the 'globalapi.js' script in the 'index.html' file; 在“ index.html”文件中包含“ globalapi.js”脚本; 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> 
  4. Modify the 'add' function in 'customfunctions.ts' file so that it returns something different if 'someGlobalString' is defined: 修改“ customfunctions.ts”文件中的“ add”函数,以便在定义“ someGlobalString”的情况下返回不同的内容:

     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. 现在,将自定义函数'= CONTOSO.ADD(1,2)'输入到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'. 对于Excel Desktop(以'npm run sideload'开头),单元格中显示的结果为'3';对于Excel Online(加载清单后),结果为'99999'。

How can the difference be explained? 如何解释差异?

Sorry, there's a bug right now on the latest windows build with web requests on Windows build 16.0.11406.20002. 抱歉,Windows build 16.0.11406.20002上带有Web请求的最新Windows build上现在有一个错误。

https://github.com/OfficeDev/Excel-Custom-Functions/issues/115 https://github.com/OfficeDev/Excel-Custom-Functions/issues/115

We'll update that issue as soon as the next build rolls out to the Insiders channel. 下一个版本发布到Insiders频道后,我们将立即更新该问题。

thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM