Class Selection

选择内容

访问活动工作表中的当前活动选择内容。选择内容是指用户在工作表中突出显示的一组单元格,这些单元格可以是相邻的范围,也可以是不相邻的范围。选择内容中的一个单元格是 当前单元格,即用户当前关注的单元格。在 Google 表格界面中,当前单元格会以 较深的边框突出显示。

const activeSheet = SpreadsheetApp.getActiveSheet();
const rangeList = activeSheet.getRangeList(['A1:B4', 'D1:E4']);
rangeList.activate();

const selection = activeSheet.getSelection();
// Current Cell: D1
console.log(`Current Cell: ${selection.getCurrentCell().getA1Notation()}`);
// Active Range: D1:E4
console.log(`Active Range: ${selection.getActiveRange().getA1Notation()}`);
// Active Ranges: A1:B4, D1:E4
const ranges = selection.getActiveRangeList().getRanges();
for (let i = 0; i < ranges.length; i++) {
  console.log(`Active Ranges: ${ranges[i].getA1Notation()}`);
}
console.log(`Active Sheet: ${selection.getActiveSheet().getName()}`);

方法

方法返回值类型简介
getActiveRange()Range|null返回活动工作表中的所选范围;如果没有活动范围,则返回 null
getActiveRangeList()RangeList|null返回活动工作表中的活动范围列表;如果没有活动范围,则返回 null
getActiveSheet()Sheet返回电子表格中的活动工作表。
getCurrentCell()Range|null返回在某个活动范围中选择的当前(突出显示)单元格;如果没有当前单元格,则返回 null
getNextDataRange(direction)Range|nullcurrent cellactive range 开始,沿给定方向移动,返回调整后的范围,其中范围的相应边缘已移动以覆盖 next data cell,同时仍覆盖当前单元格。

详细文档

getActiveRange()

返回活动工作表中的所选范围;如果没有活动范围,则返回 null。如果选择了多个范围,此方法只会返回最后选择的范围。

const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
const activeRange = selection.getActiveRange();

返回

Range|null - 活动范围。

授权

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getActiveRangeList()

返回活动工作表中的活动范围列表;如果没有活动范围,则返回 null

如果仅选择了一个范围,此方法的行为与调用 getActiveRange() 相同。

const sheet = SpreadsheetApp.getActiveSheet();
// Returns the list of active ranges.
const activeRangeList = sheet.getActiveRangeList();

返回

RangeList|null - 活动范围列表。

授权

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getActiveSheet()

返回电子表格中的活动工作表。

const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
const activeSheet = selection.getActiveSheet();

返回

Sheet - 电子表格中的活动工作表。

授权

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getCurrentCell()

返回在某个活动范围中选择的当前(突出显示)单元格;如果没有当前单元格,则返回 null

const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
// Returns the current highlighted cell in the one of the active ranges.
const currentCell = selection.getCurrentCell();

返回

Range|null - 当前单元格。

授权

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getNextDataRange(direction)

current cellactive range 开始,沿给定方向移动,返回调整后的范围,其中范围的相应边缘已移动以覆盖 next data cell,同时仍覆盖当前单元格。如果活动范围沿方向的 dimension 无界,则返回原始活动范围。如果没有当前单元格或活动范围,则返回 null。这相当于在编辑器中选择一个范围,然后按 Ctrl+Shift+[arrow key]

// Assume the active spreadsheet is blank.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0];

// Makes C3 the current cell and C3:E5 the active range.
sheet.getRange('C3:E5').activate();
// Logs 'C1:E3'
console.log(
    SpreadsheetApp.getSelection()
        .getNextDataRange(SpreadsheetApp.Direction.UP)
        .getA1Notation(),
);

参数

名称类型说明
directionDirection查找下一个数据区域边缘单元格的方向。

返回

Range|null - 包含数据单元格的调整后范围;如果没有选择内容,则返回 null

授权

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets