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,否則傳回 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

另請參閱