Class Sheet

表格

访问和修改电子表格工作表。常见的操作包括重命名工作表和从工作表访问范围对象。

方法

方法返回类型简介
activate()Sheet激活此工作表。
addDeveloperMetadata(key)Sheet将具有指定键的开发者元数据添加到工作表。
addDeveloperMetadata(key, visibility)Sheet将具有指定键和可见性的开发者元数据添加到工作表。
addDeveloperMetadata(key, value)Sheet将具有指定键和值的开发者元数据添加到工作表。
addDeveloperMetadata(key, value, visibility)Sheet将具有指定键、值和可见性的开发者元数据添加到工作表。
appendRow(rowContents)Sheet向工作表中当前数据区域的底部附加一行。
asDataSourceSheet()DataSourceSheet如果工作表的类型为 SheetType.DATASOURCE,则以 DataSourceSheet 形式返回工作表,否则返回 null
autoResizeColumn(columnPosition)Sheet设置指定列的宽度,以适合其内容。
autoResizeColumns(startColumn, numColumns)Sheet设置从指定列位置开始的所有列的宽度,以适应其内容。
autoResizeRows(startRow, numRows)Sheet设置从指定行位置开始的所有行的高度,以适应其内容。
clear()Sheet清除工作表中的内容和格式设置信息。
clear(options)Sheet根据指定的高级选项,清除工作表内容和/或格式。
clearConditionalFormatRules()void从工作表中移除所有条件格式规则。
clearContents()Sheet清除工作表中的内容,同时保留格式信息。
clearFormats()Sheet清除设置了格式的工作表,同时保留内容。
clearNotes()Sheet清除所有备注的工作表。
collapseAllColumnGroups()Sheet收起工作表中的所有列组。
collapseAllRowGroups()Sheet收起工作表中的所有行组。
copyTo(spreadsheet)Sheet将工作表复制到指定的电子表格(可以是与源电子表格相同的电子表格)。
createDeveloperMetadataFinder()DeveloperMetadataFinder返回 DeveloperMetadataFinder,用于查找此表格范围内的开发者元数据。
createTextFinder(findText)TextFinder为工作表创建文本查找器,即可查找和替换工作表中的文本。
deleteColumn(columnPosition)Sheet删除位于指定列位置的列。
deleteColumns(columnPosition, howMany)void删除从指定列位置开始的若干列。
deleteRow(rowPosition)Sheet删除位于指定行位置的行。
deleteRows(rowPosition, howMany)void删除从指定行位置开始的若干行。
expandAllColumnGroups()Sheet展开工作表中的所有列组。
expandAllRowGroups()Sheet展开工作表中的所有行组。
expandColumnGroupsUpToDepth(groupDepth)Sheet展开指定深度的所有列组,并收起其他所有列组。
expandRowGroupsUpToDepth(groupDepth)Sheet展开指定深度的所有行组,并收起其他所有行组。
getActiveCell()Range返回此工作表中的活动单元格。
getActiveRange()Range返回当前工作表中的所选范围,如果没有有效范围,则返回 null
getActiveRangeList()RangeList返回活动工作表中的活动范围列表,如果没有有效范围,则返回 null
getBandings()Banding[]返回此工作表中的所有条带。
getCharts()EmbeddedChart[]返回此工作表中的一组图表。
getColumnGroup(columnIndex, groupDepth)Group返回具有指定索引和组深度的列组。
getColumnGroupControlPosition()GroupControlTogglePosition返回工作表中所有列组的 GroupControlTogglePosition
getColumnGroupDepth(columnIndex)Integer返回给定索引处列的组深度。
getColumnWidth(columnPosition)Integer获取指定列的宽度(以像素为单位)。
getConditionalFormatRules()ConditionalFormatRule[]获取此工作表中的所有条件格式规则。
getCurrentCell()Range返回当前工作表中的当前单元格;如果没有当前单元格,则返回 null
getDataRange()Range返回与存在数据的维度对应的 Range
getDataSourceFormulas()DataSourceFormula[]获取所有数据源公式。
getDataSourcePivotTables()DataSourcePivotTable[]获取所有数据源数据透视表。
getDataSourceTables()DataSourceTable[]获取所有数据源表。
getDeveloperMetadata()DeveloperMetadata[]获取与此表格关联的所有开发者元数据。
getDrawings()Drawing[]返回工作表上的绘图数组。
getFilter()Filter返回此工作表中的过滤条件,如果没有过滤条件,则返回 null
getFormUrl()String返回将其回复发送到此工作表的表单的网址,如果此工作表没有关联的表单,则返回 null
getFrozenColumns()Integer返回冻结列的数量。
getFrozenRows()Integer返回冻结行的数量。
getImages()OverGridImage[]返回工作表上的所有网格上方图片。
getIndex()Integer获取工作表在其父电子表格中的位置。
getLastColumn()Integer返回包含内容的最后一个列的位置。
getLastRow()Integer返回具有内容的最后一行的位置。
getMaxColumns()Integer返回工作表中的当前列数,不考虑内容。
getMaxRows()Integer返回工作表中当前的行数,而不考虑内容。
getName()String返回工作表的名称。
getNamedRanges()NamedRange[]获取此工作表中的所有命名范围。
getParent()Spreadsheet返回包含此工作表的 Spreadsheet
getPivotTables()PivotTable[]返回此工作表中的所有数据透视表。
getProtections(type)Protection[]获取表示工作表中所有受保护范围的对象的数组,或者获取表示工作表本身保护的单元素数组。
getRange(row, column)Range返回左上角单元格位于指定坐标的范围。
getRange(row, column, numRows)Range返回左上角单元格位于指定坐标处和指定行数的范围。
getRange(row, column, numRows, numColumns)Range返回左上角单元格位于指定坐标处以及指定行数和列数的范围。
getRange(a1Notation)Range返回以 A1 或 R1C1 表示法指定的范围。
getRangeList(a1Notations)RangeList返回 RangeList 集合,该集合表示通过非空 A1 表示法或 R1C1 表示法指定的同一工作表中指定的范围。
getRowGroup(rowIndex, groupDepth)Group返回具有指定索引和组深度的行组。
getRowGroupControlPosition()GroupControlTogglePosition返回工作表中所有行组的 GroupControlTogglePosition
getRowGroupDepth(rowIndex)Integer返回行位于指定索引处的组深度。
getRowHeight(rowPosition)Integer获取指定行的高度(以像素为单位)。
getSelection()Selection返回电子表格中的当前 Selection
getSheetId()Integer返回此对象代表的工作表的 ID。
getSheetName()String返回工作表名称。
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]返回此范围值从指定坐标开始的矩形网格。
getSlicers()Slicer[]返回工作表上的截剪器数组。
getTabColorObject()Color获取工作表标签页颜色,如果工作表标签页没有颜色,则返回 null
getType()SheetType返回工作表的类型。
hasHiddenGridlines()Boolean如果工作表的网格线处于隐藏状态,则返回 true;否则返回 false
hideColumn(column)void隐藏指定范围内的一列或多列。
hideColumns(columnIndex)void隐藏位于指定索引的单个列。
hideColumns(columnIndex, numColumns)void隐藏从指定索引开始的一个或多个连续列。
hideRow(row)void隐藏给定范围内的行。
hideRows(rowIndex)void隐藏位于指定索引的行。
hideRows(rowIndex, numRows)void隐藏从指定索引开始的一行或多行连续行。
hideSheet()Sheet隐藏此工作表。
insertChart(chart)void向此工作表中添加新图表。
insertColumnAfter(afterPosition)Sheet在指定列位置之后插入一列。
insertColumnBefore(beforePosition)Sheet在指定列位置之前插入一列。
insertColumns(columnIndex)void在工作表中的指定位置插入空白列。
insertColumns(columnIndex, numColumns)void在工作表中从指定位置开始插入一个或多个连续的空白列。
insertColumnsAfter(afterPosition, howMany)Sheet在指定列位置之后插入指定数量的列。
insertColumnsBefore(beforePosition, howMany)Sheet在指定列位置之前插入多个列。
insertImage(blobSource, column, row)OverGridImage在文档的指定行和列处插入 BlobSource 作为图片。
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImage在文档中的指定行和列处插入 BlobSource 作为图像,并偏移像素。
insertImage(url, column, row)OverGridImage在文档中的给定行和列处插入图片。
insertImage(url, column, row, offsetX, offsetY)OverGridImage在文档中的指定行和列处插入图片(偏移像素)。
insertRowAfter(afterPosition)Sheet在指定行位置之后插入一行。
insertRowBefore(beforePosition)Sheet在指定行位置前插入一行。
insertRows(rowIndex)void在工作表中的指定位置插入空白行。
insertRows(rowIndex, numRows)void在工作表中从指定位置开始插入一个或多个连续空白行。
insertRowsAfter(afterPosition, howMany)Sheet在给定行位置之后插入一定数量的行。
insertRowsBefore(beforePosition, howMany)Sheet在给定行位置之前插入若干行。
insertSlicer(range, anchorRowPos, anchorColPos)Slicer向此工作表添加新的截剪器。
insertSlicer(range, anchorRowPos, anchorColPos, offsetX, offsetY)Slicer向此工作表添加新的截剪器。
isColumnHiddenByUser(columnPosition)Boolean返回用户是否隐藏指定列。
isRightToLeft()Boolean如果此工作表布局为从右到左,则返回 true
isRowHiddenByFilter(rowPosition)Boolean返回给定行是否被过滤器(而不是过滤视图)隐藏。
isRowHiddenByUser(rowPosition)Boolean返回用户是否隐藏了指定行。
isSheetHidden()Boolean如果工作表当前处于隐藏状态,则返回 true
moveColumns(columnSpec, destinationIndex)void将给定范围选择的列移到 destinationIndex 指示的位置。
moveRows(rowSpec, destinationIndex)void将给定范围选择的行移动到 destinationIndex 指示的位置。
newChart()EmbeddedChartBuilder返回构建器为此工作表创建新图表。
protect()Protection创建一个对象,该对象可以防止工作表被拥有的用户修改。
removeChart(chart)void从父工作表中移除图表。
setActiveRange(range)Range将指定范围设为活动工作表中的 active range,并将范围左上角的单元格设为 current cell
setActiveRangeList(rangeList)RangeList将指定的范围列表设为有效工作表中的 active ranges
setActiveSelection(range)Range设置此工作表的有效选择区域。
setActiveSelection(a1Notation)Range按照 A1 或 R1C1 表示法指定有效的选择。
setColumnGroupControlPosition(position)Sheet设置列组控件在工作表中的位置。
setColumnWidth(columnPosition, width)Sheet设置指定列的宽度(以像素为单位)。
setColumnWidths(startColumn, numColumns, width)Sheet设置指定列的宽度(以像素为单位)。
setConditionalFormatRules(rules)void将工作表中当前的所有现有条件格式规则替换为输入规则。
setCurrentCell(cell)Range将指定单元格设置为 current cell
setFrozenColumns(columns)void冻结指定数量的列。
setFrozenRows(rows)void冻结指定的行数。
setHiddenGridlines(hideGridlines)Sheet隐藏或显示工作表网格线。
setName(name)Sheet设置工作表名称。
setRightToLeft(rightToLeft)Sheet将工作表布局设为从右到左。
setRowGroupControlPosition(position)Sheet设置行组控件开关在工作表中的位置。
setRowHeight(rowPosition, height)Sheet设置指定行的行高(以像素为单位)。
setRowHeights(startRow, numRows, height)Sheet设置指定行的高度(以像素为单位)。
setRowHeightsForced(startRow, numRows, height)Sheet设置指定行的高度(以像素为单位)。
setTabColor(color)Sheet设置工作表标签颜色。
setTabColorObject(color)Sheet设置工作表标签颜色。
showColumns(columnIndex)void取消隐藏位于指定索引的列。
showColumns(columnIndex, numColumns)void取消隐藏从指定索引开始的一个或多个连续列。
showRows(rowIndex)void取消隐藏位于指定索引的行。
showRows(rowIndex, numRows)void取消隐藏从指定索引开始的一行或多行连续行。
showSheet()Sheet显示工作表。
sort(columnPosition)Sheet按列对工作表进行升序排序。
sort(columnPosition, ascending)Sheet按列对工作表进行排序。
unhideColumn(column)void取消隐藏给定范围内的列。
unhideRow(row)void取消隐藏给定范围内的行。
updateChart(chart)void更新此工作表中的图表。

详细文档

activate()

激活此工作表。不会更改工作表本身,只会更改父级对有效工作表的概念。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.activate();

弃踢回攻

Sheet - 新的有效工作表。


addDeveloperMetadata(key)

将具有指定键的开发者元数据添加到工作表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds the key 'NAME' to the developer metadata for the sheet.
sheet.addDeveloperMetadata('NAME');

// Gets the updated metadata info and logs it to the console.
console.log(sheet.getDeveloperMetadata()[0].getKey());

参数

名称类型说明
keyString新开发者元数据的键。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

addDeveloperMetadata(key, visibility)

将具有指定键和可见性的开发者元数据添加到工作表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds the key 'NAME' and sets the developer metadata visibility to PROJECT
// for the sheet.
sheet.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT);

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getVisibility().toString());

参数

名称类型说明
keyString新开发者元数据的键。
visibilityDeveloperMetadataVisibility新开发者元数据的公开范围。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

addDeveloperMetadata(key, value)

将具有指定键和值的开发者元数据添加到工作表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata for the sheet.
sheet.addDeveloperMetadata('COMPANY', 'TECH');

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getValue());

参数

名称类型说明
keyString新开发者元数据的键。
valueString新开发者元数据的值。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

addDeveloperMetadata(key, value, visibility)

将具有指定键、值和可见性的开发者元数据添加到工作表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata and sets the
// visibility to DOCUMENT for the sheet.
sheet.addDeveloperMetadata(
  'COMPANY',
  'TECH',
  SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT);

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getValue());
console.log(developerMetaData.getVisibility().toString());

参数

名称类型说明
keyString新开发者元数据的键。
valueString新开发者元数据的值。
visibilityDeveloperMetadataVisibility新开发者元数据的公开范围。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

appendRow(rowContents)

向工作表中当前数据区域的底部附加一行。如果某个单元格的内容以 = 开头,则会被解释为公式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Appends a new row with 3 columns to the bottom of the current
// data region in the sheet containing the values in the array.
sheet.appendRow(["a man", "a plan", "panama"]);

参数

名称类型说明
rowContentsObject[]要在工作表中的最后一行之后插入的一组值。

弃踢回攻

Sheet - 工作表,可用于方法链接。

授权

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

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

asDataSourceSheet()

如果工作表的类型为 SheetType.DATASOURCE,则以 DataSourceSheet 形式返回工作表,否则返回 null

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

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

// Gets the data source sheet value if the sheet is of type
// SpreadsheetApp.SheetType.DATASOURCE, otherwise this returns a null value.
const dataSourceSheet = sheet.asDataSourceSheet();

// Gets the data source sheet value and logs it to the console.
console.log(dataSourceSheet);
console.log(sheet.getType().toString());

弃踢回攻

DataSourceSheet - 数据源工作表。

授权

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

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

autoResizeColumn(columnPosition)

设置指定列的宽度,以适应其内容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.getRange('a1').setValue('Whenever it is a damp, drizzly November in my soul...');

// Sets the first column to a width which fits the text
sheet.autoResizeColumn(1);

