【问题标题】:Excel Custom Function is not shownExcel 自定义函数未显示
【发布时间】:2022-02-22 00:11:17
【问题描述】:

我正在 Office 加载项中创建 Excel 自定义函数。我的出发点是 Visual Studio 2017 脚手架。

在我的清单中有

<FunctionFile resid="Contoso.DesktopFunctionFile.Url" />

在哪里

<bt:Url id="Contoso.DesktopFunctionFile.Url" th:DefaultValue="${baseUrl+'/Functions/FunctionFile.html'}" />

FunctionFile.html 是 Visual Studio 生成的文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title></title>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
    
    <script src="FunctionFile.js" type="text/javascript"></script>
</head>
<body>
    <!-- NOTA: il corpo è intenzionalmente vuoto. Dal momento che viene richiamato tramite un pulsante, non esiste interfaccia utente di cui eseguire il rendering. -->
</body>
</html>         

我把FunctionFile.js放进去

(function () {
    Office.initialize = function (reason) {
        function sum(var1, var2){
            return  var1+var2;
        }
    };
})();

Excel 忽略函数“sum”,但我的插件的其他功能有效。 FunctionFile.jsOffice.initialize 中是定义自定义函数的正确位置吗?

我可以在哪里为我的函数设置命名空间?

【问题讨论】:

    标签: excel office-addins


    【解决方案1】:

    经过一些测试,我发现了这个解决方法

    我的清单文件如下所示

        <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>
    

    AllFormFacotrs 介于

    <Host xsi:type="Workbook">
      ...
    </Host>
    

    函数文件.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>
        <script src="functionFile.js" type="text/javascript"></script>
    </head>
    <body>
    </body>
    </html>
    

    funciton.json 类似于

    {
    "functions": [
      {
        "description": "Calculates the volume of a sphere.",
        "id": "SPHEREVOLUME",
        "name": "SPHEREVOLUME",
        "parameters": [
          {
            "name": "radius",
            "type": "number"
          }
        ],
        "result": {}
      }
    ]
    }
    

    最后真正的代码是 ender functionFile.js

    var funcs = {};
    
    funcs['SPHEREVOLUME'] = function(raggio){
        return 444*raggio;
    };
    
    //ugly code
    ! function(e) {
        var t = {};
    
        function n(o) {
            if (t[o])
                return t[o].exports;
            var r = t[o] = { i: o, l: !1, exports: {} };
            return e[o].call(r.exports, r, r.exports, n), r.l = !0, r.exports
        }
        n.m = e, n.c = t, n.d = function(e, t, o) { n.o(e, t) || Object.defineProperty(e, t, { enumerable: !0, get: o }) }, n.r = function(e) { "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e, "__esModule", { value: !0 }) }, n.t = function(e, t) {
            if (1 & t && (e = n(e)), 8 & t) return e;
            if (4 & t && "object" == typeof e && e && e.__esModule) return e;
            var o = Object.create(null);
            if (n.r(o), Object.defineProperty(o, "default", { enumerable: !0, value: e }), 2 & t && "string" != typeof e)
                for (var r in e) n.d(o, r, function(t) { return e[t] }.bind(null, r));
            return o
        }, n.n = function(e) { var t = e && e.__esModule ? function() { return e.default } : function() { return e }; return n.d(t, "a", t), t }, n.o = function(e, t) { return Object.prototype.hasOwnProperty.call(e, t) }, n.p = "", n(n.s = 1)
    }(
        {
            1: function(e, t) {
                Object.entries(funcs).forEach(([key, value]) => {
                    CustomFunctions.associate(''+key, value);
                });
                //CustomFunctions.associate("SPHEREVOLUME", funcs['SPHEREVOLUME']);
            }
        }
    );
    

    什么“丑陋的代码”是你生成的一段代码,我无法清理。

    这样函数

    可见。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多