Class SpreadsheetApp

SpreadsheetApp

访问和创建 Google 表格文件。此类是电子表格服务的父类。

属性

属性类型说明
AutoFillSeriesAutoFillSeries用于计算自动填充值的系列类型的枚举。
BandingThemeBandingTheme枚举可能的条带主题。
BooleanCriteriaBooleanCriteria条件格式布尔值条件的枚举。
BorderStyleBorderStyle用于在 Range 上设置边框的有效样式的枚举。
ColorTypeColorType枚举可能的颜色类型。
CopyPasteTypeCopyPasteType枚举可能的粘贴类型。
DataExecutionErrorCodeDataExecutionErrorCode枚举可能的数据执行错误代码。
DataExecutionStateDataExecutionState枚举可能的数据执行状态。
DataSourceParameterTypeDataSourceParameterType可能的数据源参数类型的枚举。
DataSourceRefreshScopeDataSourceRefreshScope枚举可能的数据源刷新范围。
DataSourceTypeDataSourceType枚举可能的数据源类型。
DataValidationCriteriaDataValidationCriteria一个枚举,表示可对范围设置的数据验证条件。
DateTimeGroupingRuleTypeDateTimeGroupingRuleType日期时间分组规则的枚举。
DeveloperMetadataLocationTypeDeveloperMetadataLocationType枚举的开发者元数据位置类型。
DeveloperMetadataVisibilityDeveloperMetadataVisibility枚举可能的开发者元数据可见性。
DimensionDimension枚举电子表格的可能尺寸。
DirectionDirection使用箭头在电子表格中移动的可能方向的枚举 键。
FrequencyTypeFrequencyType可能的频率类型的枚举。
GroupControlTogglePositionGroupControlTogglePosition一个枚举,表示组控制切换开关可能处于的位置。
InterpolationTypeInterpolationType条件格式渐变插值类型的枚举。
PivotTableSummarizeFunctionPivotTableSummarizeFunction一个枚举函数,可用于汇总数据透视表中的值。
PivotValueDisplayTypePivotValueDisplayType枚举数据透视值的显示方式。
ProtectionTypeProtectionType一个枚举,表示电子表格中可以保护其免遭编辑的部分。
RecalculationIntervalRecalculationInterval枚举可用于重新计算电子表格的可能区间。
RelativeDateRelativeDate一个相对日期选项的枚举,用于计算要在基于日期的 BooleanCriteria 中使用的值。
SheetTypeSheetType一个枚举,用于枚举电子表格中可以包含的不同类型的工作表。
SortOrderSortOrder排序顺序的枚举。
TextDirectionTextDirection一个枚举的有效文本路线。
TextToColumnsDelimiterTextToColumnsDelimiter枚举用于将文本分列的预设分隔符。
ThemeColorTypeThemeColorType枚举可能的主题颜色类型。
ValueTypeValueType电子表格服务的 Range 类中由 Range.getValue()Range.getValues() 返回的值类型的枚举。枚举值 下列是对 NumberBooleanDateString 的补充。
WrapStrategyWrapStrategy用于封装单元格的策略的枚举。

方法

