Skip to main content

attachEvent

Subscribes to the specified event and calls the callback function when the event fires.

Syntax

expression.attachEvent(eventName, callback);

expression - A variable that represents a Api class.

Parameters

NameRequired/OptionalData typeDefaultDescription
eventNameRequiredstringThe event name.
callbackRequiredfunctionFunction to be called when the event fires.

Returns

This method doesn't return any data.

Example

Subscribe to a change notification so code runs automatically when cells are edited in a spreadsheet.

// How do I run custom code whenever a cell value changes in a spreadsheet?

// React to user edits without polling by registering a listener for worksheet changes in a spreadsheet.

let worksheet = Api.GetActiveSheet();
let range = worksheet.GetRange("A1");
range.SetValue("1");
Api.attachEvent("onWorksheetChange", function(range){
console.log("onWorksheetChange");
console.log(range.GetAddress());
});