Is there a way to use Excel.run() inside Custom Function?

北战南征 提交于 2020-03-05 06:09:11

问题


As a result of my Custom Function I want to fill few worksheet cells, taking cell that the function was invoked as a base cell. I have to return some complex data, that wouldn't fit in one cell.

So the question is: is there a way to use Excel.run() inside the Custom Function? Or is there a way to return more complex data (objects, array of objects) as the return value of Custom Function?

This is simple example of what I'm trying to do, but it doesn't work:

/**
 * 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:


It is not yet possible to work with the Excel object model from a custom function. For your scenario, as I understand it, you can return multiple values by returning a matrix of the type number[][] and enter the formula in the Excel grid as an array formula (using Ctrl+Shift+Enter). Once the dynamic arrays feature is available broadly, an array formula will no longer be needed, and the result will automatically "spill" to the adjacent cells. Dynamic arrays is currently in preview in the Office Insider builds.



来源:https://stackoverflow.com/questions/56239514/is-there-a-way-to-use-excel-run-inside-custom-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!