方法返回类型简介
create(name)Spreadsheet使用指定名称创建新电子表格。
create(name, rows, columns)Spreadsheet使用给定名称和指定的行数和列数创建新电子表格。
enableAllDataSourcesExecution()void为所有类型的数据源开启数据执行功能。
enableBigQueryExecution()void为 BigQuery 数据源开启数据执行功能。
flush()void应用所有待处理的电子表格更改。
getActive()Spreadsheet返回当前处于活动状态的电子表格,如果没有电子表格,则返回 null
getActiveRange()Range返回当前工作表中的所选范围,如果没有有效范围,则返回 null
getActiveRangeList()RangeList返回当前工作表中的有效范围列表;如果没有范围,则返回 null 已选择。
getActiveSheet()Sheet获取电子表格中的活动工作表。
getActiveSpreadsheet()Spreadsheet返回当前处于活动状态的电子表格,如果没有电子表格,则返回 null
getCurrentCell()Range返回在 活动工作表;如果没有当前单元格,则使用 null
getSelection()Selection返回电子表格中的当前 Selection
getUi()Ui返回电子表格界面环境的实例,通过该实例,脚本可以 以便添加菜单、对话框和边栏等功能。
newCellImage()CellImageBuilderCellImage 创建构建器。
newColor()ColorBuilderColor 创建构建器。
newConditionalFormatRule()ConditionalFormatRuleBuilder为条件格式规则创建构建器。
newDataSourceSpec()DataSourceSpecBuilderDataSourceSpec 创建构建器。
newDataValidation()DataValidationBuilder为数据验证规则创建构建器。
newFilterCriteria()FilterCriteriaBuilderFilterCriteria 创建构建器。
newRichTextValue()RichTextValueBuilder创建富文本值的构建器。
newTextStyle()TextStyleBuilder创建文本样式的构建器。
open(file)Spreadsheet打开与指定 File 对象对应的电子表格。
openById(id)Spreadsheet打开具有指定 ID 的电子表格。
openByUrl(url)Spreadsheet使用指定网址打开电子表格。
setActiveRange(range)Range将指定范围设置为 active range,并将顶部范围设为 范围内的左侧单元格作为 current cell
setActiveRangeList(rangeList)RangeList将指定的范围列表设置为 active ranges
setActiveSheet(sheet)Sheet设置电子表格中的活动工作表。
setActiveSheet(sheet, restoreSelection)Sheet设置电子表格中的活动工作表,并提供用于恢复最近选择的选项 。
setActiveSpreadsheet(newActiveSpreadsheet)void设置活动电子表格。
setCurrentCell(cell)Range将指定单元格设为 current cell

详细文档

create(name)

使用指定名称创建新电子表格。

// The code below creates a new spreadsheet "Finances" and logs the URL for it
var ssNew = SpreadsheetApp.create("Finances");
Logger.log(ssNew.getUrl());

参数

名称类型说明
nameString电子表格的名称。

返回

Spreadsheet - 新电子表格

授权

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

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

create(name, rows, columns)

使用给定名称和指定的行数和列数创建新电子表格。

// The code below creates a new spreadsheet "Finances" with 50 rows and 5 columns and logs the
// URL for it
var ssNew = SpreadsheetApp.create("Finances", 50, 5);
Logger.log(ssNew.getUrl());

参数

名称类型说明
nameString电子表格的名称。
rowsInteger电子表格的行数。
columnsInteger电子表格的列数。

返回

Spreadsheet - 新电子表格

授权

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

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

enableAllDataSourcesExecution()

为所有类型的数据源开启数据执行功能。

如果数据源类型未开启,数据执行会抛出异常。使用此方法 为所有数据源类型开启数据执行功能。

// Turns data execution on for all types of data sources.
SpreadsheetApp.enableAllDataSourcesExecution();

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets the first data source sheet in the spreadsheet and refreshes the data.
ss.getDataSourceSheets()[0].refreshData();

授权

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

  • https://www.googleapis.com/auth/bigquery.readonly

enableBigQueryExecution()

为 BigQuery 数据源开启数据执行功能。

如果未开启 BigQuery 数据源的数据执行功能,系统会抛出异常。

// Turns data execution on for BigQuery data sources.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets the first data source sheet in the spreadsheet and refreshes the BigQuery data.
ss.getDataSourceSheets()[0].refreshData();

授权

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

  • https://www.googleapis.com/auth/bigquery.readonly

flush()

应用所有待处理的电子表格更改。

为提高性能,电子表格操作有时会捆绑在一起,例如, 对 Range.getValue() 进行多次调用。不过,有时您可能需要 更改会立即生效,例如,在脚本执行时显示用户数据。

// The code below changes the background color of cells A1 and B1 twenty times. You should be
// able to see the updates live in the spreadsheet. If flush() is not called, the updates may
// be applied live or may all be applied at once when the script completes.
function colors() {
  var sheet = SpreadsheetApp.getActiveSheet();
  for (var i = 0; i < 20; i++) {
    if ((i % 2) == 0) {
      sheet.getRange('A1').setBackground('green');
      sheet.getRange('B1').setBackground('red');
    } else {
      sheet.getRange('A1').setBackground('red');
      sheet.getRange('B1').setBackground('green');
    }
    SpreadsheetApp.flush();
  }
}

