Class Group
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
群组
访问和修改电子表格群组。组是指一组连续的行或列,可以作为一个整体展开或收起,以隐藏/显示这些行或列。
每个组在紧邻该组之前或之后的行或列(具体取决于设置)中都有一个控制切换开关,可用于展开或收起整个组。
群组的深度是指群组的嵌套位置以及包含该群组的更大型群组的数量。群组的收起状态是指在展开父群组后,该群组应保持收起状态还是展开状态。此外,在折叠或展开群组时,系统会隐藏或显示群组中的行或列,但无论群组处于折叠状态还是展开状态,都可以隐藏或显示单个行或列。
详细文档
collapse()
收起此组。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
const range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Collapses this group.
group.collapse();
返回
Group
- 此群组,用于链式调用。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
expand()
展开此群组。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
const range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Expands this group.
group.expand();
返回
Group
- 此群组,用于链式调用。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getControlIndex()
返回相应组的控制切换开关索引。如果控制切换开关显示在组之前,则为范围之前的索引;否则为范围之后的索引。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.setRowGroupControlAfter(true);
const range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Returns 4
const controlIndex = group.getControlIndex();
返回
Integer
- 相应组的控制切换开关索引。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDepth()
返回此组的深度。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
const range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Returns 1 if the group is at depth 1.
const depth = group.getDepth();
返回
Integer
- 此组的深度。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRange()
返回此群组存在的时间范围。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
let range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(1, 1);
// Returns the range 2:3 if the group is over rows 2:3
range = group.getRange();
返回
Range
- 群组存在的范围。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isCollapsed()
如果相应群组已收起,则返回 true
。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
const range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Returns true if the group is collapsed.
const isCollapsed = group.isCollapsed();
返回
Boolean
- true
如果相应群组处于收起状态,则返回 false
;否则返回 Boolean
。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove()
从工作表中移除相应群组,使 range
的群组深度减少 1。这可能会修改其他群组。调用此方法后,相应群组对象将无法再使用。
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
let range = sheet.getRange('2:3');
range.shiftRowGroupDepth(1);
const group = sheet.getRowGroup(2, 1);
// Removes this group
range = group.remove();
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
另请参阅
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[[["\u003cp\u003eGroups in Google Sheets allow you to associate contiguous rows or columns, enabling them to be expanded or collapsed as a single unit to control visibility.\u003c/p\u003e\n"],["\u003cp\u003eEach group has a control toggle located before or after the group, which dictates whether the group is expanded or collapsed, affecting the visibility of its rows or columns.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eGroup\u003c/code\u003e class in Apps Script provides methods to programmatically manage groups, including collapsing, expanding, retrieving group properties, and removing groups.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eGroup\u003c/code\u003e methods like \u003ccode\u003ecollapse()\u003c/code\u003e, \u003ccode\u003eexpand()\u003c/code\u003e, \u003ccode\u003egetControlIndex()\u003c/code\u003e, \u003ccode\u003egetDepth()\u003c/code\u003e, \u003ccode\u003egetRange()\u003c/code\u003e, \u003ccode\u003eisCollapsed()\u003c/code\u003e, and \u003ccode\u003eremove()\u003c/code\u003e offer control over group behavior and properties within your scripts.\u003c/p\u003e\n"],["\u003cp\u003eUtilizing these methods allows for dynamic manipulation of spreadsheet groups, enhancing the interactivity and organization of your spreadsheet data through automation.\u003c/p\u003e\n"]]],[],null,["# Class Group\n\nGroup\n\nAccess and modify spreadsheet groups. Groups are an association between an interval of contiguous\nrows or columns that can be expanded or collapsed as a unit to hide/show the rows or columns.\nEach group has a *control toggle* on the row or column directly before or after the group\n(depending on settings) that can expand or collapse the group as a whole.\n\nThe *depth* of a group refers to the nested position of the group and how many larger\ngroups contain the group. The *collapsed state* of a group refers to whether the group\nshould remain collapsed or expanded after a parent group has been expanded. Additionally, at the\ntime that a group is collapsed or expanded, the rows or columns within the group are hidden or\nset visible, though individual rows or columns can be hidden or set visible irrespective of the\ncollapsed state. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------|---------------------------------------------------|-------------------------------------------------------------------------------------------------|\n| [collapse()](#collapse()) | [Group](#) | Collapses this group. |\n| [expand()](#expand()) | [Group](#) | Expands this group. |\n| [getControlIndex()](#getControlIndex()) | `Integer` | Returns the control toggle index of this group. |\n| [getDepth()](#getDepth()) | `Integer` | Returns the depth of this group. |\n| [getRange()](#getRange()) | [Range](/apps-script/reference/spreadsheet/range) | Returns the range over which this group exists. |\n| [isCollapsed()](#isCollapsed()) | `Boolean` | Returns `true` if this group is collapsed. |\n| [remove()](#remove()) | `void` | Removes this group from the sheet, reducing the group depth of the [range](#getRange()) by one. |\n\nDetailed documentation\n----------------------\n\n### `collapse()`\n\nCollapses this group.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nconst range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Collapses this group.\ngroup.collapse();\n```\n\n#### Return\n\n\n[Group](#) --- This group, for chaining.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `expand()`\n\nExpands this group.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nconst range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Expands this group.\ngroup.expand();\n```\n\n#### Return\n\n\n[Group](#) --- This group, for chaining.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Control``Index()`\n\nReturns the control toggle index of this group. This is the index just before the range when\nthe control toggle is shown before the group, or the index just after the range otherwise.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nsheet.setRowGroupControlAfter(true);\nconst range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Returns 4\nconst controlIndex = group.getControlIndex();\n```\n\n#### Return\n\n\n`Integer` --- The control toggle index of this group.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Depth()`\n\nReturns the depth of this group.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nconst range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Returns 1 if the group is at depth 1.\nconst depth = group.getDepth();\n```\n\n#### Return\n\n\n`Integer` --- The depth of this group.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Range()`\n\nReturns the range over which this group exists.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nlet range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(1, 1);\n\n// Returns the range 2:3 if the group is over rows 2:3\nrange = group.getRange();\n```\n\n#### Return\n\n\n[Range](/apps-script/reference/spreadsheet/range) --- The range over which the group exists.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `is``Collapsed()`\n\nReturns `true` if this group is collapsed.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nconst range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Returns true if the group is collapsed.\nconst isCollapsed = group.isCollapsed();\n```\n\n#### Return\n\n\n`Boolean` --- `true` If this group is collapsed; returns `false` otherwise.\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `remove()`\n\nRemoves this group from the sheet, reducing the group depth of the [range](#getRange()) by\none. This may modify other groups. After calling this, the group object becomes invalid to use.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];\nlet range = sheet.getRange('2:3');\nrange.shiftRowGroupDepth(1);\nconst group = sheet.getRowGroup(2, 1);\n\n// Removes this group\nrange = group.remove();\n```\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/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n#### See also\n\n- [Range.shiftRowGroupDepth(delta)](/apps-script/reference/spreadsheet/range#shiftRowGroupDepth(Integer))\n- [Range.shiftColumnGroupDepth(delta)](/apps-script/reference/spreadsheet/range#shiftColumnGroupDepth(Integer))"]]