Class GroupsApp
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
“群组”应用
此类提供对 Google 群组信息的访问权限。它可用于查询群组的电子邮件地址或用户是直接成员的群组列表等信息。
以下示例展示了当前用户是多少个群组的成员:
const groups = GroupsApp.getGroups();
Logger.log(`You belong to ${groups.length} groups.`);
详细文档
getGroupByEmail(email)
检索具有指定电子邮件地址的群组。如果群组不存在或您无权查看,则会抛出异常。
以下示例会按电子邮件地址获取群组,并输出当前用户是否为成员。在运行之前,请将示例电子邮件地址替换为真实群组的电子邮件地址。
const group = GroupsApp.getGroupByEmail('example@googlegroups.com');
const currentUser = Session.getActiveUser();
if (group.hasUser(currentUser)) {
Logger.log('You are a member of this group.');
} else {
Logger.log('You are not a member of this group.');
}
参数
名称 | 类型 | 说明 |
email | String | 要检索的群组的电子邮件地址。 |
返回
Group
- 具有指定电子邮件地址的群组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/groups
getGroups()
检索您是直接成员(或待处理成员)的所有群组。如果您不属于任何群组,则此列表为空。如果群组不存在或您无权查看,则会抛出异常。
以下示例展示了如何输出用户所属的每个群组的电子邮件地址:
function showMyGroups() {
const groups = GroupsApp.getGroups();
let str = `You are in ${groups.length} groups: `;
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
str = `${str + group.getEmail()} `;
}
Logger.log(str);
}
请注意,如果您是群组 B 的成员,而群组 B 本身又是群组 A 的成员,那么您就
间接订阅了群组 A。即使您会收到发送到“父级”群组 A 的邮件的副本,但您实际上并未订阅该群组。
您可以使用 Group.getRole(email)
确定您是返回的群组的现有成员还是待处理成员。
返回
Group[]
- 用户直接成员所属的群组列表。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/groups
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eGroupsApp\u003c/code\u003e class in Apps Script allows you to access and manage Google Groups information, such as retrieving group details and membership status.\u003c/p\u003e\n"],["\u003cp\u003eYou can use \u003ccode\u003egetGroupByEmail()\u003c/code\u003e to retrieve a specific group by its email address and check if the current user is a member.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetGroups()\u003c/code\u003e retrieves all groups where the user is a direct or pending member, enabling actions like listing group email addresses.\u003c/p\u003e\n"],["\u003cp\u003eBoth methods require authorization with the \u003ccode\u003ehttps://www.googleapis.com/auth/groups\u003c/code\u003e scope for accessing Google Groups data.\u003c/p\u003e\n"]]],[],null,["# Class GroupsApp\n\nGroupsApp\n\nThis class provides access to Google Groups information. It can be used to query information such\nas a group's email address, or the list of groups in which the user is a direct member.\n\nHere's an example that shows how many groups the current user is a member of:\n\n```javascript\nconst groups = GroupsApp.getGroups();\nLogger.log(`You belong to ${groups.length} groups.`);\n``` \n\n### Properties\n\n| Property | Type | Description |\n|----------|--------------------------------------------|-------------|\n| `Role` | [Role](/apps-script/reference/groups/role) | |\n\n### Methods\n\n| Method | Return type | Brief description |\n|----------------------------------------------------|------------------------------------------------|----------------------------------------------------------------------------------|\n| [getGroupByEmail(email)](#getGroupByEmail(String)) | [Group](/apps-script/reference/groups/group) | Retrieves the group having the specified email address. |\n| [getGroups()](#getGroups()) | [Group[]](/apps-script/reference/groups/group) | Retrieves all the groups of which you are a direct member (or a pending member). |\n\nDetailed documentation\n----------------------\n\n### `get``Group``By``Email(email)`\n\nRetrieves the group having the specified email address. Throws an exception if the group does\nnot exist or if you do not have permission to see it.\n\nHere is an example that gets a group by its email address and outputs whether the current\nuser is a member. Before running, replace the sample email address with a real group's email.\n\n```javascript\nconst group = GroupsApp.getGroupByEmail('example@googlegroups.com');\nconst currentUser = Session.getActiveUser();\nif (group.hasUser(currentUser)) {\n Logger.log('You are a member of this group.');\n} else {\n Logger.log('You are not a member of this group.');\n}\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|---------------------------------------------|\n| `email` | `String` | The email address of the group to retrieve. |\n\n#### Return\n\n\n[Group](/apps-script/reference/groups/group) --- The group with the specified email address.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/groups`\n\n*** ** * ** ***\n\n### `get``Groups()`\n\nRetrieves all the groups of which you are a direct member (or a pending member). This is an\nempty list if you are not in any groups. Throws an exception if the group does not exist or if\nyou do not have permission to see it.\n\nHere's an example of how to print the email address for every group the user belongs to:\n\n```javascript\nfunction showMyGroups() {\n const groups = GroupsApp.getGroups();\n let str = `You are in ${groups.length} groups: `;\n for (let i = 0; i \u003c groups.length; i++) {\n const group = groups[i];\n str = `${str + group.getEmail()} `;\n }\n Logger.log(str);\n}\n```\nNote that if you are a member of a group, B, which is itself a member of another group, A, then you are *indirectly* subscribed to group A. Even though you receive copies of messages sent to the \"parent\" group A, you are not actually subscribed to that group.\n\nYou can use [Group.getRole(email)](/apps-script/reference/groups/group#getRole(String)) to determine if you are an existing or pending\nmember of the returned groups.\n\n#### Return\n\n\n[Group[]](/apps-script/reference/groups/group) --- The list of groups of which the user is a direct member.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/groups`"]]