有没有一种方法可以在自定义函数中使用Excel.run()?

时间:2019-05-21 13:34:45

标签: office-js office-addins custom-functions-excel

作为“自定义函数”的结果,我想填充几个工作表单元格,将调用了该函数的单元格作为基本单元格。我必须返回一些无法容纳在一个单元格中的复杂数据。

所以问题是:是否可以在自定义函数中使用Excel.run()?还是有一种方法可以返回更复杂的数据(对象,对象数组)作为“自定义函数”的返回值?

这是我要执行的操作的简单示例,但它不起作用:

/**
 * Adds two numbers.
 * @customfunction
 * @param first First number
 * @param second Second number
 * @returns The sum of the two numbers.
 */
async function add(first: number, second: number): Promise<void> {
  return Excel.run(async context => {
    const worksheet = context.workbook.worksheets.getActiveWorksheet();
    const range = worksheet.getRangeByIndexes(0, 0, 1, 4);
    range.values = [[1, 2, 3, 4]];

    return context.sync();
  })
}
CustomFunctions.associate("ADD", add);

1 个答案:

答案 0 :(得分:0)

尚无法通过自定义函数使用Excel对象模型。就您的情况而言,据我了解,您可以通过返回类型为number [] []的矩阵来返回多个值,然后在Excel网格中将公式作为数组公式输入(使用Ctrl + Shift + Enter)。一旦动态数组功能广泛可用,就不再需要数组公式,结果将自动“溢出”到相邻单元格中。动态数组当前在Office Insider版本中处于预览状态。

相关问题