参数

名称类型说明
columnPositionInteger要调整大小的指定列的位置。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

autoResizeColumns(startColumn, numColumns)

设置从给定列位置开始的所有列的宽度,以适应其内容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first 15 columns to a width that fits their text.
sheet.autoResizeColumns(1, 15);

参数

名称类型说明
startColumnInteger要自动调整大小的起始列。
numColumnsInteger要自动调整大小的列数。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

autoResizeRows(startRow, numRows)

设置从指定行位置开始的所有行的高度,以适应其内容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first 15 rows to a height that fits their text.
sheet.autoResizeRows(1, 15);

参数

名称类型说明
startRowInteger要自动调整大小的起始行。
numRowsInteger要自动调整的行数。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

clear()

清除工作表中的内容和格式设置信息。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clear();

弃踢回攻

Sheet - 已清除的工作表。

授权

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

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

clear(options)

根据指定的高级选项,清除工作表内容和/或格式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
sheet.clear({ formatOnly: true, contentsOnly: true });

参数

名称类型说明
optionsObject包含下列高级选项的 JavaScript 地图。

高级参数

名称类型说明
contentsOnlyBoolean是否清除内容。
formatOnlyBoolean是否清除格式。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

clearConditionalFormatRules()

从工作表中移除所有条件格式规则。等同于以空数组作为输入来调用 setConditionalFormatRules(rules)

var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearConditionalFormatRules();

授权

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

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

clearContents()

清除工作表中的内容,同时保留格式信息。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearContents();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

clearFormats()

清除格式工作表,同时保留内容。

格式是指根据“格式”菜单下的选项(例如,粗体、斜体、条件格式)允许的数据设置格式,而不是单元格的宽度或高度。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearFormats();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

clearNotes()

清除工作表中的所有备注。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearNotes();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

collapseAllColumnGroups()

收起工作表中的所有列组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All column groups on the sheet are collapsed.
sheet.collapseAllColumnGroups();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

collapseAllRowGroups()

收起工作表中的所有行组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All row groups on the sheet are collapsed.
sheet.collapseAllRowGroups();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

copyTo(spreadsheet)

将工作表复制到指定的电子表格(可以是与源电子表格相同的电子表格)。复制的工作表的名称为“[原始名称]的副本”。

var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];

var destination = SpreadsheetApp.openById('ID_GOES HERE');
sheet.copyTo(destination);

参数

名称类型说明
spreadsheetSpreadsheet将此工作表复制到的电子表格,可以是与源电子表格相同的电子表格。

弃踢回攻

Sheet - 用于链接的新工作表。

授权

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

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

createDeveloperMetadataFinder()

返回 DeveloperMetadataFinder,用于查找此表格范围内的开发者元数据。如果元数据与工作表本身关联或者与该工作表中的行、列或范围关联,则该元数据在特定工作表的作用域内。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds developer metadata for testing.
sheet.addDeveloperMetadata('CITY', 'PARIS');

// Creates the developer metadata finder.
const metadatafinder = sheet.createDeveloperMetadataFinder();

// Finds the metadata with value 'PARIS' and displays its key in the console.
console.log(metadatafinder.withValue('PARIS').find()[0].getKey());

弃踢回攻

DeveloperMetadataFinder - 一种开发者元数据查找工具,用于搜索此表格范围内的元数据。


createTextFinder(findText)

为工作表创建文字查找器,以便查找和替换工作表中的文本。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// Creates  a text finder.
var textFinder = sheet.createTextFinder('dog');

// Returns the first occurrence of 'dog' in the sheet.
var firstOccurrence = textFinder.findNext();

// Replaces the last found occurrence of 'dog' with 'cat' and returns the number
// of occurrences replaced.
var numOccurrencesReplaced = findOccurrence.replaceWith('cat');

参数

名称类型说明
findTextString要搜索的文本。

弃踢回攻

TextFinder - 工作表的 TextFinder


deleteColumn(columnPosition)

删除位于指定列位置的列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Columns start at "1" - this deletes the first column
sheet.deleteColumn(1);

参数

名称类型说明
columnPositionInteger列的位置,第一列从 1 开始。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

deleteColumns(columnPosition, howMany)

删除从指定列位置开始的若干列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Columns start at "1" - this deletes the first two columns
sheet.deleteColumns(1, 2);

参数

名称类型说明
columnPositionInteger要删除的第一列的位置。
howManyInteger要删除的列数。

授权

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

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

deleteRow(rowPosition)

删除位于指定行位置的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Rows start at "1" - this deletes the first row
sheet.deleteRow(1);

参数

名称类型说明
rowPositionInteger行的位置,从第一行的 1 开始。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

deleteRows(rowPosition, howMany)

删除从指定行位置开始的若干行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Rows start at "1" - this deletes the first two rows
sheet.deleteRows(1, 2);

参数

名称类型说明
rowPositionInteger要删除的第一行的位置。
howManyInteger要删除的行数。

授权

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

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

expandAllColumnGroups()

展开工作表中的所有列组。此方法至少需要一个列组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All column groups on the sheet are expanded.
sheet.expandAllColumnGroups();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

expandAllRowGroups()

展开工作表中的所有行组。此方法至少需要一个行组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All row groups on the sheet are expanded.
sheet.expandAllRowGroups();

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

expandColumnGroupsUpToDepth(groupDepth)

展开指定深度的所有列组,并收起其他所有列组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All column groups of depth 2 and lower are expanded, and groups with depth
// 3 and higher are collapsed.
sheet.expandColumnGroupsUpToDepth(2);

参数

名称类型说明
groupDepthInteger要向其展开列组的组深度。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

expandRowGroupsUpToDepth(groupDepth)

展开指定深度的所有行组,并收起其他所有行组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// All row groups of depth 2 and lower are expanded, and groups with depth
// 3 and higher are collapsed.
sheet.expandRowGroupsUpToDepth(2);

参数

名称类型说明
groupDepthInteger要向其展开行组的组深度。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

getActiveCell()

返回此工作表中的活动单元格。

注意:最好使用 getCurrentCell(),它会返回当前突出显示的单元格。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Returns the active cell
var cell = sheet.getActiveCell();

弃踢回攻

Range - 当前活动单元格

授权

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

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

getActiveRange()

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

“活动范围”一词是指用户在活动工作表中选择的范围,但在自定义函数中,指的是系统主动重新计算的单元格。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeRange = sheet.getActiveRange();

弃踢回攻

Range - 有效范围

授权

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

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

另请参阅


getActiveRangeList()

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

如果选择一个范围,则此行为将像 getActiveRange() 调用一样。

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

弃踢回攻

RangeList - 有效范围的列表

授权

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

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

另请参阅


getBandings()

返回此工作表中的所有条带。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the banding info for the sheet.
const bandings = sheet.getBandings();

// Gets info on the bandings' second row color and logs it to the console.
for (const banding of bandings) {
  console.log(banding.getSecondRowColor());
}

弃踢回攻

Banding[] - 此工作表中的所有条带。

授权

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

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

getCharts()

返回此工作表中的一组图表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var charts = sheet.getCharts();

for (var i in charts) {
  var chart = charts[i];
  // Do something with the chart
}

弃踢回攻

EmbeddedChart[] - 图表数组。

授权

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

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

getColumnGroup(columnIndex, groupDepth)

返回具有指定索引和组深度的列组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// Returns the group whose control index is at column 2 and has a depth of 1, or
// null if the group doesn’t exist.
var columnGroup = sheet.getColumnGroup(2, 1);

参数

名称类型说明
columnIndexInteger组控件切换开关的列索引或组内的索引。
groupDepthInteger组的深度。

弃踢回攻

Group - 位于控件索引和深度的列组,如果该组不存在,则抛出异常。

授权

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

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

