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)OverGridImage在文档中的指定行和列插入 Spreadsheet 作为图片。
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)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' 中的任意一项也有效。

返程

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

获取所有数据源公式。

// 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 可以从其网址中提取。例如,网址 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()

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

// 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 是一个单调递增的整数,在工作表创建时分配,与工作表位置无关。此方法与会接受 gridId 参数(而非 Sheet 实例)的 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等方法一起使用非常有用。

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

Logger.log(sheet.getSheetId());

返程

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

授权

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

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

getSheetName()

返回工作表名称。

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

Logger.log(sheet.getSheetName());

返程

String - 工作表的名称

授权

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

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

getSheetValues(startRow, startColumn, numRows, numColumns)

返回此范围中以给定坐标为起点的矩形网格。- 指定 -1 值作为行或列位置相当于获取最后一张在表格中具有数据的行或列。

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

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

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

参数

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

返程

Object[][] - 一个二维值数组

授权

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

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

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 - 以“长”格式指定的时区(例如,“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() 方法为特定数据源类型启用数据执行。

// 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 内容中检索图片大小。支持的 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新的活动工作表。
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。

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

参数

名称类型说明
isEnabledBooleantrue(如果应启用迭代计算);否则启用 false

返程

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以“长”格式指定的时区(例如“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

已废弃的方法