Class Spreadsheet

电子表格

访问和修改 Google 表格文件。常见的操作是添加新工作表和添加协作者。

方法

方法返回类型简介
addDeveloperMetadata(key)Spreadsheet将具有指定键的开发者元数据添加到顶级电子表格。
addDeveloperMetadata(key, visibility)Spreadsheet向电子表格添加具有指定键和可见性的开发者元数据。
addDeveloperMetadata(key, value)Spreadsheet将具有指定键的开发者元数据添加到电子表格中。
addDeveloperMetadata(key, value, visibility)Spreadsheet向电子表格添加具有指定键、值和可见性的开发者元数据。
addEditor(emailAddress)Spreadsheet将指定用户添加到 Spreadsheet 的编辑器列表中。
addEditor(user)Spreadsheet将指定用户添加到 Spreadsheet 的编辑器列表中。
addEditors(emailAddresses)Spreadsheet将指定用户数组添加到 Spreadsheet 的编辑器列表中。
addMenu(name, subMenus)void在电子表格界面中创建新菜单。
addViewer(emailAddress)Spreadsheet将指定用户添加到 Spreadsheet 的查看者列表中。
addViewer(user)Spreadsheet将指定用户添加到 Spreadsheet 的查看者列表中。
addViewers(emailAddresses)Spreadsheet将指定用户数组添加到 Spreadsheet 的查看者列表中。
appendRow(rowContents)Sheet向工作表中当前数据区域的底部附加一行。
autoResizeColumn(columnPosition)Sheet设置指定列的宽度,以适合其内容。
copy(name)Spreadsheet复制电子表格并返回新电子表格。
createDeveloperMetadataFinder()DeveloperMetadataFinder返回 DeveloperMetadataFinder,以在该电子表格的范围内查找开发者元数据。
createTextFinder(findText)TextFinder为电子表格创建文本查找器,此查找器可用于查找和替换电子表格中的文本。
deleteActiveSheet()Sheet删除当前活跃的工作表。
deleteColumn(columnPosition)Sheet删除指定列位置处的列。
deleteColumns(columnPosition, howMany)void删除从指定列位置开始的多个列。
deleteRow(rowPosition)Sheet删除指定行位置处的行。
deleteRows(rowPosition, howMany)void删除从指定行位置开始的若干行。
deleteSheet(sheet)void删除指定的工作表。
duplicateActiveSheet()Sheet复制有效工作表,并将其设为有效工作表。
getActiveCell()Range返回此工作表中的活动单元格。
getActiveRange()Range返回活动工作表中的所选范围,如果没有活动范围,则返回 null
getActiveRangeList()RangeList返回活跃工作表中的有效范围列表;如果没有有效范围,则返回 null
getActiveSheet()Sheet获取电子表格中的活动工作表。
getAs(contentType)Blob以转换为指定内容类型的 blob 的形式返回此对象内的数据。
getBandings()Banding[]返回此电子表格中的所有条带。
getBlob()Blob以 blob 形式返回此对象内的数据。
getColumnWidth(columnPosition)Integer获取指定列的宽度(以像素为单位)。
getCurrentCell()Range返回活动工作表中的当前单元格;如果没有当前单元格,则返回 null
getDataRange()Range返回与存在数据的维度相对应的 Range
getDataSourceFormulas()DataSourceFormula[]获取所有数据源公式。
getDataSourcePivotTables()DataSourcePivotTable[]获取所有数据源数据透视表。
getDataSourceRefreshSchedules()DataSourceRefreshSchedule[]获取此电子表格的刷新时间表。
getDataSourceSheets()DataSourceSheet[]返回电子表格中的所有数据源工作表。
getDataSourceTables()DataSourceTable[]获取所有数据源表。
getDataSources()DataSource[]返回电子表格中的所有数据源。
getDeveloperMetadata()DeveloperMetadata[]获取与顶级电子表格关联的开发者元数据。
getEditors()User[]获取此 Spreadsheet 的编辑器列表。
getFormUrl()String返回将回复发送到此电子表格的表单的网址,如果此电子表格没有关联的表单,则返回 null
getFrozenColumns()Integer返回冻结列的数量。
getFrozenRows()Integer返回冻结的行数。
getId()String获取此电子表格的唯一标识符。
getImages()OverGridImage[]返回工作表上的所有网格图片。
getIterativeCalculationConvergenceThreshold()Number返回迭代计算期间使用的阈值。
getLastColumn()Integer返回最后一个包含内容的列的位置。
getLastRow()Integer返回包含内容的最后一行的位置。
getMaxIterativeCalculationCycles()Integer返回迭代计算期间可以使用的迭代次数上限。
getName()String获取文档的名称。
getNamedRanges()NamedRange[]获取此电子表格中的所有命名范围。
getNumSheets()Integer返回此电子表格中的工作表数量。
getOwner()User返回文档的所有者,对于共享云端硬盘中的文档,返回 null
getPredefinedSpreadsheetThemes()SpreadsheetTheme[]返回预定义主题列表。
getProtections(type)Protection[]获取代表电子表格中所有受保护范围或工作表的对象数组。
getRange(a1Notation)Range返回采用 A1 表示法或 R1C1 表示法指定的范围。
getRangeByName(name)Range返回已命名的范围;如果未找到具有指定名称的范围,则返回 null
getRangeList(a1Notations)RangeList返回 RangeList 集合,该集合表示同一工作表中由非空值 A1 表示法或 R1C1 表示法指定的范围。
getRecalculationInterval()RecalculationInterval返回此电子表格的计算时间间隔。
getRowHeight(rowPosition)Integer获取指定行的高度(以像素为单位)。
getSelection()Selection返回电子表格中的当前 Selection
getSheetByName(name)Sheet返回具有指定名称的工作表。
getSheetId()Integer返回此对象表示的工作表的 ID。
getSheetName()String返回工作表名称。
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]用于返回此范围值的矩形网格,从指定坐标开始。
getSheets()Sheet[]获取此电子表格中的所有工作表。
getSpreadsheetLocale()String获取电子表格语言区域。
getSpreadsheetTheme()SpreadsheetTheme返回电子表格的当前主题,如果未应用任何主题,则返回 null
getSpreadsheetTimeZone()String获取电子表格的时区。
getUrl()String返回给定电子表格的网址。
getViewers()User[]获取此 Spreadsheet 的查看者和评论者列表。
hideColumn(column)void隐藏指定范围内的一列或多列。
hideRow(row)void隐藏指定范围内的行。
insertColumnAfter(afterPosition)Sheet在指定的列位置后插入列。
insertColumnBefore(beforePosition)Sheet在指定列位置前插入列。
insertColumnsAfter(afterPosition, howMany)Sheet在给定列位置后插入若干列。
insertColumnsBefore(beforePosition, howMany)Sheet在指定的列位置前插入多列。
insertDataSourceSheet(spec)DataSourceSheet在电子表格中插入新的 DataSourceSheet 并开始数据执行。
insertImage(blobSource, column, row)OverGridImageSpreadsheet 作为图片插入文档中指定行和列。
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImage以图片的形式在给定行和列插入具有像素偏移量的 Spreadsheet
insertImage(url, column, row)OverGridImage在文档中指定行和列插入图片。
insertImage(url, column, row, offsetX, offsetY)OverGridImage在文档中按指定行和列插入图片,并设定像素偏移量。
insertRowAfter(afterPosition)Sheet在给定行位置后插入行。
insertRowBefore(beforePosition)Sheet在给定行位置前插入行。
insertRowsAfter(afterPosition, howMany)Sheet在给定行位置之后插入若干行。
insertRowsBefore(beforePosition, howMany)Sheet在给定行位置前插入多行。
insertSheet()Sheet使用默认工作表名称在电子表格中插入新工作表。
insertSheet(sheetIndex)Sheet根据指定索引在电子表格中插入新的工作表。
insertSheet(sheetIndex, options)Sheet使用指定的索引在电子表格中插入新的工作表,并使用可选的高级参数。
insertSheet(options)Sheet使用默认工作表名称和可选的高级参数在电子表格中插入新的工作表。
insertSheet(sheetName)Sheet使用指定名称在电子表格中插入新工作表。
insertSheet(sheetName, sheetIndex)Sheet将具有指定名称且位于指定索引的电子表格插入电子表格。
insertSheet(sheetName, sheetIndex, options)Sheet将具有指定名称且位于指定索引的新工作表插入电子表格,并使用可选的高级参数。
insertSheet(sheetName, options)Sheet使用给定名称在电子表格中插入新的工作表,并使用可选的高级参数。
insertSheetWithDataSourceTable(spec)Sheet在电子表格中插入新的工作表,创建跨越给定数据源规范的整个工作表的 DataSourceTable,然后开始执行数据。
isColumnHiddenByUser(columnPosition)Boolean返回指定列是否被用户隐藏。
isIterativeCalculationEnabled()Boolean返回此电子表格中是否启用迭代计算。
isRowHiddenByFilter(rowPosition)Boolean返回给定行是否被过滤器(而非过滤视图)隐藏。
isRowHiddenByUser(rowPosition)Boolean返回指定行是否被用户隐藏。
moveActiveSheet(pos)void将当前工作表移至工作表列表中的指定位置。
moveChartToObjectSheet(chart)Sheet创建新的 SheetType.OBJECT 工作表,并将提供的图表移至其中。
refreshAllDataSources()void刷新所有受支持的数据源及其关联的数据源对象,跳过无效的数据源对象。
removeEditor(emailAddress)SpreadsheetSpreadsheet 的编辑器列表中移除给定用户。
removeEditor(user)SpreadsheetSpreadsheet 的编辑器列表中移除给定用户。
removeMenu(name)void移除 addMenu(name, subMenus) 添加的菜单。
removeNamedRange(name)void删除具有指定名称的命名范围。
removeViewer(emailAddress)Spreadsheet将指定用户从 Spreadsheet 的查看者和评论者列表中移除。
removeViewer(user)Spreadsheet将指定用户从 Spreadsheet 的查看者和评论者列表中移除。
rename(newName)void重命名文档。
renameActiveSheet(newName)void将当前活动工作表重命名为指定的新名称。
resetSpreadsheetTheme()SpreadsheetTheme移除已应用的主题并在电子表格中设置默认主题。
setActiveRange(range)Range将指定范围设为活动工作表中的 active range,将范围中的左上角单元格设为 current cell
setActiveRangeList(rangeList)RangeList将指定的范围列表设置为活动工作表中的 active ranges
setActiveSelection(range)Range设置此工作表的有效选择区域。
setActiveSelection(a1Notation)Range设置有效的选择,如 A1 或 R1C1 表示法中所述。
setActiveSheet(sheet)Sheet将给定工作表设置为电子表格中的活动工作表。
setActiveSheet(sheet, restoreSelection)Sheet将给定的工作表设置为电子表格中的活动工作表,并可以选择恢复该工作表中的最新选择。
setColumnWidth(columnPosition, width)Sheet设置指定列的宽度(以像素为单位)。
setCurrentCell(cell)Range将指定的单元格设置为 current cell
setFrozenColumns(columns)void冻结给定列数。
setFrozenRows(rows)void冻结给定行数。
setIterativeCalculationConvergenceThreshold(minThreshold)Spreadsheet设置迭代计算的最小阈值。
setIterativeCalculationEnabled(isEnabled)Spreadsheet设置是否在此电子表格中启用迭代计算。
setMaxIterativeCalculationCycles(maxIterations)Spreadsheet设置应在迭代计算期间执行的计算迭代次数上限。
setNamedRange(name, range)void为范围命名。
setRecalculationInterval(recalculationInterval)Spreadsheet设置此电子表格的重新计算频率。
setRowHeight(rowPosition, height)Sheet设置指定行的行高(以像素为单位)。
setSpreadsheetLocale(locale)void设置电子表格的语言区域。
setSpreadsheetTheme(theme)SpreadsheetTheme在电子表格中设置主题背景。
setSpreadsheetTimeZone(timezone)void设置电子表格的时区。
show(userInterface)void在以用户浏览器视口为中心的对话框中显示自定义界面组件。
sort(columnPosition)Sheet按列升序对工作表进行排序。
sort(columnPosition, ascending)Sheet按列对工作表进行排序。
toast(msg)void在电子表格的右下角显示一个弹出式窗口,其中包含特定消息。
toast(msg, title)void在电子表格的右下角显示带有给定消息和标题的弹出式窗口。
toast(msg, title, timeoutSeconds)void在电子表格的右下角显示一个弹出式窗口,其中包含给定标题和消息,并在一段时间内显示。
unhideColumn(column)void取消隐藏指定范围内的列。
unhideRow(row)void取消隐藏指定范围内的行。
updateMenu(name, subMenus)void更新 addMenu(name, subMenus) 添加的菜单。
waitForAllDataExecutionsCompletion(timeoutInSeconds)void等待电子表格中所有当前执行完成,并在提供的秒数后超时。

详细文档

addDeveloperMetadata(key)

将具有指定键的开发者元数据添加到顶级电子表格。

参数

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

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

addDeveloperMetadata(key, visibility)

向电子表格添加具有指定键和可见性的开发者元数据。

参数

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

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

addDeveloperMetadata(key, value)

将具有指定键的开发者元数据添加到电子表格中。

参数

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

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

addDeveloperMetadata(key, value, visibility)

向电子表格添加具有指定键、值和可见性的开发者元数据。

参数

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

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

addEditor(emailAddress)

将指定用户添加到 Spreadsheet 的编辑器列表中。如果用户已位于查看者列表中,则此方法会将该用户从查看者列表中升级。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

addEditor(user)

将指定用户添加到 Spreadsheet 的编辑器列表中。如果用户已位于查看者列表中,则此方法会将该用户从查看者列表中升级。

参数

名称类型说明
userUser要添加的用户的表示形式。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

addEditors(emailAddresses)

将指定用户数组添加到 Spreadsheet 的编辑器列表中。如果其中有任何用户已经在查看者列表中,此方法会将他们从查看者列表中升级。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

addMenu(name, subMenus)

在电子表格界面中创建新菜单。

每个菜单项都运行一个用户定义的函数。通常情况下,您需要从 onOpen() 函数中调用该函数,以便在电子表格加载后自动创建菜单。

// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [];
  // When the user clicks on "addMenuExample" then "Menu Entry 1", the function function1 is
  // executed.
  menuEntries.push({name: "Menu Entry 1", functionName: "function1"});
  menuEntries.push(null); // line separator
  menuEntries.push({name: "Menu Entry 2", functionName: "function2"});

  ss.addMenu("addMenuExample", menuEntries);
}

参数

名称类型说明
nameString要创建的菜单的名称。
subMenusObject[]一个包含 namefunctionName 参数的 JavaScript 映射数组。您可以使用所含库中的函数,例如 Library.libFunction1

授权

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

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

addViewer(emailAddress)

将指定用户添加到 Spreadsheet 的查看者列表中。如果用户已经在编辑器列表中,则此方法不会产生任何影响。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

addViewer(user)

将指定用户添加到 Spreadsheet 的查看者列表中。如果用户已经在编辑器列表中,则此方法不会产生任何影响。

参数

名称类型说明
userUser要添加的用户的表示形式。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

addViewers(emailAddresses)

将指定用户数组添加到 Spreadsheet 的查看者列表中。如果任何用户已经在编辑器列表中,此方法对他们没有影响。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

  • 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

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

copy(name)

复制电子表格并返回新电子表格。

// This code makes a copy of the current spreadsheet and names it appropriately
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.copy("Copy of " + ss.getName());

