Class Group

群组

访问和修改电子表格群组。群组是连续行或列区间之间的关联,可以作为一个单元展开或收起,以隐藏/显示行或列。 每个群组在群组之前或之后的行或列(取决于设置)上都有一个 控制切换开关,可以展开或收起整个群组。

群组的 深度是指群组的嵌套位置以及包含该群组的较大群组的数量。群组的 收起状态是指在展开父群组后,群组应保持收起还是展开状态。此外,在收起或展开群组时,群组中的行或列会被隐藏或设置为可见,但无论收起状态如何,都可以隐藏或显示单个行或列。

方法

方法返回值类型简介
collapse()Group收起此群组。
expand()Group展开此群组。
getControlIndex()Integer返回此群组的控制切换开关索引。
getDepth()Integer返回此群组的深度。
getRange()Range返回此群组存在的范围。
isCollapsed()Boolean如果此群组已收起,则返回 true
remove()void从工作表中移除此群组,并将 range 的群组深度减 1。

详细文档

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

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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

另请参阅