Class 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 数据源启用数据执行。
enableLookerExecution()void为 Looker 数据源启用数据执行。
flush()void应用所有待处理的电子表格更改。
getActive()Spreadsheet返回当前活跃的电子表格;如果没有,则返回 null
getActiveRange()Range返回活动工作表中的所选范围,如果没有活动范围,则返回 null
getActiveRangeList()RangeList|null返回活动工作表中的活动范围列表,如果没有选择任何范围,则返回 null
getActiveSheet()Sheet获取电子表格中的活动工作表。
getActiveSpreadsheet()Spreadsheet返回当前活跃的电子表格;如果没有,则返回 null
getCurrentCell()Range|null返回活动工作表中某个活动范围内的当前(突出显示)所选单元格;如果没有当前单元格,则返回 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
const 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
const 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

enableLookerExecution()

为 Looker 数据源启用数据执行。

如果未开启 Looker 数据源的数据执行,则会抛出异常。

// Turns data execution on for Looker data sources.
SpreadsheetApp.enableLookerExecution();

// 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
// associated Looker data.
ss.getDataSourceSheets()[0].refreshData();

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() {
  const sheet = SpreadsheetApp.getActiveSheet();
  for (let 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.
const 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.
const rangeList = SpreadsheetApp.getActiveRangeList();

返回

RangeList|null - 活跃范围的列表

授权

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

  • 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.
const currentCell = SpreadsheetApp.getCurrentCell();

返回

Range|null - 当前单元格

授权

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

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

getSelection()

返回电子表格中的当前 Selection

const selection = SpreadsheetApp.getSelection();
const 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 创建构建器。

const 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.
const sheet = SpreadsheetApp.getActive().getActiveSheet();
const range = sheet.getRange('A1:B3');
const rule = SpreadsheetApp.newConditionalFormatRule()
                 .whenNumberBetween(1, 10)
                 .setBackground('#FF0000')
                 .setRanges([range])
                 .build();
const 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()

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

const cell = SpreadsheetApp.getActive().getRange('A1');
const 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.
const cell = SpreadsheetApp.getActive().getRange('A1');
const bold = SpreadsheetApp.newTextStyle().setBold(true).build();
const 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.
const range = SpreadsheetApp.getActive().getRange('A1:B3');
const 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.
const files = DriveApp.searchFiles(
    `starred = true and mimeType = "${MimeType.GOOGLE_SHEETS}"`,
);
while (files.hasNext()) {
  const spreadsheet = SpreadsheetApp.open(files.next());
  const 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).
const 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).
const 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.
const range =
    SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4');
SpreadsheetApp.setActiveRange(range);

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

参数

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

返回

Range - 新的有效 Range


setActiveRangeList(rangeList)

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

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

const selection = SpreadsheetApp.getSelection();
// Current cell: B2
const currentCell = selection.getCurrentCell();
// Active range: B2:C4
const activeRange = selection.getActiveRange();
// Active range list: [D4, B2:C4]
const 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.
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);

参数

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

返回

Sheet - 已成为新的活动工作表的工作表


setActiveSheet(sheet, restoreSelection)

设置电子表格中的活动工作表,并可以选择恢复该工作表中的最新选择。Google 表格界面会显示所选工作表,除非该工作表属于其他电子表格。

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const firstSheet = spreadsheet.getSheets()[0];
const 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
const 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
const 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.
const cell =
    SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5');
SpreadsheetApp.setCurrentCell(cell);

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

参数

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

返回

Range - 新设置的当前单元格

授权

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

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