0

由于我的自定义函数,我想填充几个工作表单元格,将调用函数的单元格作为基本单元格。我必须返回一些不适合一个单元格的复杂数据。

所以问题是:有没有办法在自定义函数中使用 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);
4

1 回答 1

0

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

于 2019-05-21T17:15:14.827 回答