【问题标题】:Excel Web Add-in Custom Function using Visual Studio使用 Visual Studio 的 Excel Web 加载项自定义函数
【发布时间】:2021-03-25 21:18:52
【问题描述】:

我使用 VS 2019 创建了 Excel Web Addin 项目,并尝试将自定义函数合并到该项目中。不幸的是,MSDN 上的所有文档都引用了 Yeoman 生成器,所以我不确定 VS 创建的项目是否不支持自定义功能,或者我遗漏了什么? 我在 HOST 下添加了以下因素

<AllFormFactors>
          <ExtensionPoint xsi:type="CustomFunctions">
            <Script>
              <SourceLocation resid="CustomFunction.Script" />
            </Script>
            <Page>
              <SourceLocation resid="CustomFunction.Page" />
            </Page>
            <Namespace resid="CustomFunction.Namespace" />
          </ExtensionPoint>
        </AllFormFactors>

所有的 URL 都指向我创建的文件

<bt:Url id="CustomFunction.Script" DefaultValue="~remoteAppUrl/CustomFunctions/CustomFunction.js" />
        <bt:Url id="CustomFunction.Page" DefaultValue="~remoteAppUrl/CustomFunctions/CustomFunction.html" />

我的 CustomFunction.js 文件内容如下:

// The initialize function must be run each time a new page is loaded.
(function () {
    Office.initialize = function (reason) {
        // If you need to initialize something you can do so here.
    };

    /**
     * Add two numbers
     * @customfunction
     * @param {number} first First number
     * @param {number} second Second number
     * @returns {number} The sum of the two numbers.
     */
    function add2(first, second) {
        return first + second;
    }

    CustomFunctions.associate("ADD2", add2);

})();
HTML文件看起来像

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Expires" content="0" />
    <title></title>
    <script src="https://appsforoffice.microsoft.com/lib/1.1/hosted/custom-functions-runtime.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>

我可以成功调试项目,当我运行它时,Excel 会打开,但没有加载任何加载项。 谁能指出正确的方向吗?

【问题讨论】:

  • 在没有自定义函数的情况下,插件会加载成功吗?还是仅当加载项中存在客户函数时才会出现此问题?
  • 嗨@Phoebe123,是的,在对自定义函数进行任何更改之前,插件加载成功。真的很奇怪,所有自定义函数的文档都只使用 Yoeman 生成器(不是 VS)。
  • 你明白了吗?我认为你不能在 VS 2019 中创建自定义函数。在清单文件中我有这样的评论: 所以我猜 AllFormFactors 没有位置。

标签: visual-studio-2019 office-js excel-addins custom-functions-excel


【解决方案1】:

您没有包含元数据的 JSON 存档:

<AllFormFactors>
          <ExtensionPoint xsi:type="CustomFunctions">
            <Script>
              <SourceLocation resid="Functions.Script.Url" />
            </Script>
            <Page>
              <SourceLocation resid="Functions.Page.Url"/>
            </Page>
            <Metadata>
              <SourceLocation resid="Functions.Metadata.Url" />
            </Metadata>
            <Namespace resid="Functions.Namespace" />
          </ExtensionPoint>
        </AllFormFactors> 

您必须手动配置元数据 JSON:

Manually create JSON

您可以创建一个包含 HTML、JS 和 JSON 档案的新文件夹。然后,将它们指向清单(页面、脚本和元数据)。

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多