getColumnGroupControlPosition()

返回工作表中所有列组的 GroupControlTogglePosition

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// GroupControlTogglePosition.AFTER if the column grouping control toggle is shown after the
// group.
var columnGroupControlPosition = sheet.getColumnGroupControlPosition();

弃踢回攻

GroupControlTogglePosition - 如果在此工作表上的组后面显示列分组控件切换开关,则为 true,否则为 false

授权

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

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

getColumnGroupDepth(columnIndex)

返回给定索引处列的组深度。

组深度表示有多少组与该列重叠。该值的范围在 0 到 8 之间。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// 1 if there is a group over columns 1 through 3
var groupDepth = sheet.getColumnGroupDepth(1);

参数

名称类型说明
columnIndexInteger列的索引。

弃踢回攻

Integer - 列在指定索引处的组深度。

授权

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

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

getColumnWidth(columnPosition)

获取指定列的宽度(以像素为单位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Columns start at 1
Logger.log(sheet.getColumnWidth(1));

参数

名称类型说明
columnPositionInteger要检查的列的位置。

弃踢回攻

Integer - 列宽(以像素为单位)

授权

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

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

getConditionalFormatRules()

获取此工作表中的所有条件格式规则。

// Logs the conditional format rules in a sheet.
var rules = SpreadsheetApp.getActiveSheet().getConditionalFormatRules();
for (var i = 0; i < rules.length; i++) {
  var rule = rules[i];
  Logger.log(rule);
}

弃踢回攻

ConditionalFormatRule[] - 包含工作表中所有规则的数组。

授权

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

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

getCurrentCell()

返回当前工作表中的当前单元格;如果没有当前单元格,则返回 null。当前单元格是 Google 表格界面中具有焦点的单元格,并用深色边框突出显示。当前单元格数量永远不会超过 1 个。当用户选择一个或多个单元格范围时,所选范围内的某个单元格就是当前单元格。

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

弃踢回攻

Range - 当前单元格

授权

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

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

getDataRange()

返回与存在数据的维度对应的 Range

这在功能上等同于创建由 A1 和(Sheet.getLastColumn()、Sheet.getLastRow())限定的范围。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This represents ALL the data
var range = sheet.getDataRange();
var values = range.getValues();

// This logs the spreadsheet in CSV format with a trailing comma
for (var i = 0; i < values.length; i++) {
  var row = "";
  for (var j = 0; j < values[i].length; j++) {
    if (values[i][j]) {
      row = row + values[i][j];
    }
    row = row + ",";
  }
  Logger.log(row);
}

弃踢回攻

Range - 包含电子表格中所有数据的范围

授权

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

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

getDataSourceFormulas()

获取所有数据源公式。

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

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

// Gets an array of the data source formulas on Sheet1.
// To get an array of data source formulas for the entire spreadsheet,
// replace 'sheet' with 'ss'.
const dataSourceFormulas = sheet.getDataSourceFormulas();

// Logs the first data source formula in the array.
console.log(dataSourceFormulas[0].getFormula());

弃踢回攻

DataSourceFormula[] - 数据源公式列表。

授权

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

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

getDataSourcePivotTables()

获取所有数据源数据透视表。

// 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 Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets an array of the data source pivot tables on Sheet1.
// To get an array of data source pivot tables for the entire
// spreadsheet, replace 'sheet' with 'ss'.
const dataSourcePivotTables = sheet.getDataSourcePivotTables();

// Logs the last time that the first pivot table in the array was refreshed.
console.log(dataSourcePivotTables[0].getStatus().getLastRefreshedTime());

弃踢回攻

DataSourcePivotTable[] - 数据源数据透视表的列表。

授权

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

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

getDataSourceTables()

获取所有数据源表。

// 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 Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets an array of data source tables on Sheet1.
// To get an array of data source tables for the entire spreadsheet,
// replace 'sheet' with 'ss'.
const dataSourceTables = sheet.getDataSourceTables();

// Logs the last completed data execution time on the first data source table.
console.log(dataSourceTables[0].getStatus().getLastExecutionTime());

弃踢回攻

DataSourceTable[] - 数据源表的列表。

授权

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

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

getDeveloperMetadata()

获取与此表格关联的所有开发者元数据。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds developer metadata for testing.
sheet.addDeveloperMetadata('CITY', 'PARIS');

// Gets all the developer metadata for the sheet.
const developerMetaDataList = sheet.getDeveloperMetadata();

// Logs the developer metadata to the console.
for (const developerMetaData of developerMetaDataList) {
  console.log(developerMetaData.getKey());
}

弃踢回攻

DeveloperMetadata[] - 与此工作表关联的开发者元数据。

授权

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

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

getDrawings()

返回工作表上的绘图数组。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all the drawings from the sheet.
const allDrawings = sheet.getDrawings();

 // Logs the number of drawings present on the sheet.
console.log(allDrawings.length);

弃踢回攻

Drawing[] - 此工作表上的绘图列表。

授权

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

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

getFilter()

返回此工作表中的过滤条件,如果没有过滤条件,则返回 null

// Gets the filter on the active sheet.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();

弃踢回攻

Filter - 过滤器。

授权

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

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

getFormUrl()

返回将其回复发送到此工作表的表单的网址,如果此工作表没有关联的表单,则返回 null

var sheet = SpreadsheetApp.getActiveSheet();
var url = sheet.getFormUrl();

弃踢回攻

String - 将回复放入此工作表的表单的网址,如果此工作表没有关联的表单,则为 null

授权

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

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

getFrozenColumns()

返回冻结列的数量。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log("Number of frozen columns: %s", sheet.getFrozenColumns());

弃踢回攻

Integer - 冻结列的数量

授权

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

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

getFrozenRows()

返回冻结行的数量。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log("Number of frozen rows: %s", sheet.getFrozenRows());

弃踢回攻

Integer - 冻结行的数量

授权

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

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

getImages()

返回工作表上的所有网格上方图片。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, 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 the over-the-grid images from Sheet1.
// To get the over-the-grid images from the entire spreadsheet, use ss.getImages() instead.
const images = sheet.getImages();

// For each image, logs the anchor cell in A1 notation.
for (const image of images) {
 console.log(image.getAnchorCell().getA1Notation());
}

弃踢回攻

OverGridImage[] - 网格上方图片数组。

授权

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

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

getIndex()

获取工作表在其父电子表格中的位置。从 1 开始。

var ss = SpreadsheetApp.getActiveSpreadsheet();
// Note that the JavaScript index is 0, but this logs 1
var sheet = ss.getSheets()[0];
// ... because spreadsheets are 1-indexed
Logger.log(sheet.getIndex());

弃踢回攻

Integer - 工作表在其父电子表格中的位置。

授权

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

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

getLastColumn()

返回包含内容的最后一个列的位置。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());

弃踢回攻

Integer - 工作表中包含内容的最后一列

授权

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

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

getLastRow()

返回具有内容的最后一行的位置。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());

弃踢回攻

Integer - 工作表中包含内容的最后一行

授权

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

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

getMaxColumns()

无论内容如何,返回工作表中的当前列数。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
Logger.log(first.getMaxColumns());

弃踢回攻

Integer - 工作表的最大宽度。

授权

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

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

getMaxRows()

返回工作表中当前的行数,而不考虑内容。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
Logger.log(first.getMaxRows());

弃踢回攻

Integer - 工作表的最大高度。

授权

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

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

getName()

返回工作表的名称。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getName());

弃踢回攻

String - 工作表的名称。

授权

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

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

getNamedRanges()

获取此工作表中的所有命名范围。

// The code below logs the name of the first named range.
var namedRanges = SpreadsheetApp.getActiveSheet().getNamedRanges();
if (namedRanges.length > 1) {
  Logger.log(namedRanges[0].getName());
}

弃踢回攻

