Class LockService
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
LockService
Zapobiega jednoczesnemu dostępowi do sekcji kodu. Może to być przydatne, gdy masz wielu użytkowników lub procesów modyfikujących udostępniony zasób i chcesz zapobiec konfliktom.
Metody
Metoda | Zwracany typ | Krótki opis |
getDocumentLock() | Lock | Pobiera blokadę, która uniemożliwia dowolnemu użytkownikowi bieżącego dokumentu jednoczesne uruchamianie sekcji kodu. |
getScriptLock() | Lock | Uzyskiwanie blokady, która uniemożliwia dowolnemu użytkownikowi jednoczesne uruchamianie sekcji kodu. |
getUserLock() | Lock | Pobiera blokadę, która uniemożliwia bieżącemu użytkownikowi jednoczesne uruchamianie sekcji kodu. |
Szczegółowa dokumentacja
getDocumentLock()
Pobiera blokadę, która uniemożliwia dowolnemu użytkownikowi bieżącego dokumentu jednoczesne uruchamianie sekcji kodu. Sekcja kodu chroniona przez blokadę dokumentu może być wykonywana jednocześnie przez instancje skryptu działające w kontekście różnych dokumentów, ale nie więcej niż raz w przypadku każdego dokumentu. Pamiętaj, że blokada jest faktycznie pobierana dopiero po wywołaniu funkcji Lock.tryLock(timeoutInMillis)
lub Lock.waitLock(timeoutInMillis)
. Jeśli ta metoda jest wywoływana poza kontekstem dokumentu zawierającego (np. z osobnego skryptu lub aplikacji internetowej), zwracana jest wartość null
.
Powrót
Lock
– blokada ograniczona do skryptu i bieżącego dokumentu lub null
, jeśli jest wywoływana z osobnego skryptu lub aplikacji internetowej.
getScriptLock()
Uzyskiwanie blokady, która uniemożliwia dowolnemu użytkownikowi jednoczesne uruchamianie sekcji kodu. Sekcja kodu chroniona przez blokadę skryptu nie może być wykonywana jednocześnie niezależnie od tożsamości użytkownika. Pamiętaj, że blokada jest faktycznie pobierana dopiero po wywołaniu funkcji Lock.tryLock(timeoutInMillis)
lub Lock.waitLock(timeoutInMillis)
.
Powrót
Lock
– blokada ograniczona do skryptu.
getUserLock()
Pobiera blokadę, która uniemożliwia bieżącemu użytkownikowi jednoczesne uruchamianie sekcji kodu. Sekcja kodu chroniona za pomocą blokady użytkownika może być wykonywana jednocześnie przez różnych użytkowników, ale nie więcej niż jeden raz przez danego użytkownika. Blokada jest „prywatna” dla użytkownika. Pamiętaj, że blokada jest faktycznie nabywana dopiero po wywołaniu funkcji Lock.tryLock(timeoutInMillis)
lub Lock.waitLock(timeoutInMillis)
.
Powrót
Lock
– blokada ograniczona do skryptu i obecnego użytkownika
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-26 UTC.
[null,null,["Ostatnia aktualizacja: 2025-07-26 UTC."],[[["\u003cp\u003e\u003ccode\u003eLockService\u003c/code\u003e prevents concurrent access to shared resources in Apps Script, avoiding collisions when multiple users or processes modify them.\u003c/p\u003e\n"],["\u003cp\u003eIt offers three types of locks: \u003ccode\u003egetDocumentLock()\u003c/code\u003e, \u003ccode\u003egetScriptLock()\u003c/code\u003e, and \u003ccode\u003egetUserLock()\u003c/code\u003e, each with different scopes of access restriction.\u003c/p\u003e\n"],["\u003cp\u003eThese locks are acquired using \u003ccode\u003eLock.tryLock(timeoutInMillis)\u003c/code\u003e or \u003ccode\u003eLock.waitLock(timeoutInMillis)\u003c/code\u003e methods for controlled access to critical code sections.\u003c/p\u003e\n"],["\u003cp\u003eEach lock type returns a \u003ccode\u003eLock\u003c/code\u003e object that provides methods to manage the lock, ensuring data integrity and consistency in shared environments.\u003c/p\u003e\n"]]],[],null,["# Class LockService\n\nLockService\n\nPrevents concurrent access to sections of code. This can be useful when you have multiple users\nor processes modifying a shared resource and want to prevent collisions. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------|------------------------------------------|---------------------------------------------------------------------------------------------------------|\n| [getDocumentLock()](#getDocumentLock()) | [Lock](/apps-script/reference/lock/lock) | Gets a lock that prevents any user of the current document from concurrently running a section of code. |\n| [getScriptLock()](#getScriptLock()) | [Lock](/apps-script/reference/lock/lock) | Gets a lock that prevents any user from concurrently running a section of code. |\n| [getUserLock()](#getUserLock()) | [Lock](/apps-script/reference/lock/lock) | Gets a lock that prevents the current user from concurrently running a section of code. |\n\nDetailed documentation\n----------------------\n\n### `get``Document``Lock()`\n\nGets a lock that prevents any user of the current document from concurrently running a section\nof code. A code section guarded by a document lock can be executed simultaneously by script\ninstances running in the context of different documents, but by no more than one execution for\nany given document. Note that the lock is not actually acquired until [Lock.tryLock(timeoutInMillis)](/apps-script/reference/lock/lock#tryLock(Integer))\nor [Lock.waitLock(timeoutInMillis)](/apps-script/reference/lock/lock#waitLock(Integer)) is called. If this method is called outside of the context of a\ncontaining document (such as from a standalone script or webapp), `null` is returned.\n\n#### Return\n\n\n[Lock](/apps-script/reference/lock/lock) --- a lock scoped to the script and current document, or `null` if called from a\nstandalone script or webapp\n\n*** ** * ** ***\n\n### `get``Script``Lock()`\n\nGets a lock that prevents any user from concurrently running a section of code. A code section\nguarded by a script lock cannot be executed simultaneously regardless of the identity of the\nuser. Note that the lock is not actually acquired until [Lock.tryLock(timeoutInMillis)](/apps-script/reference/lock/lock#tryLock(Integer)) or [Lock.waitLock(timeoutInMillis)](/apps-script/reference/lock/lock#waitLock(Integer)) is called.\n\n#### Return\n\n\n[Lock](/apps-script/reference/lock/lock) --- a lock scoped to the script\n\n*** ** * ** ***\n\n### `get``User``Lock()`\n\nGets a lock that prevents the current user from concurrently running a section of code. A code\nsection guarded by a user lock can be executed simultaneously by different users, but by no\nmore than one execution for any given user. The lock is \"private\" to the user. Note that the\nlock is not actually acquired until [Lock.tryLock(timeoutInMillis)](/apps-script/reference/lock/lock#tryLock(Integer)) or [Lock.waitLock(timeoutInMillis)](/apps-script/reference/lock/lock#waitLock(Integer)) is\ncalled.\n\n#### Return\n\n\n[Lock](/apps-script/reference/lock/lock) --- a lock scoped to the script and current user"]]