Mantieni tutto organizzato con le raccolte Salva e classifica i contenuti in base alle tue preferenze.
LockService
Impedisce l'accesso simultaneo a sezioni di codice. Questa opzione può essere utile quando più utenti o processi modificano una risorsa condivisa e vuoi evitare collisioni.
Ottiene un blocco che impedisce all'utente corrente di eseguire contemporaneamente una sezione di codice.
Documentazione dettagliata
getDocumentLock()
Recupera un blocco che impedisce a qualsiasi utente del documento corrente di eseguire contemporaneamente una sezione di codice. Una sezione di codice protetta da un blocco del documento può essere eseguita contemporaneamente da istanze di script in esecuzione nel contesto di documenti diversi, ma non da più di un'esecuzione per un determinato documento. Tieni presente che la serratura non viene acquisita finché non viene chiamata Lock.tryLock(timeoutInMillis) o Lock.waitLock(timeoutInMillis). Se questo metodo viene chiamato al di fuori del contesto di un documento contenente (ad esempio da uno script o una web app autonomi), viene restituito null.
Invio
Lock: un blocco limitato allo script e al documento corrente oppure null se chiamato da uno script o da una web app autonomi
getScriptLock()
Ottiene un blocco che impedisce a qualsiasi utente di eseguire contemporaneamente una sezione di codice. Una sezione di codice protetta da un blocco dello script non può essere eseguita contemporaneamente, indipendentemente dall'identità dell'utente. Tieni presente che la serratura non viene effettivamente acquisita finché non viene chiamata Lock.tryLock(timeoutInMillis) o Lock.waitLock(timeoutInMillis).
Ottiene un blocco che impedisce all'utente corrente di eseguire contemporaneamente una sezione di codice. Una sezione di codice protetta da un blocco utente può essere eseguita contemporaneamente da utenti diversi, ma non da più di un utente alla volta. La serratura è "privata" per l'utente. Tieni presente che il blocco non viene effettivamente acquisito finché non viene chiamato Lock.tryLock(timeoutInMillis) o Lock.waitLock(timeoutInMillis).
Invio
Lock: un blocco limitato allo script e all'utente corrente
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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"]]