NamedRange[] - 包含工作表中所有命名范围的数组。

授权

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

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

getParent()

返回包含此工作表的 Spreadsheet

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// parent is identical to ss
var parent = sheet.getParent();

弃踢回攻

Spreadsheet - 父级电子表格。

授权

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

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

getPivotTables()

返回此工作表中的所有数据透视表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all the pivot table info for the sheet.
const pivotTables = sheet.getPivotTables();

// Logs the pivot tables to the console.
for (const pivotTable of pivotTables) {
  console.log(pivotTable.getSourceDataRange().getValues());
}

弃踢回攻

PivotTable[] - 此工作表中的数据透视表。

授权

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

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

getProtections(type)

获取表示工作表中所有受保护范围的对象的数组,或获取表示工作表本身保护的单元素数组。

// Remove all range protections in the spreadsheet that the user has permission to edit.
var sheet = SpreadsheetApp.getActiveSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove sheet protection from the active sheet, if the user has permission to edit it.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && protection.canEdit()) {
  protection.remove();
}

参数

名称类型说明
typeProtectionType受保护区域的类型,值为 SpreadsheetApp.ProtectionType.RANGESpreadsheetApp.ProtectionType.SHEET

弃踢回攻

Protection[] - 表示工作表中所有受保护范围的对象的数组,或者表示工作表本身保护的单元素数组。

授权

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

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

getRange(row, column)

返回左上角单元格位于指定坐标的范围。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Passing only two arguments returns a "range" with a single cell.
var range = sheet.getRange(1, 1);
var values = range.getValues();
Logger.log(values[0][0]);

参数

名称类型说明
rowInteger要返回的单元格的行索引;行索引从 1 开始。
columnInteger要返回的单元格的列索引;列索引从 1 开始。

弃踢回攻

Range - 仅包含此单元格的范围。

授权

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

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

getRange(row, column, numRows)

返回左上角单元格位于指定坐标处和指定行数的范围。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// When the "numRows" argument is used, only a single column of data is returned.
var range = sheet.getRange(1, 1, 3);
var values = range.getValues();

// Prints 3 values from the first column, starting from row 1.
for (var row in values) {
  for (var col in values[row]) {
    Logger.log(values[row][col]);
  }
}

参数

名称类型说明
rowInteger范围的起始行索引;行索引从 1 开始。
columnInteger范围的列索引;列索引从 1 开始。
numRowsInteger要返回的行数。

弃踢回攻

Range - 包含指定行数的单列数据的范围。

授权

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

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

getRange(row, column, numRows, numColumns)

返回左上角单元格位于指定坐标处以及指定行数和列数的范围。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, 3, 3);
var values = range.getValues();

// Print values from a 3x3 box.
for (var row in values) {
  for (var col in values[row]) {
    Logger.log(values[row][col]);
  }
}

参数

名称类型说明
rowInteger范围的起始行索引;行索引从 1 开始。
columnInteger范围的起始列索引;列索引从 1 开始。
numRowsInteger要返回的行数。
numColumnsInteger要返回的列数。

弃踢回攻

Range - 与指定区域对应的范围。

授权

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

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

getRange(a1Notation)

返回以 A1 或 R1C1 表示法指定的范围。

// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");

// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");

参数

名称类型说明
a1NotationString要返回的范围,采用 A1 表示法或 R1C1 表示法指定。

弃踢回攻

Range - 指定位置的范围

授权

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

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

getRangeList(a1Notations)

返回 RangeList 集合,该集合表示通过非空 A1 表示法或 R1C1 表示法指定的同一工作表中指定的范围。

// Get a list of ranges A1:D4, F1:H4.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var rangeList  = sheet.getRangeList(['A1:D4', 'F1:H4']);

参数

名称类型说明
a1NotationsString[]要返回的范围列表,采用 A1 表示法或 R1C1 表示法指定。

弃踢回攻

RangeList - 指定位置的范围列表

授权

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

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

getRowGroup(rowIndex, groupDepth)

返回具有指定索引和组深度的行组。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// Returns the group whose control index is at row 2 and has a depth of 1, or
// null if the group doesn’t exist.
var rowGroup = sheet.getRowGroup(2, 1);

参数

名称类型说明
rowIndexInteger组控件切换开关的行索引或组内的索引。
groupDepthInteger组的深度。

弃踢回攻

Group - 位于控件索引和深度的行组,如果该行组不存在,则抛出异常。

授权

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

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

getRowGroupControlPosition()

返回工作表中所有行组的 GroupControlTogglePosition

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// GroupControlTogglePosition.AFTER if the row grouping control toggle is shown after the
// group.
var rowGroupControlPosition = sheet.getRowGroupControlPosition();

弃踢回攻

GroupControlTogglePosition - 如果行分组控件切换开关显示在此工作表的组后面,则为 true,否则为 false

授权

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

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

getRowGroupDepth(rowIndex)

返回位于指定索引的行的组深度。

组深度表示与该行重叠的组数量。范围在 0 到 8 之间。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// 1 if there is a group over rows 1 through 3
var groupDepth = sheet.getRowGroupDepth(1);

参数

名称类型说明
rowIndexInteger行的索引。

弃踢回攻

Integer - 行位于指定索引处的组深度。

授权

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

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

getRowHeight(rowPosition)

获取指定行的高度(以像素为单位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.getRowHeight(1));

参数

名称类型说明
rowPositionInteger要检查的行的位置。

弃踢回攻

Integer - 行高(以像素为单位)

授权

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

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

getSelection()

返回电子表格中的当前 Selection

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

弃踢回攻

Selection - 当前选择

授权

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

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

getSheetId()

返回此对象代表的工作表的 ID。

这是该电子表格专属的工作表 ID。该 ID 是在创建工作表时分配的单调递增整数,与工作表位置无关。这在与接受 gridId 参数(而非 Sheet 实例)的 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等方法结合使用时很有用。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log(sheet.getSheetId());

弃踢回攻

Integer - 电子表格所独有的工作表 ID

授权

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

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

getSheetName()

返回工作表名称。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log(sheet.getSheetName());

弃踢回攻

String - 工作表的名称

授权

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

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

getSheetValues(startRow, startColumn, numRows, numColumns)

返回此范围值从指定坐标开始的矩形网格。将值设为 -1 表示行或列的位置值相当于获取表格中包含数据的最后一行或最后一列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// The two samples below produce the same output
var values = sheet.getSheetValues(1, 1, 3, 3);
Logger.log(values);

var range = sheet.getRange(1, 1, 3, 3);
values = range.getValues();
Logger.log(values);

参数

名称类型说明
startRowInteger起始行的位置。
startColumnInteger起始列的位置。
numRowsInteger要返回其值的行数。
numColumnsInteger要返回其值的列数。

弃踢回攻

Object[][] - 二维值数组

授权

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

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

getSlicers()

返回工作表上的截剪器数组。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all slicers in the spreadsheet.
const slicers = sheet.getSlicers();

// Logs the slicer titles to the console.
for (const slicer of slicers) {
  console.log(slicer.getTitle());
}

弃踢回攻

Slicer[] - 此工作表中的截剪器列表。

授权

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

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

getTabColorObject()

获取工作表标签页颜色,如果工作表标签页没有颜色,则返回 null

// This example assumes there is a sheet named "Sheet1"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Sheet1");
var color = first.getTabColorObject();

弃踢回攻

Color - 工作表标签页颜色,如果工作表标签页没有颜色,则为 null

授权

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

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

getType()

返回工作表的类型。

工作表的默认类型为 SheetType.GRID。包含单个嵌入对象的工作表(例如 EmbeddedChart)称为 SheetType.OBJECT 工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getType());

弃踢回攻

SheetType - 工作表的类型。

授权

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

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

hasHiddenGridlines()

