Class User
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用户
与 Google 云端硬盘中的文件关联的用户。您可以通过 File.getEditors()
、Folder.getViewers()
和其他方法访问用户。
// Log the email address of all users who have edit access to a file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const editors = file.getEditors();
for (let i = 0; i < editors.length; i++) {
Logger.log(editors[i].getEmail());
}
详细文档
getDomain()
获取与用户账号关联的域名。
// Log the domain names associated with all users who have edit access to a
// file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const editors = file.getEditors();
for (let i = 0; i < editors.length; i++) {
Logger.log(editors[i].getDomain());
}
返回
String
- 与用户账号关联的域名
getEmail()
获取用户的电子邮件地址。只有在以下情况下,系统才会提供用户的电子邮件地址:用户已选择在 Google+ 账号设置页面中分享该地址;或者,用户与运行脚本的用户属于同一网域,并且网域管理员已允许网域中的所有用户查看其他用户的电子邮件地址。
// Log the email address of all users who have edit access to a file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const editors = file.getEditors();
for (let i = 0; i < editors.length; i++) {
Logger.log(editors[i].getEmail());
}
返回
String
- 用户的电子邮件地址,如果没有电子邮件地址,则为空字符串
getName()
获取用户的姓名。如果用户的姓名不可用,此方法会返回 null
。
// Log the names of all users who have edit access to a file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const editors = file.getEditors();
for (let i = 0; i < editors.length; i++) {
Logger.log(editors[i].getName());
}
返回
String
- 用户的姓名,如果没有姓名,则为 null
getPhotoUrl()
获取用户照片的网址。如果用户没有照片,此方法会返回 null
。
// Log the URLs for the photos of all users who have edit access to a file.
const file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
const editors = file.getEditors();
for (let i = 0; i < editors.length; i++) {
Logger.log(editors[i].getPhotoUrl());
}
返回
String
- 用户照片的网址,如果没有照片,则为 null
已弃用的方法
getUserLoginId()
已弃用。自 2013 年 6 月 24 日起,已被 getEmail()
替换。
获取用户的电子邮件地址。
// Log the email address of the person running the script.
Logger.log(Session.getActiveUser().getUserLoginId());
返回
String
- 用户的电子邮件地址。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eA User in Google Drive represents an individual associated with a file or folder, accessible through methods like \u003ccode\u003egetEditors()\u003c/code\u003e and \u003ccode\u003egetViewers()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve user information such as their domain, email address, name, and photo URL using dedicated methods of the \u003ccode\u003eUser\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetUserLoginId()\u003c/code\u003e method is deprecated and has been replaced by \u003ccode\u003egetEmail()\u003c/code\u003e for accessing a user's email address.\u003c/p\u003e\n"],["\u003cp\u003eUser email addresses might not be available if privacy settings restrict access or if domain-wide sharing isn't enabled.\u003c/p\u003e\n"],["\u003cp\u003eUser names and photo URLs may return \u003ccode\u003enull\u003c/code\u003e if they are not available or have not been set by the user.\u003c/p\u003e\n"]]],[],null,["# Class User\n\nUser\n\nA user associated with a file in Google Drive. Users can be accessed from [File.getEditors()](/apps-script/reference/drive/file#getEditors()), [Folder.getViewers()](/apps-script/reference/drive/folder#getViewers()), and other methods.\n\n```javascript\n// Log the email address of all users who have edit access to a file.\nconst file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst editors = file.getEditors();\nfor (let i = 0; i \u003c editors.length; i++) {\n Logger.log(editors[i].getEmail());\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------|-------------|----------------------------------------------------------|\n| [getDomain()](#getDomain()) | `String` | Gets the domain name associated with the user's account. |\n| [getEmail()](#getEmail()) | `String` | Gets the user's email address. |\n| [getName()](#getName()) | `String` | Gets the user's name. |\n| [getPhotoUrl()](#getPhotoUrl()) | `String` | Gets the URL for the user's photo. |\n\n### Deprecated methods\n\n| Method | Return type | Brief description |\n|---------------------------------------|-------------|--------------------------------|\n| [getUserLoginId()](#getUserLoginId()) | `String` | Gets the user's email address. |\n\nDetailed documentation\n----------------------\n\n### `get``Domain()`\n\nGets the domain name associated with the user's account.\n\n```javascript\n// Log the domain names associated with all users who have edit access to a\n// file.\nconst file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst editors = file.getEditors();\nfor (let i = 0; i \u003c editors.length; i++) {\n Logger.log(editors[i].getDomain());\n}\n```\n\n#### Return\n\n\n`String` --- the domain name associated with the user's account\n\n*** ** * ** ***\n\n### `get``Email()`\n\nGets the user's email address. The user's email address is only available if the user has\nchosen to share the address from the Google+ account settings page, or if the user belongs to\nthe same domain as the user running the script and the domain administrator has allowed all\nusers within the domain to see other users' email addresses.\n\n```javascript\n// Log the email address of all users who have edit access to a file.\nconst file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst editors = file.getEditors();\nfor (let i = 0; i \u003c editors.length; i++) {\n Logger.log(editors[i].getEmail());\n}\n```\n\n#### Return\n\n\n`String` --- the user's email's address, or a blank string if the email address is not available\n\n*** ** * ** ***\n\n### `get``Name()`\n\nGets the user's name. This method returns `null` if the user's name is not available.\n\n```javascript\n// Log the names of all users who have edit access to a file.\nconst file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst editors = file.getEditors();\nfor (let i = 0; i \u003c editors.length; i++) {\n Logger.log(editors[i].getName());\n}\n```\n\n#### Return\n\n\n`String` --- the user's name, or `null` if the name is not available\n\n*** ** * ** ***\n\n### `get``Photo``Url()`\n\nGets the URL for the user's photo. This method returns `null` if the user's photo is not\navailable.\n\n```javascript\n// Log the URLs for the photos of all users who have edit access to a file.\nconst file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');\nconst editors = file.getEditors();\nfor (let i = 0; i \u003c editors.length; i++) {\n Logger.log(editors[i].getPhotoUrl());\n}\n```\n\n#### Return\n\n\n`String` --- the URL for the user's photo, or `null` if the photo is not available\n\nDeprecated methods\n------------------\n\n### `get``User``Login``Id()`\n\n\n**Deprecated.** As of June 24, 2013, replaced by [getEmail()](#getEmail()).\n\nGets the user's email address.\n\n```javascript\n// Log the email address of the person running the script.\nLogger.log(Session.getActiveUser().getUserLoginId());\n```\n\n#### Return\n\n\n`String` --- The user's email's address."]]