Lock Service
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ล็อก
บริการนี้ช่วยให้สคริปต์ป้องกันไม่ให้เข้าถึงส่วนต่างๆ ของโค้ดพร้อมกันได้ ซึ่งจะมีประโยชน์เมื่อคุณมีหลายผู้ใช้หรือหลายกระบวนการที่แก้ไขทรัพยากรที่แชร์และต้องการป้องกันการทับซ้อนกัน
ชั้นเรียน
ชื่อ | รายละเอียดแบบย่อ |
Lock | การนําเสนอการล็อกแบบยกเว้นซึ่งกันและกัน |
LockService | ป้องกันไม่ให้เข้าถึงส่วนต่างๆ ของโค้ดพร้อมกัน |
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
hasLock() | Boolean | แสดงผลเป็น "จริง" หากได้รับข้อมูลล็อก |
releaseLock() | void | ปล่อยการล็อกเพื่อให้กระบวนการอื่นๆ ที่รอการล็อกดำเนินการต่อได้ |
tryLock(timeoutInMillis) | Boolean | พยายามขอล็อก โดยระบบจะหมดเวลาหลังจากผ่านไปตามจำนวนมิลลิวินาทีที่ระบุ |
waitLock(timeoutInMillis) | void | พยายามขอล็อก โดยหมดเวลาพร้อมกับข้อยกเว้นหลังจากผ่านไปตามจำนวนมิลลิวินาทีที่ระบุ |
เมธอด
วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
getDocumentLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้เอกสารปัจจุบันเรียกใช้ส่วนโค้ดพร้อมกัน |
getScriptLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้เรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน |
getUserLock() | Lock | รับล็อกที่ป้องกันไม่ให้ผู้ใช้ปัจจุบันเรียกใช้โค้ดส่วนใดส่วนหนึ่งพร้อมกัน |
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe Lock service in Apps Script helps prevent conflicts when multiple users or processes access shared resources.\u003c/p\u003e\n"],["\u003cp\u003eIt provides different types of locks: Document Lock, Script Lock, and User Lock, each controlling access at various levels.\u003c/p\u003e\n"],["\u003cp\u003eLocks can be acquired, released, and checked for status using methods like \u003ccode\u003etryLock\u003c/code\u003e, \u003ccode\u003ereleaseLock\u003c/code\u003e, and \u003ccode\u003ehasLock\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTimeouts can be set while acquiring locks to avoid indefinite waiting using methods like \u003ccode\u003etryLock\u003c/code\u003e and \u003ccode\u003ewaitLock\u003c/code\u003e.\u003c/p\u003e\n"]]],["The service provides a mechanism to prevent concurrent code access, crucial for shared resource modification. `LockService` offers methods like `getDocumentLock`, `getScriptLock`, and `getUserLock` to acquire different types of locks. The `Lock` class manages the lock itself, with methods like `hasLock` to check if a lock has been obtained, `releaseLock` to release it, and `tryLock` and `waitLock` to acquire a lock with a timeout.\n"],null,["# Lock Service\n\nLock\n\nThis service allows scripts to prevent concurrent access to sections of\ncode. This can be useful when you have multiple users or processes modifying a\nshared resource and want to prevent collisions.\n\nClasses\n-------\n\n| Name | Brief description |\n|---------------------------------------------------------|-------------------------------------------------|\n| [Lock](/apps-script/reference/lock/lock) | A representation of a mutual-exclusion lock. |\n| [LockService](/apps-script/reference/lock/lock-service) | Prevents concurrent access to sections of code. |\n\n[Lock](/apps-script/reference/lock/lock)\n----------------------------------------\n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------------------------------------------|-------------|-------------------------------------------------------------------------------------------------------|\n| [hasLock()](/apps-script/reference/lock/lock#hasLock()) | `Boolean` | Returns true if the lock was acquired. |\n| [releaseLock()](/apps-script/reference/lock/lock#releaseLock()) | `void` | Releases the lock, allowing other processes waiting on the lock to continue. |\n| [tryLock(timeoutInMillis)](/apps-script/reference/lock/lock#tryLock(Integer)) | `Boolean` | Attempts to acquire the lock, timing out after the provided number of milliseconds. |\n| [waitLock(timeoutInMillis)](/apps-script/reference/lock/lock#waitLock(Integer)) | `void` | Attempts to acquire the lock, timing out with an exception after the provided number of milliseconds. |\n\n[LockService](/apps-script/reference/lock/lock-service)\n-------------------------------------------------------\n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------------------------------------------|------------------------------------------|---------------------------------------------------------------------------------------------------------|\n| [getDocumentLock()](/apps-script/reference/lock/lock-service#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()](/apps-script/reference/lock/lock-service#getScriptLock()) | [Lock](/apps-script/reference/lock/lock) | Gets a lock that prevents any user from concurrently running a section of code. |\n| [getUserLock()](/apps-script/reference/lock/lock-service#getUserLock()) | [Lock](/apps-script/reference/lock/lock) | Gets a lock that prevents the current user from concurrently running a section of code. |"]]