访问和创建 Google 表格文件。此类是电子表格服务的父类。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Auto | Auto | 用于计算自动填充值的序列类型的枚举。 |
Banding | Banding | 可能出现的条带主题的枚举。 |
Boolean | Boolean | 条件格式布尔值条件的枚举。 |
Border | Border | 用于在 Range 上设置边框的有效样式的枚举。 |
Color | Color | 可能颜色类型的枚举。 |
Copy | Copy | 可能粘贴类型的枚举。 |
Data | Data | 可能的数据执行错误代码的枚举。 |
Data | Data | 可能的数据执行状态的枚举。 |
Data | Data | 可能的数据源参数类型的枚举。 |
Data | Data | 一个枚举,包含可能的数据源刷新范围。 |
Data | Data | 可能的数据源类型的枚举。 |
Data | Data | 一种枚举,表示可针对范围设置的数据验证条件。 |
Date | Date | 日期时间分组规则的枚举。 |
Developer | Developer | 可能存在的开发者元数据位置类型的枚举。 |
Developer | Developer | 一个枚举,包含可能的开发者元数据公开范围。 |
Dimension | Dimension | 一个枚举,包含电子表格的可能维度。 |
Direction | Direction | 一个枚举,用于表示使用箭头键在电子表格中移动时可能采用的方向。 |
Frequency | Frequency | 可能频次类型的枚举。 |
Group | Group | 群组控制切换开关可处于的位置的枚举。 |
Interpolation | Interpolation | 条件格式渐变插值类型的枚举。 |
Pivot | Pivot | 一种枚举,用于表示可用于汇总数据透视表中的值的函数。 |
Pivot | Pivot | 一种枚举,用于表示透视值的显示方式。 |
Protection | Protection | 一个枚举,表示可以防止修改的电子表格部分。 |
Recalculation | Recalculation | 可在电子表格重新计算中使用的可能间隔的枚举。 |
Relative | Relative | 用于计算基于日期的 Boolean 中所用值的相对日期选项的枚举。 |
Sheet | Sheet | 一种枚举,用于表示电子表格中可能存在的不同类型的工作表。 |
Sort | Sort | 一种排序顺序枚举。 |
Text | Text | 有效文字方向的枚举。 |
Text | Text | 用于将文本分列的预设分隔符的枚举。 |
Theme | Theme | 可能的主题颜色类型的枚举。 |
Value | Value | 一种枚举,用于表示电子表格服务的 Range 类中 Range.getValue() 和 Range.getValues() 返回的值类型。除了 Number、Boolean、Date 或 String 之外,还可使用以下枚举值。 |
Wrap | Wrap | 用于封装单元格的策略的枚举。 |
方法
详细文档
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());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 电子表格的名称。 |
返回
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());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 电子表格的名称。 |
rows | Integer | 电子表格的行数。 |
columns | Integer | 电子表格的列数。 |
返回
Spreadsheet - 新电子表格
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets
enable All Data Sources Execution()
为所有类型的数据源启用数据执行。
如果未开启数据源类型,数据执行会抛出异常。使用此方法可为所有数据源类型启用数据执行。
// 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
enable Big Query Execution()
针对 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
enable Looker Execution()
为 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(); } }
get Active()
返回当前活跃的电子表格;如果没有,则返回 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
get Active Range()
返回活动工作表中的所选范围,如果没有活动范围,则返回 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
get Active Range List()
返回活动工作表中的活动范围列表,如果没有选择任何范围,则返回 null。包含当前突出显示单元格的活动范围位于列表的末尾。
如果只选择了一个范围,此方法会像 get 调用一样运行。
// Returns the list of active ranges. const rangeList = SpreadsheetApp.getActiveRangeList();
返回
Range - 活跃范围的列表
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Sheet()
获取电子表格中的活动工作表。
电子表格中的活动工作表是指电子表格界面中当前显示的工作表。
// 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
get Active Spreadsheet()
返回当前活跃的电子表格;如果没有,则返回 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
get Current Cell()
返回活动工作表中某个活动范围内的当前(突出显示)所选单元格;如果没有当前单元格,则返回 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
get Selection()
get Ui()
返回电子表格的用户界面环境的实例,该实例允许脚本添加菜单、对话框和边栏等功能。脚本只能与打开的电子表格的当前实例的界面互动,并且只有在脚本绑定到电子表格时才能互动。如需了解详情,请参阅菜单和对话框和边栏指南。
// 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 - 此电子表格的界面环境的实例
new Cell Image()
为 Cell 创建构建器。
// 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);
返回
Cell - 新构建器。
new Color()
为 Color 创建构建器。
const rgbColor = SpreadsheetApp.newColor().setRgbColor('#FF0000').build();
返回
Color - 新构建器。
new Conditional Format Rule()
为条件格式规则创建构建器。
// 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);
返回
Conditional - 新构建器。
new Data Source Spec()
为 Data 创建构建器。
// 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);
返回
Data - 新构建器。
new Data Validation()
为数据验证规则创建构建器。
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);
返回
Data - 新构建器。
new Filter Criteria()
为 Filter 创建构建器。
// 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);
返回
Filter - 新构建器
new Rich Text Value()
为富文本值创建构建器。
// 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);
返回
Rich - 新构建器。
new Text Style()
创建文本样式的构建器。
// 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);
返回
Text - 新构建器。
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()); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
file | File | 要打开的文件。 |
返回
Spreadsheet - 电子表格
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets
open By Id(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());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 相应电子表格的唯一标识符。 |
返回
Spreadsheet - 具有指定 ID 的电子表格对象
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets
open By Url(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());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
url | String | 电子表格的网址。 |
返回
Spreadsheet - 具有指定网址的电子表格对象。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets
set Active Range(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();
参数
| 名称 | 类型 | 说明 |
|---|---|---|
range | Range | 要设为有效范围的范围。 |
返回
Range - 新的有效 Range
set Active Range List(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();
参数
| 名称 | 类型 | 说明 |
|---|---|---|
range | Range | 要选择的范围列表。 |
返回
Range - 新选择的范围列表
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Sheet(sheet)
set Active Sheet(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());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
sheet | Sheet | 新的活动工作表。 |
restore | Boolean | 如果值为 true,则当新工作表变为有效工作表时,系统会重新选择新有效工作表的最近一次选择;如果值为 false,则新工作表变为有效工作表,但不会更改当前选择。 |
返回
Sheet - 新的活动工作表
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Spreadsheet(newActiveSpreadsheet)
设置活动电子表格。
// The code below makes the spreadsheet with key "1234567890" the active // spreadsheet const ss = SpreadsheetApp.openById('1234567890'); SpreadsheetApp.setActiveSpreadsheet(ss);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
new | Spreadsheet | 要设为活动电子表格的电子表格。 |
set Current Cell(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();
参数
| 名称 | 类型 | 说明 |
|---|---|---|
cell | Range | 要设置为当前单元格的单元格。 |
返回
Range - 新设置的当前单元格
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets