0

我正在尝试从 Excel.js API 创建一个自定义函数调用方法。我遵循了Excel 自定义函数教程
但是我总是#VALUE!在工作表上得到错误值,调试时出现这个错误:

Verbose Runtime [Console] [Log] Unexpected CustomFunctions [Execution] [End] [Failure] [ExceptionThrown] Function=EXTRACTFORM ReferenceError: 'Excel' is not defined {}     
Unexpected  CustomFunctions [Execution] [Async] [End] [Failure] Function=EXTRACTFORM, Workbook=Book1.xlsx

我正在使用以下代码:

对于描述文件:

{
  "functions": [
    {
      "id": "EXTRACTFORM",
      "name": "EXTRACTFORM",
      "description": "Extract formData from relaunch button",
      "result": {
        "type": "string",
        "dimensionality": "scalar"
      },
      "parameters": [
        {
          "name": "address",
          "description": "",
          "type": "string",
          "dimensionality": "scalar"
        }
      ]
    }
  ]
}

js源码:

function run(address) {
    var context = new Excel.RequestContext();
    var range = context.workbook.worksheets.getActiveWorksheet().getRange(address);
    range.load();
    return context.sync()
        .then(function() {
            return range.values[0][0];
        });
}
CustomFunctions.associate("EXTRACTFORM", run);

和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>"Custom Functions Upgrade Test"</title>
      <script src="https://appsforoffice.microsoft.com/lib/1.1/hosted/custom-functions-runtime.js" type="text/javascript"></script>
      <script src="./extractForm.js" type="text/javascript"></script>
    </head>
    
    <body>
        <h1>"Custom function"</h1>
    </body>
</html>

谢谢您的帮助 !

4

1 回答 1

0

为了使自定义函数调用 Excel.js API 方法,您需要将加载项配置为使用共享运行时。请注意,通过自定义函数调用 Office.js有一些限制。

于 2020-09-13T11:09:24.993 回答