如果工作表的网格线处于隐藏状态,则返回 true;否则返回 false。默认情况下,网格线处于可见状态。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Checks if the spreadsheet has hidden gridelines and logs the result to the console.
console.log(sheet.hasHiddenGridlines());

弃踢回攻

Boolean - 如果网格线隐藏,则为 true;否则为 false

授权

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

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

hideColumn(column)

隐藏给定范围内的一列或多列。

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];

// This hides the first column
let range = sheet.getRange("A1");
sheet.hideColumn(range);

// This hides the first 3 columns
let range = sheet.getRange("A:C");
sheet.hideColumn(range);

参数

名称类型说明
columnRange要隐藏的列范围。

授权

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

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

hideColumns(columnIndex)

隐藏位于指定索引的单个列。此方法使用 1 索引。

如需使用索引隐藏多个列,请使用 hideColumns(columnIndex, numColumns)

要使用某个范围隐藏多个列,请使用 hideColumn()

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];
// Hides the first column
sheet.hideColumns(1);

参数

名称类型说明
columnIndexInteger要隐藏的列的索引。

授权

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

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

hideColumns(columnIndex, numColumns)

隐藏从指定索引开始的一个或多个连续列。此方法使用 1 索引。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three columns
sheet.hideColumns(1, 3);

参数

名称类型说明
columnIndexInteger要隐藏的列的起始索引。
numColumnsInteger要隐藏的列数。

授权

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

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

hideRow(row)

隐藏给定范围内的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This hides the first row
var range = sheet.getRange("A1");
sheet.hideRow(range);

参数

名称类型说明
rowRange要隐藏的行范围。

授权

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

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

hideRows(rowIndex)

隐藏位于指定索引的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first row
sheet.hideRows(1);

参数

名称类型说明
rowIndexInteger要隐藏的行的索引。

授权

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

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

hideRows(rowIndex, numRows)

隐藏从指定索引开始的一行或多行连续行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);

参数

名称类型说明
rowIndexInteger要隐藏的行的起始索引。
numRowsInteger要隐藏的行数。

授权

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

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

hideSheet()

隐藏此工作表。如果工作表已隐藏,则不会产生任何影响。如果在唯一可见的工作表上调用此方法,则会抛出异常。

var sheet = SpreadsheetApp.getActiveSheet();
sheet.hideSheet();

弃踢回攻

Sheet - 当前工作表。

授权

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

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

insertChart(chart)

向此工作表中添加新图表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This creates a simple bar chart from the first three rows
// of the first two columns of the spreadsheet
var chart = sheet.newChart()
    .setChartType(Charts.ChartType.BAR)
    .addRange(sheet.getRange("A1:B4"))
    .setPosition(5, 5, 0, 0)
    .setOption("title", "Dynamic Chart")
    .build();
sheet.insertChart(chart);

参数

名称类型说明
chartEmbeddedChart要插入的图表。

授权

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

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

insertColumnAfter(afterPosition)

在指定列位置之后插入一列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a column after the first column position
sheet.insertColumnAfter(1);

参数

名称类型说明
afterPositionInteger应在其后添加新列的列。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertColumnBefore(beforePosition)

在指定列位置之前插入一列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a column in the first column position
sheet.insertColumnBefore(1);

参数

名称类型说明
beforePositionInteger应在其前添加新列的列。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertColumns(columnIndex)

在工作表中的指定位置插入空白列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all columns by one
sheet.insertColumns(1);

参数

名称类型说明
columnIndexInteger指示插入列的位置的索引。

授权

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

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

insertColumns(columnIndex, numColumns)

在工作表中从指定位置开始插入一个或多个连续的空白列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all columns by three
sheet.insertColumns(1, 3);

参数

名称类型说明
columnIndexInteger指示插入列的位置的索引。
numColumnsInteger要插入的列数。

授权

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

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

insertColumnsAfter(afterPosition, howMany)

在指定列位置之后插入指定数量的列。

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];

// Inserts two columns after the first column on the first sheet of the spreadsheet.
sheet.insertColumnsAfter(1,2);

参数

名称类型说明
afterPositionInteger应在其后添加新列的列。
howManyInteger要插入的列数。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertColumnsBefore(beforePosition, howMany)

在指定列位置之前插入多个列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five columns before the first column
sheet.insertColumnsBefore(1, 5);

参数

名称类型说明
beforePositionInteger应在其前添加新列的列。
howManyInteger要插入的列数。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertImage(blobSource, column, row)

在文档的指定行和列处插入 BlobSource 作为图片。系统会从 blob 内容中检索图片大小。支持的 blob 大小上限为 2MB。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
sheet.insertImage(blob, 1, 1);

参数

名称类型说明
blobSourceBlobSource包含图片内容、MIME 类型和(可选)名称的 blob。
columnInteger列位置。
rowInteger行位置。

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertImage(blobSource, column, row, offsetX, offsetY)

在文档中的指定行和列处插入 BlobSource 作为图像,并偏移像素。系统从 blob 内容中检索图像大小。支持的 blob 大小上限为 2 MB。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
sheet.insertImage(blob, 1, 1, 10, 10);

参数

名称类型说明
blobSourceBlobSource包含图片内容、MIME 类型和(可选)名称的 blob。
columnInteger列位置。
rowInteger行位置。
offsetXInteger相对于单元格角的水平偏移量(以像素为单位)。
offsetYInteger相对于单元格角的垂直偏移量(以像素为单位)。

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertImage(url, column, row)

在文档中的给定行和列处插入图片。

提供的网址必须可公开访问。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1);

参数

名称类型说明
urlString图片的网址。
columnInteger网格列的位置。
rowInteger网格行位置。

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertImage(url, column, row, offsetX, offsetY)

在文档中的指定行和列处插入图片(偏移像素)。

提供的网址必须可公开访问。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1, 10, 10);

参数

名称类型说明
urlString图片的网址。
columnInteger列位置。
rowInteger行位置。
offsetXInteger相对于单元格角的水平偏移量(以像素为单位)。
offsetYInteger相对于单元格角的垂直偏移量(以像素为单位)。

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertRowAfter(afterPosition)

在指定行位置之后插入一行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a row after the first row position
sheet.insertRowAfter(1);

参数

名称类型说明
afterPositionInteger应在其后面添加新行的行。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertRowBefore(beforePosition)

在指定行位置前插入一行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a row before the first row position
sheet.insertRowBefore(1);

参数

名称类型说明
beforePositionInteger应在其前面添加新行的行。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertRows(rowIndex)

在工作表中的指定位置插入空白行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all rows down by one
sheet.insertRows(1);

参数

名称类型说明
rowIndexInteger指示插入行的位置的索引。

授权

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

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

insertRows(rowIndex, numRows)

在工作表中从指定位置开始插入一个或多个连续的空白行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all rows down by three
sheet.insertRows(1, 3);

参数

名称类型说明
rowIndexInteger指示插入行的位置的索引。
numRowsInteger要插入的行数。

授权

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

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

insertRowsAfter(afterPosition, howMany)

在指定行位置之后插入一定数量的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five rows after the first row
sheet.insertRowsAfter(1, 5);

参数

名称类型说明
afterPositionInteger应在其后添加新行的行。
howManyInteger要插入的行数。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertRowsBefore(beforePosition, howMany)

在指定行位置之前插入若干行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five rows before the first row
sheet.insertRowsBefore(1, 5);

参数

名称类型说明
beforePositionInteger应在其前面添加新行的行。
howManyInteger要插入的行数。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

insertSlicer(range, anchorRowPos, anchorColPos)

向此工作表添加新的截剪器。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the range of the sheet.
const range = sheet.getRange('A1:D10');

// Inserts the slicer with a random range into the sheet.
const insertSlicers = sheet.insertSlicer(range.randomize(), 1, 10);

