Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
LockService
Ngăn chặn việc truy cập đồng thời vào các phần mã. Điều này có thể hữu ích khi bạn có nhiều người dùng hoặc quy trình sửa đổi một tài nguyên dùng chung và muốn ngăn chặn xung đột.
Lấy khoá ngăn người dùng hiện tại chạy đồng thời một phần mã.
Tài liệu chi tiết
getDocumentLock()
Lấy khoá ngăn mọi người dùng của tài liệu hiện tại chạy đồng thời một phần mã. Một phần mã được bảo vệ bằng khoá tài liệu có thể được thực thi đồng thời bởi các thực thể tập lệnh chạy trong ngữ cảnh của nhiều tài liệu, nhưng không được thực thi nhiều lần cho một tài liệu bất kỳ. Xin lưu ý rằng khoá thực sự không được mua cho đến khi Lock.tryLock(timeoutInMillis) hoặc Lock.waitLock(timeoutInMillis) được gọi. Nếu phương thức này được gọi bên ngoài ngữ cảnh của tài liệu chứa (chẳng hạn như từ một tập lệnh độc lập hoặc ứng dụng web), null sẽ được trả về.
Cầu thủ trả bóng
Lock – khoá trong phạm vi tập lệnh và tài liệu hiện tại, hoặc null nếu được gọi từ một tập lệnh độc lập hoặc ứng dụng web
getScriptLock()
Lấy khoá ngăn mọi người dùng chạy đồng thời một phần mã. Không thể thực thi đồng thời một phần mã được bảo vệ bằng khoá tập lệnh, bất kể danh tính của người dùng. Xin lưu ý rằng khoá thực sự không được mua cho đến khi Lock.tryLock(timeoutInMillis) hoặc Lock.waitLock(timeoutInMillis) được gọi.
Lấy khoá ngăn người dùng hiện tại chạy đồng thời một phần mã. Nhiều người dùng có thể thực thi đồng thời một phần mã được bảo vệ bằng khoá người dùng, nhưng không được thực thi nhiều lần cho một người dùng bất kỳ. Khoá là "riêng tư" đối với người dùng. Xin lưu ý rằng khoá thực sự không được mua cho đến khi Lock.tryLock(timeoutInMillis) hoặc Lock.waitLock(timeoutInMillis) được gọi.
Cầu thủ trả bóng
Lock – khoá trong phạm vi tập lệnh và người dùng hiện tại
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 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"]]