参数

名称类型说明
nameString副本的名称。

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

createDeveloperMetadataFinder()

返回 DeveloperMetadataFinder,以在该电子表格的范围内查找开发者元数据。默认情况下,系统会考虑与电子表格、工作表、行和列关联的所有元数据。

返回

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


createTextFinder(findText)

为电子表格创建文本查找器,此查找器可用于查找和替换电子表格中的文本。从电子表格的第一页开始搜索。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

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

// Returns the first occurrence of 'dog' in the spreadsheet.
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


deleteActiveSheet()

删除当前有效的工作表。

// The code below deletes the currently active sheet and stores the new active sheet in a
// variable
var newSheet = SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();

返回

Sheet - 新的活动工作表。

授权

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

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

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

deleteSheet(sheet)

删除指定的工作表。

// The code below deletes the specified sheet.
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('My Sheet');
ss.deleteSheet(sheet);

参数

名称类型说明
sheetSheet要删除的工作表。

授权

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

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

duplicateActiveSheet()

复制当前工作表,并将其设为当前工作表。

// The code below makes a duplicate of the active sheet
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();

返回

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

另请参阅


getActiveSheet()

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

电子表格中的活动工作表就是电子表格界面中显示的工作表。

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

返回

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


getAs(contentType)

以转换为指定内容类型的 blob 的形式返回此对象内的数据。此方法会在文件名中添加适当的扩展名,例如“myfile.pdf”。不过,其中假定文件名中最后一个句号后面的部分(如果有)是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”将变为“ShoppingList.12.25.pdf”。

如需查看转化次数的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能暂时受到更严格的配额约束。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任何一种也有效。

返回

Blob - 表示 blob 的数据。


getBandings()

返回此电子表格中的所有条带。

返回

Banding[] - 此电子表格中的条纹。

授权

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

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

getBlob()

以 blob 形式返回此对象内的数据。

返回

Blob - 表示 blob 的数据。


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

getCurrentCell()

返回活动工作表中的当前单元格;如果没有当前单元格,则返回 null。当前单元格是在 Google 表格界面中获得焦点的单元格,并以深色边框突出显示。当前单元格不能超过一个。当用户选择一个或多个单元格范围时,所选单元格中的一个是当前单元格。

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()) 限定的 Range。

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()

获取所有数据源公式。

返回

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

授权

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

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

getDataSourcePivotTables()

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

返回

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

授权

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

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

getDataSourceRefreshSchedules()

获取此电子表格的刷新时间表。

返回

DataSourceRefreshSchedule[] - 此电子表格的刷新时间表。

授权

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

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

getDataSourceSheets()

返回电子表格中的所有数据源工作表。

返回

DataSourceSheet[] - 代表所有数据源工作表的数组。

授权

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

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

getDataSourceTables()

获取所有数据源表。

返回

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

授权

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

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

getDataSources()

返回电子表格中的所有数据源。

返回

DataSource[] - 代表所有数据源的数组。

授权

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

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

getDeveloperMetadata()

获取与顶级电子表格关联的开发者元数据。

返回

DeveloperMetadata[] - 与此范围关联的开发者元数据。

授权

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

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

getEditors()

获取此 Spreadsheet 的编辑器列表。

返回

User[] - 具有修改权限的用户数组。

授权

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

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

getFormUrl()

返回将回复发送到此电子表格的表单的网址,如果此电子表格没有关联的表单,则返回 null。如果有多个表单向此电子表格发送回复,则返回的表单网址是不确定的。作为替代方案,可以通过 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

getId()