// Logs the insert slicer result to the console.
console.log(insertSlicers);

参数

名称类型说明
rangeRange创建截剪器截剪器的范围。
anchorRowPosInteger截剪器的顶部锚定在此行中。
anchorColPosInteger截剪器的顶部固定在此列中。

弃踢回攻

Slicer - 新插入的截剪器。

授权

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

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

insertSlicer(range, anchorRowPos, anchorColPos, offsetX, offsetY)

向此工作表添加新的截剪器。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the range.
const range = sheet.getRange('A1:D10');

// Inserts a slicer using the random range function.
const insertSlicers = sheet.insertSlicer(range.randomize(), 1, 10, 0, 0);

// Logs the insert slicer result to the console.
console.log(insertSlicers);

参数

名称类型说明
rangeRange创建截剪器截剪器的范围。
anchorRowPosInteger截剪器的顶部锚定在此行中。
anchorColPosInteger截剪器的顶部固定在此列中。
offsetXInteger相对于单元格角的水平偏移量(以像素为单位)。
offsetYInteger相对于单元格角的垂直偏移量(以像素为单位)。

弃踢回攻

Slicer - 新插入的截剪器。

授权

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

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

isColumnHiddenByUser(columnPosition)

返回用户是否隐藏指定列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Columns start at 1
Logger.log(sheet.isColumnHiddenByUser(1));

参数

名称类型说明
columnPositionInteger要检查的列的位置。

弃踢回攻

Boolean - 如果列隐藏,则为 true;否则为 false

授权

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

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

isRightToLeft()

如果此工作表布局为从右到左,则返回 true。如果工作表使用默认的从左到右布局,则返回 false

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Checks if a spreadsheet is ordered from right to left and logs the result to the console.
console.log(sheet.isRightToLeft());

弃踢回攻

Boolean - 如果从右到左,则为 true;否则为 false

授权

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

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

isRowHiddenByFilter(rowPosition)

返回给定行是否被过滤器(而不是过滤视图)隐藏。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.isRowHiddenByFilter(1));

参数

名称类型说明
rowPositionInteger要检查的行的位置。

弃踢回攻

Boolean - 如果该行隐藏,则为 true;否则为 false

授权

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

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

isRowHiddenByUser(rowPosition)

返回用户是否隐藏了指定行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.isRowHiddenByUser(1));

参数

名称类型说明
rowPositionInteger要检查的行的位置。

弃踢回攻

Boolean - 如果该行隐藏,则为 true;否则为 false

授权

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

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

isSheetHidden()

如果工作表当前处于隐藏状态,则返回 true

var sheet = SpreadsheetApp.getActiveSheet();
if (sheet.isSheetHidden()) {
  // do something...
}

弃踢回攻

Boolean - 如果工作表处于隐藏状态,则返回 true;否则,返回 false

授权

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

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

moveColumns(columnSpec, destinationIndex)

将给定范围选择的列移到 destinationIndex 指示的位置。columnSpec 本身不必精确表示要移动的整整列或一组列,它可以选择范围涵盖的所有列。

// The code below moves rows A-B to destination index 5.
// This results in those columns becoming columns C-D.
var sheet = SpreadsheetApp.getActiveSheet();
// Selects column A and column B to be moved.
var columnSpec = sheet.getRange("A1:B1");
sheet.moveColumns(columnSpec, 5);

参数

名称类型说明
columnSpecRange跨越要移动的列的范围。
destinationIndexInteger列应移至的索引。请注意,此索引基于移动列之前的坐标。现有数据会右移,以便为移动的列腾出空间,同时源列会从网格中移除。因此,数据最终可能无法获得最初指定的索引。

授权

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

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

moveRows(rowSpec, destinationIndex)

将给定范围选择的行移动到 destinationIndex 指示的位置。rowSpec 本身不必精确表示要移动的整行或整组行,它可以选择范围涵盖的所有行。

// The code below moves rows 1-2 to destination index 5.
// This results in those rows becoming rows 3-4.
var sheet = SpreadsheetApp.getActiveSheet();
// Selects row 1 and row 2 to be moved.
var rowSpec = sheet.getRange("A1:A2");
sheet.moveRows(rowSpec, 5);

参数

名称类型说明
rowSpecRange跨越应移动的行的范围。
destinationIndexInteger要将行移动到的索引。请注意,此索引基于移动行之前的坐标。从网格中移除源行时,现有数据会下移,以便为移动的行腾出空间。因此,数据最终可能无法与最初指定的索引不同。

授权

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

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

newChart()

返回构建器为此工作表创建新图表。

以下示例展示了如何创建新图表:

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B8");
var chartBuilder = sheet.newChart();
chartBuilder.addRange(range)
    .setChartType(Charts.ChartType.LINE)
    .setPosition(2, 2, 0, 0)
    .setOption('title', 'My Line Chart!');
sheet.insertChart(chartBuilder.build());

弃踢回攻

EmbeddedChartBuilder - 用于创建新图表的构建器。


protect()

创建一个对象,该对象可以防止工作表被拥有的用户修改。在脚本实际更改工作表的编辑者列表(通过调用 Protection.removeEditor(emailAddress)Protection.removeEditor(user)Protection.removeEditors(emailAddresses)Protection.addEditor(emailAddress)Protection.addEditor(user)Protection.addEditors(emailAddresses) 或为 Protection.setDomainEdit(editable) 设置新值)之前,权限会镜像电子表格本身的权限,这实际上意味着该工作表将保持不保护。如果工作表已受保护,则此方法会返回一个表示其现有保护设置的对象。 受保护的工作表可能包含不受保护的区域。

// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

弃踢回攻

Protection - 一个表示保护设置的对象。

授权

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

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

removeChart(chart)

从父工作表中移除图表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This removes all the embedded charts from the spreadsheet
var charts = sheet.getCharts();
for (var i in charts) {
  sheet.removeChart(charts[i]);
}

参数

名称类型说明
chartEmbeddedChart要移除的图表。

授权

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

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

setActiveRange(range)

将指定范围设为活动工作表中的 active range,并将范围左上角的单元格设为 current cell

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('A1:D4');
sheet.setActiveRange(range);

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

参数

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

弃踢回攻

Range - 新的有效范围

授权

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

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

setActiveRangeList(rangeList)

将指定的范围列表设为有效工作表中的 active ranges。列表中的最后一个范围被设为 active range

var sheet = SpreadsheetApp.getActiveSheet();
var rangeList = sheet.getRangeList(['D4', 'B2:C4']);
sheet.setActiveRangeList(rangeList);

var selection = sheet.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

setActiveSelection(range)

设置此工作表的有效选择区域。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var range = sheet.getRange("A1:D4");
sheet.setActiveSelection(range);

参数

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

弃踢回攻

Range - 新的有效范围

授权

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

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

setActiveSelection(a1Notation)

按照 A1 或 R1C1 表示法指定有效的选择。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.setActiveSelection("A1:D4");

参数

名称类型说明
a1NotationString要设置为有效状态的范围,采用 A1 表示法或 R1C1 表示法指定。

弃踢回攻

Range - 新的有效范围

授权

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

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

setColumnGroupControlPosition(position)

设置列组控件切换开关在工作表中的位置。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.setColumnGroupControlPosition(SpreadsheetApp.GroupControlTogglePosition.AFTER);

参数

名称类型说明
positionGroupControlTogglePosition列组控件切换开关的位置。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setColumnWidth(columnPosition, width)

