Class LockService
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
LockService
ป้องกันไม่ให้เข้าถึงส่วนต่างๆ ของโค้ดพร้อมกัน ซึ่งอาจเป็นประโยชน์เมื่อคุณมีผู้ใช้หรือกระบวนการหลายรายการที่แก้ไขทรัพยากรที่แชร์และต้องการป้องกันการทับซ้อนกัน
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
getDocumentLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้เอกสารปัจจุบันเรียกใช้ส่วนโค้ดพร้อมกัน |
getScriptLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้เรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน |
getUserLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้ปัจจุบันเรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน |
เอกสารประกอบโดยละเอียด
getDocumentLock()
รับล็อกที่ป้องกันไม่ให้ผู้ใช้เอกสารปัจจุบันเรียกใช้ส่วนโค้ดพร้อมกัน อินสแตนซ์สคริปต์ที่ทำงานในบริบทของเอกสารต่างๆ สามารถเรียกใช้ส่วนโค้ดที่มีการป้องกันโดยล็อกเอกสารพร้อมกันได้ แต่ไม่เกิน 1 ครั้งสำหรับเอกสารหนึ่งๆ โปรดทราบว่าระบบจะไม่รับล็อกจนกว่าจะมีการเรียกใช้ Lock.tryLock(timeoutInMillis)
หรือ Lock.waitLock(timeoutInMillis)
หากเรียกใช้เมธอดนี้นอกบริบทของเอกสารที่มี (เช่น จากสคริปต์หรือเว็บแอปแบบสแตนด์อโลน) ระบบจะแสดงผล null
รีเทิร์น
Lock
— ล็อกที่มีขอบเขตเป็นสคริปต์และเอกสารปัจจุบัน หรือ null
หากเรียกใช้สคริปต์หรือเว็บแอปแบบสแตนด์อโลน
getScriptLock()
รับล็อกที่ป้องกันไม่ให้ผู้ใช้เรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน ส่วนโค้ดที่มีการป้องกันโดยล็อกสคริปต์จะไม่สามารถเรียกใช้ได้พร้อมกัน ไม่ว่าผู้ใช้จะเป็นใครก็ตาม โปรดทราบว่าระบบจะไม่รับสิทธิ์เข้าถึงล็อกจนกว่าจะมีการเรียกใช้ Lock.tryLock(timeoutInMillis)
หรือ Lock.waitLock(timeoutInMillis)
รีเทิร์น
Lock
— การล็อกที่มีขอบเขตเป็นสคริปต์
getUserLock()
รับล็อกที่ป้องกันไม่ให้ผู้ใช้ปัจจุบันเรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน ผู้ใช้หลายคนสามารถเรียกใช้ส่วนโค้ดที่มีการป้องกันโดยล็อกผู้ใช้พร้อมกันได้ แต่ผู้ใช้แต่ละรายจะเรียกใช้ได้ไม่เกิน 1 ครั้ง ล็อกเป็นแบบ "ส่วนตัว" สำหรับผู้ใช้ โปรดทราบว่าระบบจะไม่รับล็อกจนกว่าจะมีการเรียกใช้ Lock.tryLock(timeoutInMillis)
หรือ Lock.waitLock(timeoutInMillis)
รีเทิร์น
Lock
— การล็อกที่มีขอบเขตระดับสคริปต์และผู้ใช้ปัจจุบัน
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 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"]]