获取此电子表格的唯一标识符。电子表格 ID 可以从网址中提取。 例如,在网址 https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 中,电子表格 ID 为“abc1234567”。

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

返回

String - 电子表格的唯一 ID(或键)。


getImages()

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

返回

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

授权

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

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

getIterativeCalculationConvergenceThreshold()

返回迭代计算期间使用的阈值。当连续计算的结果差异小于此值时,迭代计算将停止。

返回

Number - 收敛阈值。

授权

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

  • 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

getMaxIterativeCalculationCycles()

返回迭代计算期间可以使用的迭代次数上限。

返回

Integer - 计算迭代次数上限。

授权

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

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

getName()

获取文档的名称。

var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.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.getActiveSpreadsheet().getNamedRanges();
for (var i = 0; i < namedRanges.length; i++) {
  Logger.log(namedRanges[i].getName());
}

返回

NamedRange[] - 表示电子表格中所有命名范围的数组。

授权

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

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

getNumSheets()

返回此电子表格中的工作表数量。

// The code below logs the number of sheets in the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getNumSheets());

返回

Integer - 电子表格中的工作表数量。


getOwner()

返回文档的所有者,对于共享云端硬盘中的文档,返回 null

var ss = SpreadsheetApp.getActiveSpreadsheet();
var owner = ss.getOwner();
Logger.log(owner.getEmail());

返回

User - 文档的所有者,如果文档位于共享云端硬盘中,则为 null

授权

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

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

getPredefinedSpreadsheetThemes()

返回预定义的主题列表。

// The code below returns the list of predefined themes.
var predefinedThemesList = SpreadsheetApp.getActiveSpreadsheet()
                               .getPredefinedSpreadsheetThemes();

返回

SpreadsheetTheme[] - 预定义主题列表。

授权

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

  • 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 ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove all sheet protections in the spreadsheet that the user has permission to edit.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (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(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

getRangeByName(name)

返回已命名的范围;如果未找到具有指定名称的范围,则返回 null。如果电子表格有多个工作表使用相同的范围名称,请指定工作表名称(不带 getRangeByName('TaxRates')getRangeByName('Sheet Name!TaxRates')),但不能添加 getRangeByName('"Sheet Name"!TaxRates')

// Log the number of columns for the range named 'TaxRates' in the active spreadsheet.
var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('TaxRates');
if (range != null) {
  Logger.log(range.getNumColumns());
}

参数

名称类型说明
nameString要获取的范围的名称。

返回

Range - 具有指定名称的单元格范围。


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

getRecalculationInterval()

返回此电子表格的计算时间间隔。

返回

RecalculationInterval - 此电子表格的计算间隔。

授权

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

  • 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

getSheetByName(name)

返回具有指定名称的工作表。

如果多个工作表同名,则返回最左侧的工作表。如果没有给定名称的工作表,则返回 null

// The code below logs the index of a sheet named "Expenses"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
if (sheet != null) {
  Logger.log(sheet.getIndex());
}

参数

名称类型说明
nameString要获取的工作表的名称。

返回

Sheet - 包含指定名称的工作表。


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 或 -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

getSheets()

获取此电子表格中的所有工作表。

// The code below logs the name of the second sheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
if (sheets.length > 1) {
  Logger.log(sheets[1].getName());
}

返回

Sheet[] - 表示电子表格中所有工作表的数组。


getSpreadsheetLocale()

获取电子表格语言区域。

返回

String - 电子表格的语言区域。

授权

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

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

getSpreadsheetTheme()

返回电子表格的当前主题,如果未应用任何主题,则返回 null

// The code below returns the current theme of the spreadsheet.
var currentTheme = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTheme();

返回

SpreadsheetTheme - 当前已应用的主题。

授权

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

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

getSpreadsheetTimeZone()

获取电子表格的时区。

返回

String - 以“长”格式指定的时区(例如“America/New_York”,如 Joda.org 所列)。

授权

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

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

getUrl()

返回给定电子表格的网址。

var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getUrl());

返回

String - 给定电子表格的网址。

授权

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

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

getViewers()

获取此 Spreadsheet 的查看者和评论者列表。

返回

User[] - 拥有查看或评论权限的用户数组。

授权

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

  • 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

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

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

insertColumnsAfter(afterPosition, howMany)

在给定列位置后插入若干列。

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

// This inserts a column in the second column position
sheet.insertColumnsAfter(1);

参数

名称类型说明
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

insertDataSourceSheet(spec)

在电子表格中插入新的 DataSourceSheet 并开始数据执行。但这样做也会使新工作表变为活动工作表。

如果数据源类型未启用,则会抛出异常。使用 SpreadsheetApp#enable...Execution() 方法为特定数据源类型启用数据执行。

参数

名称类型说明
specDataSourceSpec要插入的数据源规范。

返回

DataSourceSheet - 新的数据源工作表。

授权

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

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

insertImage(blobSource, column, row)

Spreadsheet 作为图片插入文档中指定行和列。系统会从 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)

