Edit

Share via


custom-functions-runtime package

Classes

CustomFunctions.Error

Use this class to handle errors and write custom error messages.

Interfaces

CustomFunctions.CancelableInvocation

Provides information about the invocation of a cancelable custom function. A cancelable custom function can provide a handler for the onCanceled event.

To indicate that a function is cancelable, in the metadata JSON file, the function options should specify: { "cancelable": true }

If the metadata JSON file is being generated from JSDoc comments, include the tag @cancelable.

CustomFunctions.Invocation

Provides information about the invocation of a custom function.

CustomFunctions.StreamingInvocation

Provides information about the invocation of a streaming custom function. A streaming custom function can provide results which can change over time.

Call setResult() one or more times to provide the result instead of returning a result from the function.

Enums

CustomFunctions.ErrorCode

Error codes for custom functions. The error codes will appear in the cell that invoked the function.

Custom error messages appear in addition to these error codes. Custom messages display in the error indicator menu, which is accessed by hovering over the error flag on each cell with an error.

Functions

CustomFunctions.associate(id, functionObject)

Associates the JavaScript function to the name given by the "id" property in the metadata JSON file.

CustomFunctions.associate(mappings)

Associates the JavaScript functions to the names given by the "id" properties in the metadata JSON file.

Function Details

CustomFunctions.associate(id, functionObject)

Associates the JavaScript function to the name given by the "id" property in the metadata JSON file.

export function associate(id: string, functionObject: Function): void;

Parameters

id

string

functionObject

Function

Returns

void

Examples

/**
* Adds two numbers together.
* @customfunction
* @param {number} first First number.
* @param {number} second Second number.
* @returns {number} The sum of the two numbers.
*/
// Define your custom function.
function add(first, second) {
  return first + second;
}

// Associate the "add" function with its ID from the JSON metadata. In this sample, the JSON metadata ID is "ADD".
CustomFunctions.associate("ADD", add);

CustomFunctions.associate(mappings)

Associates the JavaScript functions to the names given by the "id" properties in the metadata JSON file.

export function associate(mappings: { [key: string]: Function }): void;

Parameters

mappings

{ [key: string]: Function }

Returns

void

Examples

/**
* Calculates the area of a rectangle.
* @customfunction
* @param {number} width The width of the rectangle.
* @param {number} height The height of the rectangle.
* @returns {number} The area of the rectangle.
*/
function calculateArea(width, height) {
  return width * height;
}

/**
* Calculates the perimeter of a rectangle.
* @customfunction
* @param {number} width The width of the rectangle.
* @param {number} height The height of the rectangle.
* @returns {number} The perimeter of the rectangle.
*/
function calculatePerimeter(width, height) {
  return 2 * (width + height);
}

// Associate multiple functions at once using an object that maps JSON metadata IDs to function names.
// In this sample, the "calculateArea" and "calculatePerimeter" functions have the JSON metadata IDs "CALCULATEAREA" and "CALCULATEPERIMETER".
CustomFunctions.associate({
  "CALCULATEAREA": calculateArea,
  "CALCULATEPERIMETER": calculatePerimeter
});