首页 > 解决方案 > 使用 Excel JS 自定义函数文件在哪里调用 Office.initialize?

问题描述

我正在 Excel JS 中创建一个自定义函数,我想使用其他 Office.js 功能。我正在尝试在自定义函数中初始化 Office.js,如下所示:

async function getData(indicator, date, handler) {
await Office.onReady();
if (date !== "earliest" && date !== "latest" && !date.includes("-")) {
  const newDate = new Date((parseFloat(date) - (25567 + 2))*86400*1000); // Converts Excel date, days since 1900, to YYYY-MM-DD as per https://gist.github.com/christopherscott/2782634
  date = formatDate(newDate)
}
dd_value = new DDValue("A1", indicator, date)
console.log(dd_value)
try {
  await Excel.run(function (context) {
      var range = context.workbook.getSelectedRange();
      range.load("address");

      return context.sync()
          .then(function () {
              console.log(`The address of the selected range is "${range.address}"`);
          });
  }).catch(errorHandlerFunction);
} catch(error) {
  console.log("ERROR")
  console.log(error)
}
}

但是,我收到以下错误:

Uncaught TypeError: Cannot redefine property: context

Uncaught Error: Office.js has not fully loaded. Your app must call "Office.onReady()" as part of it's loading sequence (or set the "Office.initialize" function). If your app has this functionality, try reloading this page.

我还尝试在 index.html 页面中初始化 Office.js(只是标题中的一个脚本),但这也没有奏效。

有没有人尝试在自定义函数中实现其他 Office.js 功能?Office.js 初始化如何为您工作?

提前致谢

标签: office-js

解决方案


这在今天是不可能的,但在我们的雷达上。

a) 我们正在积极开发传递函数调用单元格地址的功能(所选范围在这种情况下不起作用,因为用户可能选择了与输入函数不同的单元格)。

b) 如果您需要额外的 API,想知道您的场景吗?我们将以某种方式将 office.js 暴露给自定义函数,但我们通常在这里保持谨慎,因为在计算期间调用 excel 可能会导致不可预测的结果(就像今天 XLLand VBA UDF 一样)。

谢谢!


推荐阅读