apps-script-utils 2.1.1 Help

requireService

function requireService<T>( service: T | null | undefined, message: string = "Service is not defined." ): T;

An advanced service that was never enabled is simply an undefined global, and the failure that follows is a ReferenceError far from its cause. This turns it into a named exception with a message that says what to switch on.

Parameters

Parameter

Type

Description

service

T \| null \| undefined

The service to check.

message (optional) = "Service is not defined."

string

The message of the exception. A default is used when omitted.

Returns

T — the same service.

Throws

Exception

Condition

ServiceIsNotDefinedException

the service is null or undefined.

Examples

In use

// An advanced service is a global that only exists once it has been enabled. const directory = requireService(AdminDirectory, "Enable the Admin SDK service.");

See also

Source

src/appsscript/requireService.ts

23 September 2026

This documentation was generated with AI (Claude) from the library's source code.