Class LockService

  • LockService is used to prevent concurrent access to sections of code, useful when multiple users or processes modify a shared resource.

  • getDocumentLock() gets a lock that prevents any user of the current document from concurrently running a section of code.

  • getScriptLock() gets a lock that prevents any user from concurrently running a section of code.

  • getUserLock() gets a lock that prevents the current user from concurrently running a section of code.

LockService

Prevents concurrent access to sections of code. This can be useful when you have multiple usersor processes modifying a shared resource and want to prevent collisions.

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.

Detailed documentation

getDocumentLock()

Gets a lock that prevents any user of the current document from concurrently running a sectionof code. A code section guarded by a document lock can be executed simultaneously by scriptinstances running in the context of different documents, but by no more than one execution forany given document. Note that the lock is not actually acquired untilLock.tryLock(timeoutInMillis)orLock.waitLock(timeoutInMillis) is called. If this method is called outside of the context of acontaining document (such as from a standalone script or webapp),null is returned.

Return

Lock|null — a lock scoped to the script and current document, ornull if called from a standalone script or webapp


getScriptLock()

Gets a lock that prevents any user from concurrently running a section of code. A code sectionguarded by a script lock cannot be executed simultaneously regardless of the identity of theuser. Note that the lock is not actually acquired untilLock.tryLock(timeoutInMillis) orLock.waitLock(timeoutInMillis) is called.

Return

Lock — a lock scoped to the script


getUserLock()

Gets a lock that prevents the current user from concurrently running a section of code. A codesection guarded by a user lock can be executed simultaneously by different users, but by nomore than one execution for any given user. The lock is "private" to the user. Note that thelock is not actually acquired untilLock.tryLock(timeoutInMillis) orLock.waitLock(timeoutInMillis) iscalled.

Return

Lock — a lock scoped to the script and current user

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.