有没有办法在自定义函数中使用 Excel.run() ?
Is there a way to use Excel.run() inside Custom Function?
由于我的自定义函数,我想填充几个工作表单元格,将调用该函数的单元格作为基本单元格。我必须 return 一些复杂的数据,这些数据无法放在一个单元格中。
所以问题是:有没有办法在自定义函数中使用 Excel.run()?或者有没有办法将 return 更复杂的数据(对象、对象数组)作为自定义函数的 return 值?
这是我正在尝试做的简单示例,但它不起作用:
/**
* 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);
尚无法通过自定义函数使用 Excel 对象模型。对于您的场景,据我了解,您可以通过 returning 类型为 number[][] 的矩阵并在 Excel 网格中输入公式作为数组来 return 多个值公式(使用 Ctrl+Shift+Enter)。一旦动态数组功能广泛可用,将不再需要数组公式,结果将自动 "spill" 到相邻的单元格。动态数组当前在 Office 预览体验成员版本中处于预览状态。
意识到这个线程现在已经过时了,但是对于任何回顾这些以更好地理解从自定义函数调用的 Excel API 的人(比如我自己)来说,有些事情你应该知道。您应该了解从自定义函数调用 Excel API 的限制,尤其是当您计划更新环境时。有关详细信息,请参阅 here。
由于我的自定义函数,我想填充几个工作表单元格,将调用该函数的单元格作为基本单元格。我必须 return 一些复杂的数据,这些数据无法放在一个单元格中。
所以问题是:有没有办法在自定义函数中使用 Excel.run()?或者有没有办法将 return 更复杂的数据(对象、对象数组)作为自定义函数的 return 值?
这是我正在尝试做的简单示例,但它不起作用:
/**
* 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);
尚无法通过自定义函数使用 Excel 对象模型。对于您的场景,据我了解,您可以通过 returning 类型为 number[][] 的矩阵并在 Excel 网格中输入公式作为数组来 return 多个值公式(使用 Ctrl+Shift+Enter)。一旦动态数组功能广泛可用,将不再需要数组公式,结果将自动 "spill" 到相邻的单元格。动态数组当前在 Office 预览体验成员版本中处于预览状态。
意识到这个线程现在已经过时了,但是对于任何回顾这些以更好地理解从自定义函数调用的 Excel API 的人(比如我自己)来说,有些事情你应该知道。您应该了解从自定义函数调用 Excel API 的限制,尤其是当您计划更新环境时。有关详细信息,请参阅 here。