以图片的形式在给定行和列插入具有像素偏移量的 Spreadsheet。从 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, 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

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

insertSheet()

使用默认工作表名称在电子表格中插入新工作表。这样新工作表就会变为活动工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet();

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetIndex)

根据指定索引在电子表格中插入新的工作表。这样新工作表就会成为活动工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);

参数

名称类型说明
sheetIndexInteger新创建的工作表的索引。要在电子表格中插入第一张工作表,请将其设为 0。

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetIndex, options)

使用指定的索引在电子表格中插入新的工作表,并使用可选的高级参数。新工作表会变为当前工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet(1, {template: templateSheet});

参数

名称类型说明
sheetIndexInteger新创建的工作表的索引。要在电子表格中插入第一张工作表,请将其设为 0。
optionsObject可选的 JavaScript 高级参数。

高级参数

名称类型说明
templateSheet系统会将模板工作表中的所有数据复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。

返回

Sheet - 新工作表。

授权

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

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

insertSheet(options)

使用默认工作表名称和可选的高级参数在电子表格中插入新的工作表。新工作表会变为当前工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet({template: templateSheet});

参数

名称类型说明
optionsObject可选 JavaScript 高级参数,如下所示。

高级参数

名称类型说明
templateSheet系统会将模板工作表中的所有数据复制到新工作表中。工作表模板必须是此电子表格对象的工作表之一。

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetName)

使用指定名称在电子表格中插入新工作表。这样新工作表就会成为活动工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet('My New Sheet');

参数

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

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetName, sheetIndex)

将具有指定名称且位于指定索引的电子表格插入电子表格。这样新工作表就会成为活动工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet('My New Sheet', 1);

参数

名称类型说明
sheetNameString新工作表的名称。
sheetIndexInteger新创建的工作表的索引。要在电子表格中插入第一张工作表,请将其设为 0。

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetName, sheetIndex, options)

将具有指定名称且位于指定索引的新工作表插入电子表格,并使用可选的高级参数。新工作表会变为当前工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet('My New Sheet', 1, {template: templateSheet});

参数

名称类型说明
sheetNameString新工作表的名称。
sheetIndexInteger新插入工作表的索引。如要在电子表格中插入第一张工作表,请将其设为 0。
optionsObject可选的 JavaScript 高级参数。

高级参数

名称类型说明
templateSheet系统会将模板工作表中的所有数据复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。

返回

Sheet - 新工作表。

授权

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

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

insertSheet(sheetName, options)

使用给定名称在电子表格中插入新的工作表,并使用可选的高级参数。新工作表会变为当前工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet('My New Sheet', {template: templateSheet});

参数

名称类型说明
sheetNameString新工作表的名称。
optionsObject可选的 JavaScript 高级参数。

高级参数

名称类型说明
templateSheet系统会将模板工作表中的所有数据复制到新工作表中。工作表模板必须是电子表格对象的其中一个工作表。

返回

Sheet - 新工作表。

授权

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

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

insertSheetWithDataSourceTable(spec)

在电子表格中插入新的工作表,创建跨越给定数据源规范的整个工作表的 DataSourceTable,然后开始执行数据。附带一个好处是,将新工作表设为活动工作表。

如果数据源类型未启用,则会抛出异常。使用 SpreadsheetApp#enable...Execution() 方法为特定数据源类型启用数据执行。

参数

名称类型说明
specDataSourceSpec要插入的数据源规范。

返回

Sheet - 新工作表。

授权

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

  • 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

isIterativeCalculationEnabled()

返回此电子表格中是否启用迭代计算。

返回

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

moveActiveSheet(pos)

将当前工作表移至工作表列表中的指定位置。如果位置为负数或大于工作表数量,则会抛出异常。

// This example assumes that there are 2 sheets in the current
// active spreadsheet: one named "first" in position 1 and another named "second"
// in position 2.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Gets the "first" sheet and activates it.
var sheet = spreadsheet.getSheetByName("first").activate();

// Logs 'Current index of sheet: 1'
console.log("Current index of sheet: %s", sheet.getIndex());

spreadsheet.moveActiveSheet(2);

// Logs 'New index of sheet: 2'
console.log("New index of sheet: %s", sheet.getIndex());

参数

名称类型说明
posInteger将活动工作表移至的工作表列表中的 1-index 位置。

授权

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

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

moveChartToObjectSheet(chart)

创建新的 SheetType.OBJECT 工作表,并将提供的图表移至其中。如果图表已经存在于自己的工作表中,系统会返回该工作表,而不创建新工作表。

var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.newChart().setPosition(1, 1, 0, 0).build();
sheet.insertChart(chart);
var objectSheet = SpreadsheetApp.getActive().moveChartToObjectSheet(chart);

参数

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

返回

Sheet - 图表所在的工作表。

授权

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

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

