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 表格界面中具有焦点的单元格,并用深色边框突出显示。当前单元格数量永远不会超过 1 个。当用户选择一个或多个单元格范围时,所选范围内的某个单元格就是当前单元格。

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

弃踢回攻

Range - 当前单元格

授权

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

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

getDataRange()

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

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

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

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

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

弃踢回攻

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

授权

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

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

getDataSourceFormulas()

获取所有数据源公式。

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

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

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

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

弃踢回攻

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

授权

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

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

getDataSourcePivotTables()

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

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

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

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

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

弃踢回攻

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

授权

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

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

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 中的电子表格 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 - 时区,以“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 内容中检索图片大小。支持的 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 大小上限为 2 MB。

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

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

参数

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

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertImage(url, column, row)

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

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

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

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

参数

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

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

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

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

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

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

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

参数

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

弃踢回攻

OverGridImage - 插入的图片。

授权

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

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

insertRowAfter(afterPosition)

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

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

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

参数

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

弃踢回攻

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

授权

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

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

insertRowBefore(beforePosition)

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

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

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

参数

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

弃踢回攻

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

授权

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

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

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如果应启用迭代计算,则为 true;否则为 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时区,以“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

已废弃的方法