getActive()

返回当前处于活动状态的电子表格,如果没有电子表格,则返回 null

在电子表格环境中运行的函数可获取对 相应的电子表格对象。

// The code below logs the URL for the active spreadsheet.
Logger.log(SpreadsheetApp.getActive().getUrl());

返回

Spreadsheet - 有效的电子表格对象

授权

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

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

getActiveRange()

返回当前工作表中的所选范围,如果没有有效范围,则返回 null。如果 选择了多个范围 此方法仅返回最后选择的范围。

这通常是指用户在活动工作表中选择的范围,但在自定义的工作表中 函数时,表示正被主动重新计算的单元格。

// The code below logs the background color for the active range.
var colorObject = SpreadsheetApp.getActiveRange().getBackgroundObject();
// Assume the color has ColorType.RGB.
Logger.log(colorObject.asRgbColor().asHexString());

返回

Range - 有效范围。

授权

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

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

getActiveRangeList()

返回当前工作表中的有效范围列表;如果没有范围,则返回 null 已选择。包含当前突出显示的单元格的活动范围位于列表的最后。

如果选择一个范围,则其行为类似于 getActiveRange() 调用。

// Returns the list of active ranges.
var rangeList = SpreadsheetApp.getActiveRangeList();

返回

RangeList - 活跃范围列表

授权

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

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

getActiveSheet()

获取电子表格中的活动工作表。

电子表格中的活动工作表是指当前显示在电子表格中的工作表。 界面。

// The code below logs the name of the active sheet.
Logger.log(SpreadsheetApp.getActiveSheet().getName());

返回

Sheet - 有效的工作表对象

授权

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

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

getActiveSpreadsheet()

返回当前处于活动状态的电子表格,如果没有电子表格,则返回 null

在电子表格环境中运行的函数可获取对 相应的 Spreadsheet 对象。

// The code below logs the URL for the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getUrl());

返回

Spreadsheet - 有效的电子表格对象

授权

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

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

getCurrentCell()

返回在 活动工作表;如果没有当前单元格,则使用 null

// Returns the current highlighted cell in the one of the active ranges.
var currentCell = SpreadsheetApp.getCurrentCell();

返回

Range - 当前单元格

授权

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

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

getSelection()

返回电子表格中的当前 Selection

var selection = SpreadsheetApp.getSelection();
var currentCell = selection.getCurrentCell();

返回

Selection - 当前选择

授权

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

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

getUi()

返回电子表格界面环境的实例,通过该实例,脚本可以 以便添加菜单、对话框和边栏等功能。脚本只能与 打开的电子表格的当前实例,并且仅在该脚本绑定到该电子表格的情况下执行。如需了解详情,请参阅 菜单对话框和边栏指南。

// Add a custom menu to the active spreadsheet, including a separator and a sub-menu.
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createMenu('My Menu')
      .addItem('My menu item', 'myFunction')
      .addSeparator()
      .addSubMenu(SpreadsheetApp.getUi().createMenu('My sub-menu')
          .addItem('One sub-menu item', 'mySecondFunction')
          .addItem('Another sub-menu item', 'myThirdFunction'))
      .addToUi();
}

返回

Ui - 此电子表格界面环境的实例


newCellImage()

CellImage 创建构建器。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets cell A1 on Sheet1.
const range = sheet.getRange('A1');

// Builds an image using a source URL.
const cellImage = SpreadsheetApp.newCellImage()
  .setSourceUrl('https://www.gstatic.com/images/branding/productlogos/apps_script/v10/web-64dp/logo_apps_script_color_1x_web_64dp.png')
  .build();

// Sets the image in cell A1.
range.setValue(cellImage);

返回

CellImageBuilder - 新的构建器。


newColor()

Color 创建构建器。

var rgbColor = SpreadsheetApp.newColor().setRgbColor("#FF0000").build();

返回

ColorBuilder - 新的构建器。


newConditionalFormatRule()

为条件格式规则创建构建器。