refreshAllDataSources()

刷新所有受支持的数据源及其关联的数据源对象,跳过无效的数据源对象。

使用 SpreadsheetApp#enable...Execution() 方法为特定数据源类型启用数据执行。

授权

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

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

removeEditor(emailAddress)

Spreadsheet 的编辑器列表中移除给定用户。如果用户属于具有常规访问权限的一类用户(例如,与用户的整个网域共享 Spreadsheet,或用户可访问的 Spreadsheet 位于共享云端硬盘中),则此方法不会阻止用户访问 Spreadsheet

对于云端硬盘文件,此操作还会将用户从查看者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

removeEditor(user)

Spreadsheet 的编辑器列表中移除给定用户。如果用户属于具有常规访问权限的一类用户(例如,与用户的整个网域共享 Spreadsheet,或用户可访问的 Spreadsheet 位于共享云端硬盘中),则此方法不会阻止用户访问 Spreadsheet

对于云端硬盘文件,此操作还会将用户从查看者列表中移除。

参数

名称类型说明
userUser要移除的用户的表示形式。

返回

Spreadsheet - 此 Spreadsheet,用于串联。

授权

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

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

removeMenu(name)

移除 addMenu(name, subMenus) 添加的菜单。name 参数的值应与对 addMenu(name, subMenus) 的相应调用相同。

// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu("badMenu", [{name: "remove bad menu", functionName: "removeBadMenu"},
                                       {name: "foo", functionName: "foo"}]);
}
function removeBadMenu() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.removeMenu("badMenu");  // name must match the name used when added the menu
}
function foo(){
// Do nothing
}

参数

名称类型说明
nameString要移除的菜单的名称。

授权

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

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

removeNamedRange(name)

删除具有指定名称的命名范围。如果未在电子表格中找到具有指定名称的范围,则会抛出异常。

// The code below creates a new named range "foo", and then remove it.
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("foo", ss.getActiveRange());
ss.removeNamedRange("foo");

参数

名称类型说明
nameString范围名称。

授权

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

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

removeViewer(emailAddress)

将指定用户从 Spreadsheet 的查看者和评论者列表中移除。如果用户是编辑者,而不是查看者或评论者,则此方法不会产生任何影响。如果用户所属的类别具有常规访问权限(例如,如果与用户的整个网域共享了 Spreadsheet,或者 Spreadsheet 位于用户可以访问的共享云端硬盘中),则此方法也不会阻止用户访问 Spreadsheet

对于云端硬盘文件,此操作还会将用户从编辑者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返回

Spreadsheet - 用于串联的 Spreadsheet

授权

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

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

removeViewer(user)

将指定用户从 Spreadsheet 的查看者和评论者列表中移除。如果用户是编辑者,而非查看者,则此方法无效。如果用户所属的类别具有常规访问权限(例如,如果与用户的整个网域共享了 Spreadsheet,或者 Spreadsheet 位于用户可以访问的共享云端硬盘中),则此方法也不会阻止用户访问 Spreadsheet

对于云端硬盘文件,此操作还会将用户从编辑者列表中移除。

参数

名称类型说明
userUser要移除的用户的表示形式。

返回

Spreadsheet - 用于串联的 Spreadsheet

授权

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

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

rename(newName)

重命名文档。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.rename("This is the new name");

参数

名称类型说明
newNameString文档的新名称。

授权

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

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

renameActiveSheet(newName)

将当前活动工作表重命名为指定的新名称。

// The code below renames the active sheet to "Hello world"
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("Hello world");

参数

名称类型说明
newNameString当前活动工作表的新名称。

授权

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

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

resetSpreadsheetTheme()

移除已应用的主题并在电子表格中设置默认主题。

// The code below applies default theme on the spreadsheet.
SpreadsheetApp.getActiveSpreadsheet().resetSpreadsheetTheme();

返回

SpreadsheetTheme - 默认主题。

授权

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

  • 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

setActiveSheet(sheet)

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

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

参数

名称类型说明
sheetSheet要设置为当前工作表的工作表。

返回

Sheet - 当前工作表。

授权

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

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

setActiveSheet(sheet, restoreSelection)

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

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

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

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

参数

名称类型说明
sheetSheet新的活动工作表。
restoreSelectionBoolean在 Tf true 中,当新的工作表变为活动状态时,最近的活跃工作表将被重新选择;如果处于 false 状态,新工作表会变为活跃状态,而不更改当前选择。

返回

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

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

setIterativeCalculationConvergenceThreshold(minThreshold)

设置迭代计算的最小阈值。当连续计算的结果差异小于此值时,迭代计算将停止。此值必须是非负数,默认值为 0.05。

参数

名称类型说明
minThresholdNumber最小收敛阈值(必须为非负值)。

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

setIterativeCalculationEnabled(isEnabled)