设置指定列的宽度(以像素为单位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first column to a width of 200 pixels
sheet.setColumnWidth(1, 200);

参数

名称类型说明
columnPositionInteger要设置的指定列的位置。
widthInteger要设置的宽度(以像素为单位)。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

setColumnWidths(startColumn, numColumns, width)

设置指定列的宽度(以像素为单位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first three columns to a width of 200 pixels
sheet.setColumnWidths(1, 3, 200);

参数

名称类型说明
startColumnInteger要更改的起始列位置。
numColumnsInteger要更改的列数。
widthInteger要设置的宽度(以像素为单位)。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setConditionalFormatRules(rules)

将工作表中当前的所有现有条件格式规则替换为输入规则。 系统会根据规则的输入顺序来评估规则。

// Remove one of the existing conditional format rules.
var sheet = SpreadsheetApp.getActiveSheet();
var rules = sheet.getConditionalFormatRules();
rules.splice(1, 1); // Deletes the 2nd format rule.
sheet.setConditionalFormatRules(rules);

参数

名称类型说明
rulesConditionalFormatRule[]新的条件格式规则。

授权

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

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

setCurrentCell(cell)

将指定单元格设置为 current cell

如果指定单元格存在于已选择的范围内,则该范围会成为活动范围,并且该单元格是当前单元格。

如果指定的单元格不在任何选定范围内,则系统会移除任何现有选择,并且该单元格将成为当前单元格和活动范围。

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

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange('B5');
sheet.setCurrentCell(cell);

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

参数

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

弃踢回攻

Range - 新设置的当前单元格

授权

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

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

setFrozenColumns(columns)

冻结指定数量的列。如果为零,则不会冻结任何列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Freezes the first column
sheet.setFrozenColumns(1);

参数

名称类型说明
columnsInteger要冻结的列数。

授权

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

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

setFrozenRows(rows)

冻结指定的行数。如果为零,则不会冻结任何行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Freezes the first row
sheet.setFrozenRows(1);

参数

名称类型说明
rowsInteger要冻结的行数。

授权

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

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

setHiddenGridlines(hideGridlines)

隐藏或显示工作表网格线。

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

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

// Hides the gridlines in the sheet.
sheet.setHiddenGridlines(true);

参数

名称类型说明
hideGridlinesBoolean如果为 true,则隐藏此工作表中的网格线;否则显示网格线。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setName(name)

设置工作表名称。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.setName("not first anymore");

参数

名称类型说明
nameString工作表的新名称。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setRightToLeft(rightToLeft)

将工作表布局设为从右到左。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Sets the sheet layout, so that the sheet is ordered from right to left.
sheet.setRightToLeft(true);

参数

名称类型说明
rightToLeftBoolean如果为 true,则工作表布局设为从右到左,且单元格 A1 位于右上角。如果为 false,则工作表布局默认为从左到右,且单元格 A1 位于左上角。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setRowGroupControlPosition(position)

设置行组控件切换开关在工作表中的位置。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.setRowGroupControlPosition(SpreadsheetApp.GroupControlTogglePosition.AFTER);

参数

名称类型说明
positionGroupControlTogglePosition行组控件切换开关的位置。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setRowHeight(rowPosition, height)

设置指定行的行高(以像素为单位)。默认情况下,行会根据单元格内容进行缩放。如果要将行强制设置为指定高度,请使用 setRowHeightsForced(startRow, numRows, height)

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first row to a height of 200 pixels
sheet.setRowHeight(1, 200);

参数

名称类型说明
rowPositionInteger要更改的行位置。
heightInteger要设置的高度(以像素为单位)。

弃踢回攻

Sheet - 工作表,可用于方法链接。

授权

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

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

setRowHeights(startRow, numRows, height)

设置指定行的高度(以像素为单位)。默认情况下,行会根据单元格内容进行缩放。如果要将行强制设置为指定高度,请使用 setRowHeightsForced(startRow, numRows, height)

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first three rows to a height of 20 pixels
sheet.setRowHeights(1, 3, 20);

参数

名称类型说明
startRowInteger要更改的起始行位置。
numRowsInteger要更改的行数。
heightInteger要设置的高度(以像素为单位)。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setRowHeightsForced(startRow, numRows, height)

设置指定行的高度(以像素为单位)。默认情况下,行会根据单元格内容进行缩放。使用 setRowHeightsForced 时,即使单元格内容的高度超过行高,系统也会强制将行的高度设置为指定的高度。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first three rows to a height of 5 pixels.
sheet.setRowHeightsForced(1, 3, 5);

参数

名称类型说明
startRowInteger要更改的起始行位置。
numRowsInteger要更改的行数。
heightInteger要设置的高度(以像素为单位)。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setTabColor(color)

设置工作表标签页颜色。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.setTabColor("ff0000"); // Set the color to red.
first.setTabColor(null); // Unset the color.

参数

名称类型说明
colorStringCSS 表示法的颜色代码(如 '#ffffff''white'),或用于重置标签页颜色的 null

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

setTabColorObject(color)

设置工作表标签页颜色。

// This example assumes there is a sheet named "Sheet1"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Sheet1");
var color = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
first.setTabColorObject(color); // Set the color to theme accent 1.
first.setTabColorObject(null); // Unset the color.

参数

名称类型说明
colorColor要设置的工作表标签颜色。

弃踢回攻

Sheet - 此工作表,用于串联。

授权

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

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

showColumns(columnIndex)

取消隐藏位于指定索引的列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first column
sheet.showColumns(1);

参数

名称类型说明
columnIndexInteger要取消隐藏的列的索引。

授权

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

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

showColumns(columnIndex, numColumns)

取消隐藏从给定索引开始的一个或多个连续列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first three columns
sheet.showColumns(1, 3);

参数

名称类型说明
columnIndexInteger要取消隐藏的列的起始索引。
numColumnsInteger要取消隐藏的列数。

授权

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

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

showRows(rowIndex)

取消隐藏位于给定索引的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first row
sheet.showRows(1);

参数

名称类型说明
rowIndexInteger要取消隐藏的行的索引。

授权

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

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

showRows(rowIndex, numRows)

取消隐藏从指定索引开始的一行或多行连续行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first three rows
sheet.showRows(1, 3);

参数

名称类型说明
rowIndexInteger要取消隐藏的行的起始索引。
numRowsInteger要取消隐藏的行数。

授权

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

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

showSheet()

显示工作表。如果工作表已显示,则不会产生任何影响。

var sheet = SpreadsheetApp.getActiveSheet();
sheet.showSheet();

弃踢回攻

Sheet - 当前工作表。

授权

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

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

sort(columnPosition)

按列对工作表进行升序排序。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the first column, ascending
sheet.sort(1);

参数

名称类型说明
columnPositionInteger要排序的列。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

sort(columnPosition, ascending)

按列对工作表进行排序。接受一个参数来指定升序或降序。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the first column, descending
sheet.sort(1, false);

参数

名称类型说明
columnPositionInteger要排序的列。
ascendingBooleantrue 表示升序,false 表示降序。

弃踢回攻

Sheet - 工作表,可用于方法链接

授权

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

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

unhideColumn(column)

取消隐藏给定范围内的列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This unhides the first column if it was previously hidden
var range = sheet.getRange("A1");
sheet.unhideColumn(range);

参数

名称类型说明
columnRange要取消隐藏的范围(如果已隐藏)。

授权

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

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

unhideRow(row)

取消隐藏给定范围内的行。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This unhides the first row if it was previously hidden
var range = sheet.getRange("A1");
sheet.unhideRow(range);

参数

名称类型说明
rowRange要取消隐藏的范围(如果已隐藏)。

授权

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

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

updateChart(chart)

更新此工作表中的图表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This code is going to loop through all the charts and change them to
// column charts
var charts = sheet.getCharts();
for (var i in charts) {
  var chart = charts[i];
  var newChart = chart
      .modify()
      .setChartType(Charts.ChartType.COLUMN)
      .build();
  sheet.updateChart(newChart);
}

参数

名称类型说明
chartEmbeddedChart要更新的图表。

授权

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

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

已废弃的方法