1

我有一个 CustomFunctions javascript,它可以与同步函数完美配合,但是一旦我尝试使用 Async 函数,我就会得到:“

https://appsforoffice.microsoft.com/lib/beta/hosted/excel-win32-16.01.js 0x800a139e第 21 行第 707859 列未处理的异常 - Der opstod en JavaScript-kørselsfejl:意外消息类型

“这发生在对我的 js 文件调用任何 javascript 代码之前。消息类型是 1002,因此在 excel-win-16.01.js 中指定位置的 javascript 中,当然会抛出错误:”

if(i[t].messageType===1001)u.push(i[t]);else throw OfficeExtension.Utility.createRuntimeError(st.generalException,"意外消息类型","

该函数的 json 描述为:

    {
      "name": "helloasync",
      "description": "simple test string return",
      "helpUrl": "https://www.konsolidator.com",
      "result": { 
        "type": "string",
        "dimensionality": "scalar"
      },
      "parameters": [],
      "options": {"sync": false}
    },
        {
      "name": "ADD42ASYNC",
      "description": "asynchronously wait 250ms, then add 42",
      "helpUrl": "http://dev.office.com",
      "result": {
        "type": "number",
        "dimensionality": "scalar"
      },
      "parameters": [
        {
          "name": "num",
          "description": "Number",
          "type": "number",
          "dimensionality": "scalar"
        }
      ],
      "options": {
        "sync": false
      }
    }

JS代码:

function helloasync() {
    return new OfficeExtension.Promise(function (setResult) {
        setTimeout(function () {
            setResult("hello");
        }, 1000);
    });
}

function ADD42ASYNC(num) {
    // waits 1 second before returning the result
    return new OfficeExtension.Promise(function (resolve,reject) {
        //resolve(num);
        //reject(num);
        setTimeout(function () {
            resolve(num + 42);
        }, 1000);
    });
}

我所有的异步自定义函数都失败了!侧边栏异步函数和同步函数一样工作。

4

1 回答 1

0

您可以尝试引用以下版本的 Office.js 吗?

http://unpkg.com/@microsoft/office-js@custom-functions-preview/dist/office.js

CDN 目前有点过时,但以上内容应该为您提供最新最好的。

UPDATE,要清楚(我不确定当前是否在文档中指出了这一点,我正在跟进):您的 HTML 页面应如下所示:

    <script src="https://unpkg.com/@microsoft/office-js@custom-functions-preview/dist/office.js" type="text/javascript"></script>
    <script src="customfunctions.js" type="text/javascript"></script>
    <script type="text/javascript">
        Office.Preview.startCustomFunctions();
    </script>

中间脚本在哪里(customfunctions.js是对您为编写这些函数而创建的任何 JavaScript 文件的引用)。

于 2018-05-07T21:31:23.443 回答