CustomFunctions.StreamingInvocation interface

提供有关流式处理自定义函数的调用的信息。 流式处理自定义函数可以提供随时间变化的结果。

调用 setResult() 一次或多次以提供结果,而不是从 函数返回结果。

Extends

属性

setResult

设置自定义函数的结果。 可以多次调用。

属性详细信息

setResult

设置自定义函数的结果。 可以多次调用。

setResult: (value: ResultType | Error) => void;

属性值

(value: ResultType | CustomFunctions.Error) => void

注解

[ API set: CustomFunctionsRuntime 1.1 ]

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/streaming-function.yaml

/** @CustomFunction 
 * @description Increments the cell with a given amount at a specified interval in milliseconds.
 * @param {number} amount - The amount to add to the cell value on each increment.
 * @param {number} interval - The time in milliseconds to wait before the next increment on the cell.
 * @param {CustomFunctions.StreamingInvocation<number>} invocation - Parameter to send results to Excel
 *     or respond to the user canceling the function.
 * @returns An incrementing value.
 */
function increment(amount: number, interval: number, invocation: CustomFunctions.StreamingInvocation<number>): void {
  let result = 0;
  const timer = setInterval(() => {
    result += amount;
    invocation.setResult(result);
  }, interval);

  invocation.onCanceled = () => {
    clearInterval(timer);
  }
}