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)SpreadsheetSpreadsheet 的编辑器列表添加给定的一组用户。
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)OverGridImageSpreadsheet 作为图片插入文档中的给定行和列, 像素偏移。
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)SpreadsheetSpreadsheet 的查看者和评论者列表中移除指定用户。
removeViewer(user)SpreadsheetSpreadsheet 的查看者和评论者列表中移除指定用户。
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)

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

// 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');

// Adds the key 'NAME' in the developer metadata for the spreadsheet.
ss.addDeveloperMetadata('NAME')

// Gets the first developer metadata object and logs its key.
const developerMetaData = ss.getDeveloperMetadata()[0]
console.log(developerMetaData.getKey())

参数

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

返回

Spreadsheet - 此电子表格,用于实现链式处理。

授权

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

  • 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');

// Adds the key 'NAME' in the developer metadata for the spreadsheet and sets the visibility
// to the developer project that created the metadata.
ss.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT);

// Gets the first developer metadata object and logs its key and visibility setting.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(`Key: ${developerMetaData.getKey()},
.             Visibility: ${developerMetaData.getVisibility()}`);

参数

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

返回

Spreadsheet - 此电子表格,用于实现链式处理。

授权

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

  • 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');

// Adds the key 'NAME' and sets the value to 'GOOGLE' in the developer metadata for the
// spreadsheet.
ss.addDeveloperMetadata('NAME', 'GOOGLE');

// Gets the first developer metadata object and logs its key and value.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(`Key: ${developerMetaData.getKey()}, Value: ${developerMetaData.getValue()}`);

参数

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

返回

Spreadsheet - 此电子表格,用于实现链式处理。

授权

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

  • 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');

// Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility
// to any developer project with document access.
ss.addDeveloperMetadata('NAME', 'GOOGLE', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT);

// Gets the first developer metadata object and logs its key, value, and visibility setting.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(`Key: ${developerMetaData.getKey()},
             Value: ${developerMetaData.getValue()},
             Visibility: ${developerMetaData.getVisibility()}`);

参数

名称类型说明
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,用于查找以下范围内的开发者元数据: 此电子表格。默认情况下,系统会考虑与电子表格关联的所有元数据, 表格、行和列。

// 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');

// Adds developer metadata to the spreadsheet.
ss.addDeveloperMetadata('NAME', 'CHARLIE');
ss.addDeveloperMetadata('COMPANY', 'EXAMPLE ORGANIZATION');
ss.addDeveloperMetadata('TECHNOLOGY', 'JAVASCRIPT');

// Creates a developer metadata finder.
const developerMetadataFinder = ss.createDeveloperMetadataFinder();

// Finds the developer metadata objects with 'COMPANY' as the key.
const googleMetadataFromSpreadsheet = developerMetadataFinder.withKey('COMPANY').find();

// Gets the first result of developer metadata that has the key 'COMPANY' and logs its value.
console.log(googleMetadataFromSpreadsheet[0].getValue());

返回

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' 中的任何一个也 有效。对于 Google 文档,'text/markdown' 也有效。

返回

Blob - 以 blob 形式表示的数据。


getBandings()

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

// 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 an array of the bandings in the spreadsheet.
const bandings = ss.getBandings();

// Logs the range of the first banding in the spreadsheet to the console.
console.log(bandings[0].getRange().getA1Notation());

返回

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 为边界的 Range, (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

getDataSourceRefreshSchedules()

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

// 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');

// Activates BigQuery operations for the connected spreadsheet.
SpreadsheetApp.enableBigQueryExecution();

// Gets the frequency type of the first referesh schedule in the array.
const frequencyType = ss.getDataSourceRefreshSchedules()[0]
                        .getFrequency()
                        .getFrequencyType()
                        .toString();

// Logs the frequency type to the console.
console.log(frequencyType);

返回

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

授权

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

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

getDataSourceSheets()

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

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

// 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 the first data source sheet in the spreadsheet.
const dataSource = ss.getDataSourceSheets()[0];

// Gets the name of the data source sheet.
console.log(dataSource.asSheet().getName());

返回

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

授权

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

  • 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

getDataSources()

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

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

// 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 the data sources on the spreadsheet.
const dataSources = ss.getDataSources();

// Logs the name of the first column on the first data source.
console.log(dataSources[0].getColumns()[0].getName());

返回

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

授权

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

  • 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 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');

// Adds 'Google' as a key to the spreadsheet metadata.
ss.addDeveloperMetadata('Google');

// Gets the spreadsheet's metadata.
const ssMetadata = ss.getDeveloperMetadata();

// Gets the first set of the spreadsheet's metadata and logs the key to the console.
console.log(ssMetadata[0].getKey());

返回

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() 方法检索。

// 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 the form URL from the spreadsheet.
const formUrl = ss.getFormUrl();

// Logs the form URL to the console.
console.log(formUrl);

返回

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 可从相应网址中提取。 例如,网址中的电子表格 ID https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 对应的是“abc1234567”。

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

返回

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


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

getIterativeCalculationConvergenceThreshold()

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

// 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');

// Sets the iterative calculation convergence threshold for the spreadsheet.
ss.setIterativeCalculationConvergenceThreshold(2);

// Logs the threshold to the console.
console.log(ss.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()

返回迭代计算期间要使用的最大迭代次数。

// 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');

// Sets the max iterative calculation cycles for the spreadsheet.
ss.setMaxIterativeCalculationCycles(10);

// Logs the max iterative calculation cycles to the console.
console.log(ss.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()

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

// 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');

// Logs the calculation interval for the spreadsheet to the console.
console.log(ss.getRecalculationInterval().toString());

返回

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 是 在创建工作表时分配的整数(与工作表位置无关)。本次 在与 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等采用 gridId 参数而非 Sheet 实例的方法结合使用时很有用。

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)

返回此范围的矩形的值网格,从指定坐标开始。A -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()

获取电子表格的语言区域。

// 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 the spreadsheet locale.
const ssLocale = ss.getSpreadsheetLocale();

// Logs the locale to the console.
console.log(ssLocale);

返回

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

获取电子表格的时区。

// 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');

// Sets the time zone of the spreadsheet.
ss.setSpreadsheetTimeZone('America/New_York');

// Gets the time zone of the spreadsheet.
const ssTimeZone = ss.getSpreadsheetTimeZone();

// Logs the time zone to the console.
console.log(ssTimeZone);

返回

String - 时区,以“long”指定格式(例如“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)

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

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

insertDataSourceSheet(spec)

在电子表格中插入新的 DataSourceSheet 并开始数据执行。作为 此操作也会使新工作表成为当前工作表。

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

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// 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');

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

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

参数

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

返回

DataSourceSheet - 新的数据源工作表。

授权

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

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

insertImage(blobSource, column, row)

Spreadsheet 作为图片插入文档中的给定行和列。图片 大小。支持的 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() 方法为特定数据源启用数据执行 类型。

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// 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');

// Adds a sheet and sets cell A1 as the parameter cell.
const parameterCell = ss.insertSheet('parameterSheet').getRange('A1');

// Sets the value of the parameter cell to 'Duke'.
parameterCell.setValue('Duke');

const query = 'select * from `bigquery-public-data`.`ncaa_basketball`.' +
  '`mbb_historical_tournament_games` WHERE win_school_ncaa = @SCHOOL';

// Adds a data source with a query parameter.
// TODO(developer): Update the project ID to your own Google Cloud project ID.
const dataSourceSpec = SpreadsheetApp.newDataSourceSpec()
 .asBigQuery()
 .setProjectId('project-id-1')
 .setRawQuery(query)
 .setParameterFromCell('SCHOOL', 'parameterSheet!A1')
 .build();

// Adds sheets for the data source and data source table to the spreadsheet.
ss.insertSheetWithDataSourceTable(dataSourceSpec);

参数

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

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

// 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');

// Activates iterative calculation on the spreadsheet.
ss.setIterativeCalculationEnabled(true);

// Logs whether iterative calculation is activated for the spreadsheet.
console.log(ss.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() 方法为以下实例启用数据执行: 特定数据源类型。

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// 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 the first data source sheet on the spreadsheet.
const dataSheet = ss.getDataSourceSheets()[0];

// Refreshes all data sources on the spreadsheet.
ss.refreshAllDataSources();

// Logs the last refreshed time of the first data source sheet.
console.log(`Last refresh time: ${dataSheet.getStatus().getLastRefreshedTime()}`);

授权

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

  • 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新的活动工作表。
restoreSelectionBooleanTf 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。

// 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');

// Sets the iterative calculation convergence threshold for the spreadsheet.
ss.setIterativeCalculationConvergenceThreshold(2);

// Logs the threshold to the console.
console.log(ss.getIterativeCalculationConvergenceThreshold());

参数

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

返回

Spreadsheet - 此电子表格,用于实现链式处理。

授权

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

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

setIterativeCalculationEnabled(isEnabled)

设置是否在此电子表格中激活迭代计算。如果 计算周期和收敛阈值,因为在计算计算时 它们会分别设置为 50 和 0.05。如果之前已设置其中任一功能 保留其之前的值。

// 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');

// Activates iterative calculation on the spreadsheet.
ss.setIterativeCalculationEnabled(true);

// Logs whether iterative calculation is activated for the spreadsheet.
console.log(ss.isIterativeCalculationEnabled());

参数

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

返回

Spreadsheet - 此电子表格,用于实现链式处理。

授权

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

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

setMaxIterativeCalculationCycles(maxIterations)

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

// 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');

// Sets the max iterative calculation cycles for the spreadsheet.
ss.setMaxIterativeCalculationCycles(10);

// Logs the max iterative calculation cycles to the console.
console.log(ss.getMaxIterativeCalculationCycles());

参数

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

返回

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)

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

// 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');

// Sets the  calculation interval for the spreadsheet to 'ON_CHANGE'.
const interval = ss.setRecalculationInterval(SpreadsheetApp.RecalculationInterval.ON_CHANGE);

// Logs the calculation interval to the console.
console.log(interval);

参数

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

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

// 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');

// Sets the spreadsheet locale.
ss.setSpreadsheetLocale('fr');

// Gets the spreadsheet locale.
const ssLocale = ss.getSpreadsheetLocale();

// Logs the locale to the console.
console.log(ssLocale);

参数

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

设置电子表格的时区。

// 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');

// Sets the time zone of the spreadsheet.
ss.setSpreadsheetTimeZone('America/New_York');

// Gets the time zone of the spreadsheet.
const ssTimeZone = ss.getSpreadsheetTimeZone();

// Logs the time zone to the console.
console.log(ssTimeZone);

参数

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

授权

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

  • 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

已弃用的方法