设置是否在此电子表格中启用迭代计算。如果在启用计算功能的情况下之前尚未设置计算周期数上限和收敛阈值,则默认值分别为 50 和 0.05。如果其中任一项已设置,它们将保留之前的值。

参数

名称类型说明
isEnabledBoolean如果应启用迭代计算,则为 true;否则启用 false

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

setMaxIterativeCalculationCycles(maxIterations)

设置应在迭代计算期间执行的计算迭代次数上限。该值必须介于 1 到 10,000(含)之间,默认为 50。

参数

名称类型说明
maxIterationsInteger计算迭代次数上限(介于 1 到 10000 之间)。

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

setNamedRange(name, range)

为范围命名。

// The code below creates a new named range "TaxRates" in the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("TaxRates", SpreadsheetApp.getActiveRange());

参数

名称类型说明
nameString要指定范围的名称。
rangeRange范围规范。

授权

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

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

setRecalculationInterval(recalculationInterval)

设置此电子表格的重新计算频率。

参数

名称类型说明
recalculationIntervalRecalculationInterval新的重新计算时间间隔。

返回

Spreadsheet - 此电子表格用于链接。

授权

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

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

setRowHeight(rowPosition, height)

设置指定行的行高(以像素为单位)。默认情况下,行会增大以适应单元格内容。如果要强制行达到指定的高度,请使用 Sheet.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

setSpreadsheetLocale(locale)

设置电子表格的语言区域。

参数

名称类型说明
localeString要使用的语言区域代码(例如“en”、“fr”或“en_US”)。

授权

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

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

setSpreadsheetTheme(theme)

在电子表格中设置主题背景。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// The code below sets the second predefined theme as the current theme of the spreadsheet.
var predefinedThemesList = spreadsheet.getPredefinedSpreadsheetThemes();
spreadsheet.setSpreadsheetTheme(predefinedThemesList[1]);

参数

名称类型说明
themeSpreadsheetTheme要应用的主题。

返回

SpreadsheetTheme - 新的当前主题。

授权

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

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

setSpreadsheetTimeZone(timezone)

设置电子表格的时区。

参数

名称类型说明
timezoneString以“long”格式指定的时区(例如“Jmera.org”列出的“America/New_York”)。

授权

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

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

show(userInterface)

在以用户浏览器视口为中心的对话框中显示自定义界面组件。服务器端脚本的执行被暂停。如需与服务器端通信,界面组件必须对服务器端脚本进行异步回调。

如果服务器端脚本之前显示过尚未关闭的对话框,则系统会将现有对话框替换为新请求的对话框的界面。

以下代码段在包含指定标题、高度和宽度的对话框中显示一个简单的 HtmlService 应用:

var htmlApp = HtmlService
    .createHtmlOutput('<p>A change of speed, a change of style...</p>')
    .setTitle('My HtmlService Application')
    .setWidth(250)
    .setHeight(300);

SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);

// The script resumes execution immediately after showing the dialog.

参数

名称类型说明
userInterfaceObjectHtmlOutput

授权

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

  • https://www.googleapis.com/auth/script.container.ui

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

toast(msg)

在电子表格的右下角显示包含给定消息的弹出式窗口。

// Show a popup with the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started');

参数

名称类型说明
msgString要在消息框中显示的消息。

授权

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

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

toast(msg, title)

在电子表格的右下角显示包含给定消息和标题的弹出式窗口。

// Show a popup with the title "Status" and the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status');

参数

名称类型说明
msgString要在消息框中显示的消息。
titleString消息框的可选标题。

授权

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

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

toast(msg, title, timeoutSeconds)

在电子表格的右下角显示一个弹出式窗口,其中包含给定标题和消息,并在一段时间内显示。

// Show a 3-second popup with the title "Status" and the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status', 3);

参数

名称类型说明
msgString要在消息框中显示的消息。
titleString消息框的可选标题。
timeoutSecondsNumber以秒为单位的超时;如果为 null,消息框默认为 5 秒;如果为负值,消息框将保持关闭状态。

授权

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

  • 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

updateMenu(name, subMenus)

更新 addMenu(name, subMenus) 添加的菜单。与 addMenu(name, subMenus) 的工作方式完全相同。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Lone Menu Entry", functionName: "function1"});
ss.updateMenu("addMenuExample", menuEntries);

参数

名称类型说明
nameString要更新的菜单的名称。
subMenusObject[]一个包含 namefunctionName 参数的 JavaScript 映射数组。您可以使用所含库中的函数,例如 Library.libFunction1

授权

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

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

waitForAllDataExecutionsCompletion(timeoutInSeconds)

等待电子表格中所有当前执行完成,并在提供的秒数后超时。如果执行超时未在完成时执行,但未取消数据执行,则会抛出异常。

参数

名称类型说明
timeoutInSecondsInteger等待数据执行的时间(以秒为单位)。最大值为 300 秒。

授权

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

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

已废弃的方法