Class CacheService
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
CacheService
借助 CacheService,您可以访问缓存以短期存储数据。
借助此类,您可以获取特定的缓存实例。公共缓存适用于不依赖于哪个用户访问脚本的内容。私密缓存用于存储特定于用户的内容,例如设置或近期活动。
我们无法保证您写入缓存的数据会一直保留到其过期时间。您必须准备好从所有读取中返回 null
。
详细文档
getDocumentCache()
获取作用域限定为当前文档和脚本的缓存实例。文档缓存是特定于包含脚本的当前文档的。您可以使用这些属性来存储特定于当前文档的脚本信息。如果此方法是在包含文档的上下文之外调用(例如从独立脚本或 Web 应用调用),则此方法会返回 null
。
// Gets a cache that is specific to the current document containing the script
const cache = CacheService.getDocumentCache();
返回
Cache
- 文档缓存实例,如果没有包含文档,则为 null
getScriptCache()
获取作用域限定为脚本的缓存实例。脚本缓存对脚本的所有用户都是通用的。您可以使用这些 Cookie 存储不特定于当前用户的信息。
// 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
- 用户缓存实例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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"]]