// Adds a conditional format rule to a sheet that causes all cells in range A1:B3 to turn red
// if they contain a number between 1 and 10.
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getRange('A1:B3');
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build()
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

返回

ConditionalFormatRuleBuilder - 新的构建器。


newDataSourceSpec()

DataSourceSpec 创建构建器。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Enables BigQuery.
SpreadsheetApp.enableBigQueryExecution();

// Builds a data source specification.
// TODO (developer): Update the project ID to your own Google Cloud project ID.
const dataSourceSpec = SpreadsheetApp.newDataSourceSpec()
  .asBigQuery()
  .setProjectId('project-id-1')
  .setTableProjectId('bigquery-public-data')
  .setDatasetId('ncaa_basketball')
  .setTableId('mbb_historical_teams_games')
  .build();

// Adds the data source and its data to the spreadsheet.
ss.insertDataSourceSheet(dataSourceSpec);

返回

DataSourceSpecBuilder - 新的构建器。


newDataValidation()

为数据验证规则创建构建器。

var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = SpreadsheetApp.newDataValidation()
    .requireNumberBetween(1, 100)
    .setAllowInvalid(false)
    .setHelpText('Number must be between 1 and 100.')
    .build();
cell.setDataValidation(rule);

返回

DataValidationBuilder - 新的构建器。


newFilterCriteria()

FilterCriteria 创建构建器。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Sets the range to A1:D20.
const range = sheet.getRange('A1:D20');

// Creates a filter and applies it to the specified range.
range.createFilter();

// Gets the current filter for the range and creates filter criteria that only shows cells
// that aren't empty.
const filter = range.getFilter();
const criteria = SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();

// Sets the criteria to  column C.
filter.setColumnFilterCriteria(3, criteria);

返回

FilterCriteriaBuilder - 新的构建器


newRichTextValue()

创建富文本值的构建器。

// Sets cell A1 to have the text "Hello world", with "Hello" bolded.
var cell = SpreadsheetApp.getActive().getRange('A1');
var bold = SpreadsheetApp.newTextStyle().setBold(true).build();
var value = SpreadsheetApp.newRichTextValue()
    .setText("Hello world")
    .setTextStyle(0, 5, bold)
    .build();
cell.setRichTextValue(value);

返回

RichTextValueBuilder - 新的构建器。


newTextStyle()

创建文本样式的构建器。

// Sets range A1:B3 to have red, size 22, bolded, underlined text.
var range = SpreadsheetApp.getActive().getRange('A1:B3');
var style = SpreadsheetApp.newTextStyle()
    .setForegroundColor("red")
    .setFontSize(22)
    .setBold(true)
    .setUnderline(true)
    .build();
range.setTextStyle(style);

返回

TextStyleBuilder - 新的构建器。


open(file)

打开与指定 File 对象对应的电子表格。

// Get any starred spreadsheets from Google Drive, then open the spreadsheets and log the name
// of the first sheet within each spreadsheet.
var files = DriveApp.searchFiles(
    'starred = true and mimeType = "' + MimeType.GOOGLE_SHEETS + '"');
while (files.hasNext()) {
  var spreadsheet = SpreadsheetApp.open(files.next());
  var sheet = spreadsheet.getSheets()[0];
  Logger.log(sheet.getName());
}

参数

名称类型说明
fileFile要打开的文件。

返回

Spreadsheet - 电子表格

授权

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

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

openById(id)

打开具有指定 ID 的电子表格。电子表格 ID 可从相应网址中提取。对于 例如,网址 https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 中的电子表格 ID 为“abc1234567”。

// The code below opens a spreadsheet using its ID and logs the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Logger.log(ss.getName());

参数

名称类型说明
idString电子表格的唯一标识符。

返回

Spreadsheet - 具有指定 ID 的电子表格对象

授权

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

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

openByUrl(url)

使用指定网址打开电子表格。如果网址不存在,则会抛出脚本异常 或者用户无权访问这些数据。

// Opens a spreadsheet by its URL and logs its name.
// Note that the spreadsheet doesn't physically open on the client side.
// It opens on the server only (for modification by the script).
var ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc1234567/edit');
console.log(ss.getName());

参数

