Class CacheService
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
快取服務快取服務可讓您存取快取,以便短期儲存資料。
這個類別可讓您取得特定快取執行個體。公開快取適用於不依賴使用者存取指令碼的內容。私人快取適用於特定使用者相關內容,例如設定或近期活動。
您寫入快取的資料不保證會持續到到期時間。您必須準備好從所有讀取作業中傳回 null
。
內容詳盡的說明文件
getDocumentCache()
取得範圍限定於目前文件和指令碼的快取執行個體。文件快取是指包含指令碼的當前文件。用於儲存特定於目前文件的指令碼資訊。如果在包含文件的內容區塊外呼叫此方法 (例如從獨立指令碼或網頁應用程式),此方法會傳回 null
。
// Gets a cache that is specific to the current document containing the script
const cache = CacheService.getDocumentCache();
回攻員
Cache
:文件快取例項,如果沒有包含文件則為 null
getScriptCache()
取得範圍限定於指令碼的快取執行個體。指令碼快取是指令碼的所有使用者共用。用於儲存與目前使用者無關的資訊。
// Gets a cache that is common to all users of the script
const cache = CacheService.getScriptCache();
回攻員
Cache
:指令碼快取例項
getUserCache()
取得範圍限定於目前使用者和指令碼的快取例項。使用者快取是特定於指令碼的目前使用者。用於儲存特定於目前使用者的指令碼資訊。
// Gets a cache that is specific to the current user of the script
const cache = CacheService.getUserCache();
回攻員
Cache
:使用者快取例項
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eCacheService\u003c/code\u003e provides short-term storage for script data, offering document, script, and user-specific scopes.\u003c/p\u003e\n"],["\u003cp\u003eData stored using \u003ccode\u003eCacheService\u003c/code\u003e is not guaranteed to persist and may return \u003ccode\u003enull\u003c/code\u003e when retrieved.\u003c/p\u003e\n"],["\u003cp\u003eThree cache instances are available: \u003ccode\u003egetDocumentCache()\u003c/code\u003e for document-specific data, \u003ccode\u003egetScriptCache()\u003c/code\u003e for script-wide data, and \u003ccode\u003egetUserCache()\u003c/code\u003e for user-specific data.\u003c/p\u003e\n"],["\u003cp\u003eWhen using \u003ccode\u003egetDocumentCache()\u003c/code\u003e, it returns \u003ccode\u003enull\u003c/code\u003e if called outside a document context (standalone script or web app).\u003c/p\u003e\n"]]],[],null,["# Class CacheService\n\nCacheService\n\nCacheService allows you to access a cache for short term storage of data.\n\nThis class lets you get a specific cache instance. Public caches are for things that are not\ndependent on which user is accessing your script. Private caches are for things which are\nuser-specific, like settings or recent activity.\n\nThe data you write to the cache is not guaranteed to persist until its expiration time. You\nmust be prepared to get back `null` from all reads. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------|---------------------------------------------|--------------------------------------------------------------------|\n| [getDocumentCache()](#getDocumentCache()) | [Cache](/apps-script/reference/cache/cache) | Gets the cache instance scoped to the current document and script. |\n| [getScriptCache()](#getScriptCache()) | [Cache](/apps-script/reference/cache/cache) | Gets the cache instance scoped to the script. |\n| [getUserCache()](#getUserCache()) | [Cache](/apps-script/reference/cache/cache) | Gets the cache instance scoped to the current user and script. |\n\nDetailed documentation\n----------------------\n\n### `get``Document``Cache()`\n\nGets the cache instance scoped to the current document and script. Document caches are specific\nto the current document which contains the script. Use these to store script information that\nis specific to the current document. If this method is called outside of the context of a\ncontaining document (such as from a standalone script or web app), this method returns `null`.\n\n```javascript\n// Gets a cache that is specific to the current document containing the script\nconst cache = CacheService.getDocumentCache();\n```\n\n#### Return\n\n\n[Cache](/apps-script/reference/cache/cache) --- a document cache instance, or `null` if there is no containing document\n\n*** ** * ** ***\n\n### `get``Script``Cache()`\n\nGets the cache instance scoped to the script. Script caches are common to all users of the\nscript. Use these to store information that is not specific to the current user.\n\n```javascript\n// Gets a cache that is common to all users of the script\nconst cache = CacheService.getScriptCache();\n```\n\n#### Return\n\n\n[Cache](/apps-script/reference/cache/cache) --- a script cache instance\n\n*** ** * ** ***\n\n### `get``User``Cache()`\n\nGets the cache instance scoped to the current user and script. User caches are specific to the\ncurrent user of the script. Use these to store script information that is specific to the\ncurrent user.\n\n```javascript\n// Gets a cache that is specific to the current user of the script\nconst cache = CacheService.getUserCache();\n```\n\n#### Return\n\n\n[Cache](/apps-script/reference/cache/cache) --- a user cache instance"]]