Lock Service

  • The Lock service in Apps Script prevents concurrent access to sections of code, useful for managing shared resources and preventing collisions.

  • The LockService class provides methods to get different types of locks: document, script, and user.

  • The Lock class represents a mutual-exclusion lock and includes methods to check if a lock is acquired, release a lock, and attempt to acquire a lock with or without timing out.

Lock

This service allows scripts to prevent concurrent access to sections ofcode. This can be useful when you have multiple users or processes modifying ashared resource and want to prevent collisions.

Classes

NameBrief description
LockA representation of a mutual-exclusion lock.
LockServicePrevents concurrent access to sections of code.

Lock

Methods

MethodReturn typeBrief description
hasLock()BooleanReturns true if the lock was acquired.
releaseLock()voidReleases the lock, allowing other processes waiting on the lock to continue.
tryLock(timeoutInMillis)BooleanAttempts to acquire the lock, timing out after the provided number of milliseconds.
waitLock(timeoutInMillis)voidAttempts to acquire the lock, timing out with an exception after the provided number ofmilliseconds.

LockService

Methods

MethodReturn typeBrief description
getDocumentLock()Lock|nullGets a lock that prevents any user of the current document from concurrently running a sectionof code.
getScriptLock()LockGets a lock that prevents any user from concurrently running a section of code.
getUserLock()LockGets a lock that prevents the current user from concurrently running a section of code.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.