名称类型说明
urlString电子表格的网址。

返回

Spreadsheet - 具有指定网址的电子表格对象。

授权

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

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

setActiveRange(range)

将指定范围设置为 active range,并将指定范围设为顶部 范围内的左侧单元格作为 current cell

电子表格界面会显示包含所选范围的工作表,并选择相应单元格 所选范围内的值

// The code below sets range C1:D4 in the first sheet as the active range.
var range = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4');
SpreadsheetApp.setActiveRange(range);

var selection = SpreadsheetApp.getSelection();
// Current cell: C1
var currentCell = selection.getCurrentCell();
// Active Range: C1:D4
var activeRange = selection.getActiveRange();

参数

名称类型说明
rangeRange要设为有效范围的范围。

返回

Range - 新的有效范围


setActiveRangeList(rangeList)

将指定的范围列表设置为 active ranges。最后一个 列表中的范围已设置为 active range

// The code below sets ranges [D4, B2:C4] in the active sheet as the active ranges.
var rangeList = SpreadsheetApp.getActiveSheet().getRanges(['D4', 'B2:C4']);
SpreadsheetApp.setActiveRangeList(rangeList);

var selection = SpreadsheetApp.getSelection();
// Current cell: B2
var currentCell = selection.getCurrentCell();
// Active range: B2:C4
var activeRange = selection.getActiveRange();
// Active range list: [D4, B2:C4]
var activeRangeList = selection.getActiveRangeList();

参数

名称类型说明
rangeListRangeList要选择的范围列表。

返回

RangeList - 新选择的范围列表

授权

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

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

setActiveSheet(sheet)

设置电子表格中的活动工作表。Google 表格界面会显示所选的工作表,除非 该工作表属于其他电子表格。

// The code below makes the 2nd sheet active in the active spreadsheet.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);

参数

名称类型说明
sheetSheet新的活动工作表。

返回

Sheet - 已成为新的有效工作表的工作表


setActiveSheet(sheet, restoreSelection)

设置电子表格中的活动工作表,并提供用于恢复最近选择的选项 。除非所选工作表属于 其他电子表格。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var firstSheet = spreadsheet.getSheets()[0];
var secondSheet = spreadsheet.getSheets()[1];
// Set the first sheet as the active sheet and select the range D4:F4.
spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate();

// Switch to the second sheet to do some work.
spreadsheet.setActiveSheet(secondSheet);
// Switch back to first sheet, and restore its selection.
spreadsheet.setActiveSheet(firstSheet, true);

// The selection of first sheet is restored, and it logs D4:F4
var range = spreadsheet.getActiveSheet().getSelection().getActiveRange();
Logger.log(range.getA1Notation());

参数

名称类型说明
sheetSheet新的活动工作表。
restoreSelectionBoolean如果为 true,则最近选择的新活动工作表 在新工作表中再次选中时如果值为 false,则新建工作表 会变为有效状态,而不会改变当前选择。

返回

Sheet - 新的有效工作表

授权

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

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

setActiveSpreadsheet(newActiveSpreadsheet)

设置活动电子表格。

// The code below makes the spreadsheet with key "1234567890" the active spreadsheet
var ss = SpreadsheetApp.openById("1234567890");
SpreadsheetApp.setActiveSpreadsheet(ss);

参数

名称类型说明
newActiveSpreadsheetSpreadsheet要成为活动电子表格的电子表格。

setCurrentCell(cell)

将指定单元格设为 current cell

如果指定的单元格包含在已选中的范围中,则该范围会变为 当前单元格作为当前单元格中的单元格

如果指定的单元格不在任何选定范围内,则现有选择会应用于 移除后,该单元格会成为当前单元格和活动范围。

注意:指定的 Range 必须包含一个单元格,否则此方法 会抛出异常。

// The code below sets the cell B5 in the first sheet as the current cell.
var cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5');
SpreadsheetApp.setCurrentCell(cell);

var selection = SpreadsheetApp.getSelection();
// Current cell: B5
var currentCell = selection.getCurrentCell();

参数

名称类型说明
cellRange要设置为当前单元格的单元格。

返回

Range - 新设置的当前单元格

授权

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

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