Class Sheet

  • TheSheet object in Google Apps Script provides extensive methods for accessing and modifying individual sheets within a spreadsheet.

  • You can perform common operations like activating, hiding/showing, and clearing sheet content and formatting.

  • The object allows for detailed manipulation of sheet structure, including inserting, deleting, and moving columns and rows.

  • It offers methods to get and set metadata, manage ranges and selections, control formatting and display options, work with groups, and interact with embedded objects like charts and images.

  • Many methods require specific authorization scopes to execute successfully.

Sheet

Access and modify spreadsheet sheets. Common operations are renaming a sheet and accessing rangeobjects from the sheet.

Methods

MethodReturn typeBrief description
activate()SheetActivates this sheet.
addDeveloperMetadata(key)SheetAdds developer metadata with the specified key to the sheet.
addDeveloperMetadata(key, visibility)SheetAdds developer metadata with the specified key and visibility to the sheet.
addDeveloperMetadata(key, value)SheetAdds developer metadata with the specified key and value to the sheet.
addDeveloperMetadata(key, value, visibility)SheetAdds developer metadata with the specified key, value, and visibility to the sheet.
appendRow(rowContents)SheetAppends a row to the bottom of the current data region in the sheet.
asDataSourceSheet()DataSourceSheet|nullReturns the sheet as aDataSourceSheet if the sheet is of typeSheetType.DATASOURCE, ornull otherwise.
autoResizeColumn(columnPosition)SheetSets the width of the given column to fit its contents.
autoResizeColumns(startColumn, numColumns)SheetSets the width of all columns starting at the given column position to fit their contents.
autoResizeRows(startRow, numRows)SheetSets the height of all rows starting at the given row position to fit their contents.
clear()SheetClears the sheet of content and formatting information.
clear(options)SheetClears the sheet of contents and/or format, as specified with the given advanced options.
clearConditionalFormatRules()voidRemoves all conditional format rules from the sheet.
clearContents()SheetClears the sheet of contents, while preserving formatting information.
clearFormats()SheetClears the sheet of formatting, while preserving contents.
clearNotes()SheetClears the sheet of all notes.
collapseAllColumnGroups()SheetCollapses all column groups on the sheet.
collapseAllRowGroups()SheetCollapses all row groups on the sheet.
copyTo(spreadsheet)SheetCopies the sheet to a given spreadsheet, which can be the same spreadsheet as the source.
createDeveloperMetadataFinder()DeveloperMetadataFinderReturns aDeveloperMetadataFinder for finding developer metadata within the scope ofthis sheet.
createTextFinder(findText)TextFinderCreates a text finder for the sheet, which can find and replace text within the sheet.
deleteColumn(columnPosition)SheetDeletes the column at the given column position.
deleteColumns(columnPosition, howMany)voidDeletes a number of columns starting at the given column position.
deleteRow(rowPosition)SheetDeletes the row at the given row position.
deleteRows(rowPosition, howMany)voidDeletes a number of rows starting at the given row position.
expandAllColumnGroups()SheetExpands all column groups on the sheet.
expandAllRowGroups()SheetExpands all row groups on the sheet.
expandColumnGroupsUpToDepth(groupDepth)SheetExpands all column groups up to the given depth, and collapses all others.
expandRowGroupsUpToDepth(groupDepth)SheetExpands all row groups up to the given depth, and collapses all others.
getActiveCell()RangeReturns the active cell in this sheet.
getActiveRange()RangeReturns the selected range in the active sheet, ornull if there is no active range.
getActiveRangeList()RangeList|nullReturns the list of active ranges in the active sheet ornull if there are no activeranges.
getBandings()Banding[]Returns all the bandings in this sheet.
getCharts()EmbeddedChart[]Returns an array of charts on this sheet.
getColumnGroup(columnIndex, groupDepth)Group|nullReturns the column group at the given index and group depth.
getColumnGroupControlPosition()GroupControlTogglePositionReturns theGroupControlTogglePosition for all column groups on the sheet.
getColumnGroupDepth(columnIndex)IntegerReturns the group depth of the column at the given index.
getColumnWidth(columnPosition)IntegerGets the width in pixels of the given column.
getConditionalFormatRules()ConditionalFormatRule[]Get all conditional format rules in this sheet.
getCurrentCell()Range|nullReturns the current cell in the active sheet ornull if there is no current cell.
getDataRange()RangeReturns aRange corresponding to the dimensions in which data is present.
getDataSourceFormulas()DataSourceFormula[]Gets all the data source formulas.
getDataSourcePivotTables()DataSourcePivotTable[]Gets all the data source pivot tables.
getDataSourceTables()DataSourceTable[]Gets all the data source tables.
getDeveloperMetadata()DeveloperMetadata[]Get all developer metadata associated with this sheet.
getDrawings()Drawing[]Returns an array of drawings on the sheet.
getFilter()Filter|nullReturns the filter in this sheet, ornull if there is no filter.
getFormUrl()String|nullReturns the URL for the form that sends its responses to this sheet, ornull if thissheet has no associated form.
getFrozenColumns()IntegerReturns the number of frozen columns.
getFrozenRows()IntegerReturns the number of frozen rows.
getImages()OverGridImage[]Returns all over-the-grid images on the sheet.
getIndex()IntegerGets the position of the sheet in its parent spreadsheet.
getLastColumn()IntegerReturns the position of the last column that has content.
getLastRow()IntegerReturns the position of the last row that has content.
getMaxColumns()IntegerReturns the current number of columns in the sheet, regardless of content.
getMaxRows()IntegerReturns the current number of rows in the sheet, regardless of content.
getName()StringReturns the name of the sheet.
getNamedRanges()NamedRange[]Gets all the named ranges in this sheet.
getParent()SpreadsheetReturns theSpreadsheet that contains this sheet.
getPivotTables()PivotTable[]Returns all the pivot tables on this sheet.
getProtections(type)Protection[]Gets an array of objects representing all protected ranges in the sheet, or a single-elementarray representing the protection on the sheet itself.
getRange(row, column)RangeReturns the range with the top left cell at the given coordinates.
getRange(row, column, numRows)RangeReturns the range with the top left cell at the given coordinates, and with the given number ofrows.
getRange(row, column, numRows, numColumns)RangeReturns the range with the top left cell at the given coordinates with the given number of rowsand columns.
getRange(a1Notation)RangeReturns the range as specified in A1 notation or R1C1 notation.
getRangeList(a1Notations)RangeListReturns theRangeList collection representing the ranges in the same sheet specifiedby a non-empty list of A1 notations or R1C1 notations.
getRowGroup(rowIndex, groupDepth)Group|nullReturns the row group at the given index and group depth.
getRowGroupControlPosition()GroupControlTogglePositionReturns theGroupControlTogglePosition for all row groups on the sheet.
getRowGroupDepth(rowIndex)IntegerReturns the group depth of the row at the given index.
getRowHeight(rowPosition)IntegerGets the height in pixels of the given row.
getSelection()SelectionReturns the currentSelection in the spreadsheet.
getSheetId()IntegerReturns the ID of the sheet represented by this object.
getSheetName()StringReturns the sheet name.
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]Returns the rectangular grid of values for this range starting at the given coordinates.
getSlicers()Slicer[]Returns an array of slicers on the sheet.
getTabColorObject()Color|nullGets the sheet tab color, ornull if the sheet tab has no color.
getType()SheetTypeReturns the type of the sheet.
hasHiddenGridlines()BooleanReturnstrue if the sheet's gridlines are hidden; otherwise returnsfalse.
hideColumn(column)voidHides the column or columns in the given range.
hideColumns(columnIndex)voidHides a single column at the given index.
hideColumns(columnIndex, numColumns)voidHides one or more consecutive columns starting at the given index.
hideRow(row)voidHides the rows in the given range.
hideRows(rowIndex)voidHides the row at the given index.
hideRows(rowIndex, numRows)voidHides one or more consecutive rows starting at the given index.
hideSheet()SheetHides this sheet.
insertChart(chart)voidAdds a new chart to this sheet.
insertColumnAfter(afterPosition)SheetInserts a column after the given column position.
insertColumnBefore(beforePosition)SheetInserts a column before the given column position.
insertColumns(columnIndex)voidInserts a blank column in a sheet at the specified location.
insertColumns(columnIndex, numColumns)voidInserts one or more consecutive blank columns in a sheet starting at the specified location.
insertColumnsAfter(afterPosition, howMany)SheetInserts a given number of columns after the given column position.
insertColumnsBefore(beforePosition, howMany)SheetInserts a number of columns before the given column position.
insertImage(blobSource, column, row)OverGridImageInserts aBlobSource as an image in the document at a given row and column.
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImageInserts aBlobSource as an image in the document at a given row and column, with apixel offset.
insertImage(url, column, row)OverGridImageInserts an image in the document at a given row and column.
insertImage(url, column, row, offsetX, offsetY)OverGridImageInserts an image in the document at a given row and column, with a pixel offset.
insertRowAfter(afterPosition)SheetInserts a row after the given row position.
insertRowBefore(beforePosition)SheetInserts a row before the given row position.
insertRows(rowIndex)voidInserts a blank row in a sheet at the specified location.
insertRows(rowIndex, numRows)voidInserts one or more consecutive blank rows in a sheet starting at the specified location.
insertRowsAfter(afterPosition, howMany)SheetInserts a number of rows after the given row position.
insertRowsBefore(beforePosition, howMany)SheetInserts a number of rows before the given row position.
insertSlicer(range, anchorRowPos, anchorColPos)SlicerAdds a new slicer to this sheet.
insertSlicer(range, anchorRowPos, anchorColPos, offsetX, offsetY)SlicerAdds a new slicer to this sheet.
isColumnHiddenByUser(columnPosition)BooleanReturns whether the given column is hidden by the user.
isRightToLeft()BooleanReturnstrue if this sheet layout is right-to-left.
isRowHiddenByFilter(rowPosition)BooleanReturns whether the given row is hidden by a filter (not a filter view).
isRowHiddenByUser(rowPosition)BooleanReturns whether the given row is hidden by the user.
isSheetHidden()BooleanReturnstrue if the sheet is currently hidden.
moveColumns(columnSpec, destinationIndex)voidMoves the columns selected by the given range to the position indicated by thedestinationIndex.
moveRows(rowSpec, destinationIndex)voidMoves the rows selected by the given range to the position indicated by thedestinationIndex.
newChart()EmbeddedChartBuilderReturns a builder to create a new chart for this sheet.
protect()ProtectionCreates an object that can protect the sheet from being edited except by users who havepermission.
removeChart(chart)voidRemoves a chart from the parent sheet.
setActiveRange(range)RangeSets the specified range as theactive range in the active sheet, withthe top left cell in the range as thecurrent cell.
setActiveRangeList(rangeList)RangeListSets the specified list of ranges as theactive ranges in theactive sheet.
setActiveSelection(range)RangeSets the active selection region for this sheet.
setActiveSelection(a1Notation)RangeSets the active selection, as specified in A1 notation or R1C1 notation.
setColumnGroupControlPosition(position)SheetSets the position of the column group control toggle on the sheet.
setColumnWidth(columnPosition, width)SheetSets the width of the given column in pixels.
setColumnWidths(startColumn, numColumns, width)SheetSets the width of the given columns in pixels.
setConditionalFormatRules(rules)voidReplaces all currently existing conditional format rules in the sheet with the input rules.
setCurrentCell(cell)RangeSets the specified cell as thecurrent cell.
setFrozenColumns(columns)voidFreezes the given number of columns.
setFrozenRows(rows)voidFreezes the given number of rows.
setHiddenGridlines(hideGridlines)SheetHides or reveals the sheet gridlines.
setName(name)SheetSets the sheet name.
setRightToLeft(rightToLeft)SheetSets or unsets the sheet layout to right-to-left.
setRowGroupControlPosition(position)SheetSets the position of the row group control toggle on the sheet.
setRowHeight(rowPosition, height)SheetSets the row height of the given row in pixels.
setRowHeights(startRow, numRows, height)SheetSets the height of the given rows in pixels.
setRowHeightsForced(startRow, numRows, height)SheetSets the height of the given rows in pixels.
setTabColor(color)SheetSets the sheet tab color.
setTabColorObject(color)SheetSets the sheet tab color.
showColumns(columnIndex)voidUnhides the column at the given index.
showColumns(columnIndex, numColumns)voidUnhides one or more consecutive columns starting at the given index.
showRows(rowIndex)voidUnhides the row at the given index.
showRows(rowIndex, numRows)voidUnhides one or more consecutive rows starting at the given index.
showSheet()SheetMakes the sheet visible.
sort(columnPosition)SheetSorts a sheet by column, ascending.
sort(columnPosition, ascending)SheetSorts a sheet by column.
unhideColumn(column)voidUnhides the column in the given range.
unhideRow(row)voidUnhides the row in the given range.
updateChart(chart)voidUpdates the chart on this sheet.

Deprecated methods

MethodReturn typeBrief description
getSheetProtection()PageProtectionReturns aPageProtection instance describing the permissions for the current sheet.
getTabColor()String|nullGets the sheet tab color, ornull if the sheet tab has no color.
setSheetProtection(permissions)voidSets the permissions for the current sheet.

Detailed documentation

activate()

Activates this sheet. Does not alter the sheet itself, only the parent's notion of the activesheet.

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

Return

Sheet — The newly active sheet.


addDeveloperMetadata(key)

Adds developer metadata with the specified key to the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds the key 'NAME' to the developer metadata for the sheet.sheet.addDeveloperMetadata('NAME');// Gets the updated metadata info and logs it to the console.console.log(sheet.getDeveloperMetadata()[0].getKey());

Parameters

NameTypeDescription
keyStringThe key for the new developer metadata.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

addDeveloperMetadata(key, visibility)

Adds developer metadata with the specified key and visibility to the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds the key 'NAME' and sets the developer metadata visibility to PROJECT// for the sheet.sheet.addDeveloperMetadata('NAME',SpreadsheetApp.DeveloperMetadataVisibility.PROJECT,);// Gets the updated metadata info and logs it to the console.constdeveloperMetaData=sheet.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());console.log(developerMetaData.getVisibility().toString());

Parameters

NameTypeDescription
keyStringThe key for the new developer metadata.
visibilityDeveloperMetadataVisibilityThe visibility of the new developer metadata.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

addDeveloperMetadata(key, value)

Adds developer metadata with the specified key and value to the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata for// the sheet.sheet.addDeveloperMetadata('COMPANY','TECH');// Gets the updated metadata info and logs it to the console.constdeveloperMetaData=sheet.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());console.log(developerMetaData.getValue());

Parameters

NameTypeDescription
keyStringThe key for the new developer metadata.
valueStringThe value for the new developer metadata.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

addDeveloperMetadata(key, value, visibility)

Adds developer metadata with the specified key, value, and visibility to the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata and// sets the visibility to DOCUMENT for the sheet.sheet.addDeveloperMetadata('COMPANY','TECH',SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT,);// Gets the updated metadata info and logs it to the console.constdeveloperMetaData=sheet.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());console.log(developerMetaData.getValue());console.log(developerMetaData.getVisibility().toString());

Parameters

NameTypeDescription
keyStringThe key for the new developer metadata.
valueStringThe value for the new developer metadata.
visibilityDeveloperMetadataVisibilityThe visibility of the new developer metadata.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

appendRow(rowContents)

Appends a row to the bottom of the current data region in the sheet. If a cell's content beginswith=, it's interpreted as a formula.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=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']);

Parameters

NameTypeDescription
rowContentsObject[]An array of values to insert after the last row in the sheet.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

asDataSourceSheet()

Returns the sheet as aDataSourceSheet if the sheet is of typeSheetType.DATASOURCE, ornull otherwise.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets file, you can useSpreadsheetApp.getActiveSpreadsheet()// instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets the data source sheet value if the sheet is of type// SpreadsheetApp.SheetType.DATASOURCE, otherwise this returns a null value.constdataSourceSheet=sheet.asDataSourceSheet();// Gets the data source sheet value and logs it to the console.console.log(dataSourceSheet);console.log(sheet.getType().toString());

Return

DataSourceSheet|null — A data source sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

autoResizeColumn(columnPosition)

Sets the width of the given column to fit its contents.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=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 textsheet.autoResizeColumn(1);

Parameters

NameTypeDescription
columnPositionIntegerThe position of the given column to resize.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

autoResizeColumns(startColumn, numColumns)

Sets the width of all columns starting at the given column position to fit their contents.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first 15 columns to a width that fits their text.sheet.autoResizeColumns(1,15);

Parameters

NameTypeDescription
startColumnIntegerThe starting column to auto-resize.
numColumnsIntegerThe number of columns to auto-resize.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

autoResizeRows(startRow, numRows)

Sets the height of all rows starting at the given row position to fit their contents.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first 15 rows to a height that fits their text.sheet.autoResizeRows(1,15);

Parameters

NameTypeDescription
startRowIntegerThe starting row to auto-resize.
numRowsIntegerThe number of rows to auto-resize.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clear()

Clears the sheet of content and formatting information.

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

Return

Sheet — The cleared sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clear(options)

Clears the sheet of contents and/or format, as specified with the given advanced options.

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

Parameters

NameTypeDescription
optionsObjectA JavaScript map containing advanced options, listed below.

Advanced parameters

NameTypeDescription
contentsOnlyBooleanWhether to clear the content.
formatOnlyBooleanWhether to clear the format.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clearConditionalFormatRules()

Removes all conditional format rules from the sheet. Equivalent to callingsetConditionalFormatRules(rules) with an empty array as input.

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

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clearContents()

Clears the sheet of contents, while preserving formatting information.

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

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clearFormats()

Clears the sheet of formatting, while preserving contents.

Formatting refers to how data is formatted as allowed by choices under the "Format" menu(ex: bold, italics, conditional formatting) and not width or height of cells.

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

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

clearNotes()

Clears the sheet of all notes.

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

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

collapseAllColumnGroups()

Collapses all column groups on the sheet.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All column groups on the sheet are collapsed.sheet.collapseAllColumnGroups();

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

collapseAllRowGroups()

Collapses all row groups on the sheet.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All row groups on the sheet are collapsed.sheet.collapseAllRowGroups();

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

copyTo(spreadsheet)

Copies the sheet to a given spreadsheet, which can be the same spreadsheet as the source. Thecopied sheet is named "Copy of [original name]".

constsource=SpreadsheetApp.getActiveSpreadsheet();constsheet=source.getSheets()[0];constdestination=SpreadsheetApp.openById('ID_GOES HERE');sheet.copyTo(destination);

Parameters

NameTypeDescription
spreadsheetSpreadsheetThe spreadsheet to copy this sheet to, which can be the same spreadsheet as the source.

Return

Sheet — The new sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

createDeveloperMetadataFinder()

Returns aDeveloperMetadataFinder for finding developer metadata within the scope ofthis sheet. Metadata is in the scope of a particular sheet if it is either associated with thesheet itself, or associated with a row, column, or range on that sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds developer metadata for testing.sheet.addDeveloperMetadata('CITY','PARIS');// Creates the developer metadata finder.constmetadatafinder=sheet.createDeveloperMetadataFinder();// Finds the metadata with value 'PARIS' and displays its key in the console.console.log(metadatafinder.withValue('PARIS').find()[0].getKey());

Return

DeveloperMetadataFinder — A developer metadata finder to search for metadata in the scope of this sheet.


createTextFinder(findText)

Creates a text finder for the sheet, which can find and replace text within the sheet.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// Creates  a text finder.consttextFinder=sheet.createTextFinder('dog');// Returns the first occurrence of 'dog' in the sheet.constfirstOccurrence=textFinder.findNext();// Replaces the last found occurrence of 'dog' with 'cat' and returns the number// of occurrences replaced.constnumOccurrencesReplaced=firstOccurrence.replaceWith('cat');

Parameters

NameTypeDescription
findTextStringThe text to search for.

Return

TextFinder — TheTextFinder for the sheet.


deleteColumn(columnPosition)

Deletes the column at the given column position.

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

Parameters

NameTypeDescription
columnPositionIntegerThe position of the column, starting at 1 for the first column.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

deleteColumns(columnPosition, howMany)

Deletes a number of columns starting at the given column position.

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

Parameters

NameTypeDescription
columnPositionIntegerThe position of the first column to delete.
howManyIntegerThe number of columns to delete.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

deleteRow(rowPosition)

Deletes the row at the given row position.

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

Parameters

NameTypeDescription
rowPositionIntegerThe position of the row, starting at 1 for the first row.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

deleteRows(rowPosition, howMany)

Deletes a number of rows starting at the given row position.

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

Parameters

NameTypeDescription
rowPositionIntegerThe position of the first row to delete.
howManyIntegerThe number of rows to delete.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

expandAllColumnGroups()

Expands all column groups on the sheet. This method requires at least one column group.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All column groups on the sheet are expanded.sheet.expandAllColumnGroups();

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

expandAllRowGroups()

Expands all row groups on the sheet. This method requires at least one row group.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All row groups on the sheet are expanded.sheet.expandAllRowGroups();

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

expandColumnGroupsUpToDepth(groupDepth)

Expands all column groups up to the given depth, and collapses all others.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All column groups of depth 2 and lower are expanded, and groups with depth// 3 and higher are collapsed.sheet.expandColumnGroupsUpToDepth(2);

Parameters

NameTypeDescription
groupDepthIntegerThe group depth up to which to expand the column groups.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

expandRowGroupsUpToDepth(groupDepth)

Expands all row groups up to the given depth, and collapses all others.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// All row groups of depth 2 and lower are expanded, and groups with depth// 3 and higher are collapsed.sheet.expandRowGroupsUpToDepth(2);

Parameters

NameTypeDescription
groupDepthIntegerThe group depth up to which to expand the row groups.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getActiveCell()

Returns the active cell in this sheet.

Note: It's preferable to usegetCurrentCell(), which returns the currenthighlighted cell.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Returns the active cellconstcell=sheet.getActiveCell();

Return

Range — The current active cell.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getActiveRange()

Returns the selected range in the active sheet, ornull if there is no active range. Ifmultiple ranges are selected this method returns only the last selected range.

The term "active range" refers to the range that a user has selected in the active sheet,but in a custom function it refers to the cell being actively recalculated.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();constactiveRange=sheet.getActiveRange();

Return

Range — The active range.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

See also


getActiveRangeList()

Returns the list of active ranges in the active sheet ornull if there are no activeranges.

If there is a single range selected, this behaves as agetActiveRange() call.

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

Return

RangeList|null — The list of active ranges.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

See also


getBandings()

Returns all the bandings in this sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets the banding info for the sheet.constbandings=sheet.getBandings();// Gets info on the bandings' second row color and logs it to the console.for(constbandingofbandings){console.log(banding.getSecondRowColor());}

Return

Banding[] — All the bandings in this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getCharts()

Returns an array of charts on this sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constcharts=sheet.getCharts();for(constiincharts){constchart=charts[i];// Do something with the chart}

Return

EmbeddedChart[] — An array of charts.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getColumnGroup(columnIndex, groupDepth)

Returns the column group at the given index and group depth.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// Returns the group whose control index is at column 2 and has a depth of 1, or// null if the group doesn’t exist.constcolumnGroup=sheet.getColumnGroup(2,1);

Parameters

NameTypeDescription
columnIndexIntegerThe column index of the group control toggle or an index within the group.
groupDepthIntegerThe depth of the group.

Return

Group|null — The column group at the control index and depth, or throws an exception if the group doesn’t exist.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getColumnGroupControlPosition()

Returns theGroupControlTogglePosition for all column groups on the sheet.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// GroupControlTogglePosition.AFTER if the column grouping control toggle is// shown after the group.constcolumnGroupControlPosition=sheet.getColumnGroupControlPosition();

Return

GroupControlTogglePositiontrue if the column grouping control toggle is shown after the group on this sheet andfalse otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getColumnGroupDepth(columnIndex)

Returns the group depth of the column at the given index.

The group depth indicates how many groups overlap with the column. This can range betweenzero and eight.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// 1 if there is a group over columns 1 through 3constgroupDepth=sheet.getColumnGroupDepth(1);

Parameters

NameTypeDescription
columnIndexIntegerThe index of the column.

Return

Integer — The group depth of the column at the given index.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getColumnWidth(columnPosition)

Gets the width in pixels of the given column.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Columns start at 1Logger.log(sheet.getColumnWidth(1));

Parameters

NameTypeDescription
columnPositionIntegerThe position of the column to examine.

Return

Integer — Column width in pixels.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getConditionalFormatRules()

Get all conditional format rules in this sheet.

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

Return

ConditionalFormatRule[] — An array of all rules in the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getCurrentCell()

Returns the current cell in the active sheet ornull if there is no current cell. Thecurrent cell is the cell that has focus in the Google Sheets UI, and is highlighted by a darkborder. There is never more than one current cell. When a user selects one or more cell ranges,one of the cells in the selection is the current cell.

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

Return

Range|null — The current cell.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDataRange()

Returns aRange corresponding to the dimensions in which data is present.

This is functionally equivalent to creating a Range bounded by A1 and(Sheet.getLastColumn(), Sheet.getLastRow()).

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This represents ALL the dataconstrange=sheet.getDataRange();constvalues=range.getValues();// This logs the spreadsheet in CSV format with a trailing commafor(leti=0;i <values.length;i++){letrow='';for(letj=0;j <values[i].length;j++){if(values[i][j]){row=row+values[i][j];}row=`${row},`;}Logger.log(row);}

Return

Range — A range consisting of all the data in the spreadsheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDataSourceFormulas()

Gets all the data source formulas.

// 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.constss=SpreadsheetApp.openById('abc123456');// Gets Sheet1 by its name.constsheet=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'.constdataSourceFormulas=sheet.getDataSourceFormulas();// Logs the first data source formula in the array.console.log(dataSourceFormulas[0].getFormula());

Return

DataSourceFormula[] — A list of data source formulas.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDataSourcePivotTables()

Gets all the data source pivot tables.

// 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.constss=SpreadsheetApp.openById('abc123456');// Gets Sheet1 by its name.constsheet=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'.constdataSourcePivotTables=sheet.getDataSourcePivotTables();// Logs the last time that the first pivot table in the array was refreshed.console.log(dataSourcePivotTables[0].getStatus().getLastRefreshedTime());

Return

DataSourcePivotTable[] — A list of data source pivot tables.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDataSourceTables()

Gets all the data source tables.

// 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.constss=SpreadsheetApp.openById('abc123456');// Gets Sheet1 by its name.constsheet=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'.constdataSourceTables=sheet.getDataSourceTables();// Logs the last completed data execution time on the first data source table.console.log(dataSourceTables[0].getStatus().getLastExecutionTime());

Return

DataSourceTable[] — A list of data source tables.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDeveloperMetadata()

Get all developer metadata associated with this sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Adds developer metadata for testing.sheet.addDeveloperMetadata('CITY','PARIS');// Gets all the developer metadata for the sheet.constdeveloperMetaDataList=sheet.getDeveloperMetadata();// Logs the developer metadata to the console.for(constdeveloperMetaDataofdeveloperMetaDataList){console.log(developerMetaData.getKey());}

Return

DeveloperMetadata[] — The developer metadata associated with this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getDrawings()

Returns an array of drawings on the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets all the drawings from the sheet.constallDrawings=sheet.getDrawings();// Logs the number of drawings present on the sheet.console.log(allDrawings.length);

Return

Drawing[] — The list of drawings on this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getFilter()

Returns the filter in this sheet, ornull if there is no filter.

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

Return

Filter|null — The filter.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getFormUrl()

Returns the URL for the form that sends its responses to this sheet, ornull if thissheet has no associated form. Throws an exception if the user does not have permission to editthe spreadsheet.

constsheet=SpreadsheetApp.getActiveSheet();consturl=sheet.getFormUrl();

Return

String|null — The URL for the form that places its responses in this sheet, ornull if this sheet doesn't have an associated form.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getFrozenColumns()

Returns the number of frozen columns.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];Logger.log('Number of frozen columns: %s',sheet.getFrozenColumns());

Return

Integer — The number of frozen columns.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getFrozenRows()

Returns the number of frozen rows.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];Logger.log('Number of frozen rows: %s',sheet.getFrozenRows());

Return

Integer — The number of frozen rows.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getImages()

Returns all over-the-grid images on the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets Sheet1 by its name.constsheet=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.constimages=sheet.getImages();// For each image, logs the anchor cell in A1 notation.for(constimageofimages){console.log(image.getAnchorCell().getA1Notation());}

Return

OverGridImage[] — An array of over-the-grid images.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getIndex()

Gets the position of the sheet in its parent spreadsheet. Starts at 1.

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

Return

Integer — The position of the sheet in its parent spreadsheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getLastColumn()

Returns the position of the last column that has content.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This logs the value in the very last cell of this sheetconstlastRow=sheet.getLastRow();constlastColumn=sheet.getLastColumn();constlastCell=sheet.getRange(lastRow,lastColumn);Logger.log(lastCell.getValue());

Return

Integer — The last column of the sheet that contains content.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getLastRow()

Returns the position of the last row that has content.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This logs the value in the very last cell of this sheetconstlastRow=sheet.getLastRow();constlastColumn=sheet.getLastColumn();constlastCell=sheet.getRange(lastRow,lastColumn);Logger.log(lastCell.getValue());

Return

Integer — The last row of the sheet that contains content.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getMaxColumns()

Returns the current number of columns in the sheet, regardless of content.

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

Return

Integer — The maximum width of the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getMaxRows()

Returns the current number of rows in the sheet, regardless of content.

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

Return

Integer — The maximum height of the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getName()

Returns the name of the sheet.

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

Return

String — The name of the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getNamedRanges()

Gets all the named ranges in this sheet.

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

Return

NamedRange[] — An array of all the named ranges in the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getParent()

Returns theSpreadsheet that contains this sheet.

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

Return

Spreadsheet — The parent spreadsheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getPivotTables()

Returns all the pivot tables on this sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets all the pivot table info for the sheet.constpivotTables=sheet.getPivotTables();// Logs the pivot tables to the console.for(constpivotTableofpivotTables){console.log(pivotTable.getSourceDataRange().getValues());}

Return

PivotTable[] — The pivot tables on this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getProtections(type)

Gets an array of objects representing all protected ranges in the sheet, or a single-elementarray representing the protection on the sheet itself.

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

Parameters

NameTypeDescription
typeProtectionTypeThe type of protected area, eitherSpreadsheetApp.ProtectionType.RANGE orSpreadsheetApp.ProtectionType.SHEET.

Return

Protection[] — An array of objects representing all protected ranges in the sheet, or a single-element array representing the protection on the sheet itself.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRange(row, column)

Returns the range with the top left cell at the given coordinates.

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

Parameters

NameTypeDescription
rowIntegerThe row index of the cell to return; row indexing starts with 1.
columnIntegerThe column index of the cell to return; column indexing starts with 1.

Return

Range — A range containing only this cell.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRange(row, column, numRows)

Returns the range with the top left cell at the given coordinates, and with the given number ofrows.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// When the "numRows" argument is used, only a single column of data is// returned.constrange=sheet.getRange(1,1,3);constvalues=range.getValues();// Prints 3 values from the first column, starting from row 1.for(constrowinvalues){for(constcolinvalues[row]){Logger.log(values[row][col]);}}

Parameters

NameTypeDescription
rowIntegerThe starting row index of the range; row indexing starts with 1.
columnIntegerThe column index of the range; column indexing starts with 1.
numRowsIntegerThe number of rows to return.

Return

Range — A range containing a single column of data with the number of rows specified.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRange(row, column, numRows, numColumns)

Returns the range with the top left cell at the given coordinates with the given number of rowsand columns.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange(1,1,3,3);constvalues=range.getValues();// Print values from a 3x3 box.for(constrowinvalues){for(constcolinvalues[row]){Logger.log(values[row][col]);}}

Parameters

NameTypeDescription
rowIntegerThe starting row index of the range; row indexing starts with 1.
columnIntegerThe starting column index of the range; column indexing starts with 1.
numRowsIntegerThe number of rows to return.
numColumnsIntegerThe number of columns to return.

Return

Range — A range corresponding to the area specified.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRange(a1Notation)

Returns the range as specified in A1 notation or R1C1 notation.

// Get a range A1:D4 on sheet titled "Invoices"constss=SpreadsheetApp.getActiveSpreadsheet();constrange=ss.getRange('Invoices!A1:D4');// Get cell A1 on the first sheetconstsheet=ss.getSheets()[0];constcell=sheet.getRange('A1');

Parameters

NameTypeDescription
a1NotationStringThe range to return, as specified in A1 notation or R1C1 notation.

Return

Range — The range at the location designated.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRangeList(a1Notations)

Returns theRangeList collection representing the ranges in the same sheet specifiedby a non-empty list of A1 notations or R1C1 notations.

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

Parameters

NameTypeDescription
a1NotationsString[]The list of ranges to return, as specified in A1 notation or R1C1 notation.

Return

RangeList — The range list at the location designated.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRowGroup(rowIndex, groupDepth)

Returns the row group at the given index and group depth.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// Returns the group whose control index is at row 2 and has a depth of 1, or// null if the group doesn’t exist.constrowGroup=sheet.getRowGroup(2,1);

Parameters

NameTypeDescription
rowIndexIntegerThe row index of the group control toggle or an index within the group.
groupDepthIntegerThe depth of the group.

Return

Group|null — The row group at the control index and depth, or throws an exception if the group doesn’t exist.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRowGroupControlPosition()

Returns theGroupControlTogglePosition for all row groups on the sheet.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// GroupControlTogglePosition.AFTER if the row grouping control toggle is shown// after the group.constrowGroupControlPosition=sheet.getRowGroupControlPosition();

Return

GroupControlTogglePositiontrue if the row grouping control toggle is shown after the group on this sheet andfalse otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRowGroupDepth(rowIndex)

Returns the group depth of the row at the given index.

The group depth indicates how many groups overlap with the row. This can range between zeroand eight.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// 1 if there is a group over rows 1 through 3constgroupDepth=sheet.getRowGroupDepth(1);

Parameters

NameTypeDescription
rowIndexIntegerThe index of the row.

Return

Integer — The group depth of the row at the given index.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getRowHeight(rowPosition)

Gets the height in pixels of the given row.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Rows start at 1Logger.log(sheet.getRowHeight(1));

Parameters

NameTypeDescription
rowPositionIntegerThe position of the row to examine.

Return

Integer — Row height in pixels.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getSelection()

Returns the currentSelection in the spreadsheet.

constselection=SpreadsheetApp.getActiveSpreadsheet().getSelection();constcurrentCell=selection.getCurrentCell();

Return

Selection — The current selection.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getSheetId()

Returns the ID of the sheet represented by this object.

This is an ID for the sheet that is unique to the spreadsheet. The ID is a monotonicallyincreasing integer assigned at sheet creation time that is independent of sheet position. Thisis useful in conjunction with methods such asRange.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) that take agridId parameter rather than aSheet instance.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];Logger.log(sheet.getSheetId());

Return

Integer — An ID for the sheet unique to the spreadsheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getSheetName()

Returns the sheet name.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];Logger.log(sheet.getSheetName());

Return

String — The name of the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getSheetValues(startRow, startColumn, numRows, numColumns)

Returns the rectangular grid of values for this range starting at the given coordinates. A -1value given as the row or column position is equivalent to getting the very last row or columnthat has data in the sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// The two samples below produce the same outputletvalues=sheet.getSheetValues(1,1,3,3);Logger.log(values);constrange=sheet.getRange(1,1,3,3);values=range.getValues();Logger.log(values);

Parameters

NameTypeDescription
startRowIntegerThe position of the starting row.
startColumnIntegerThe position of the starting column.
numRowsIntegerThe number of rows to return values for.
numColumnsIntegerThe number of columns to return values for.

Return

Object[][] — A two-dimensional array of values.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getSlicers()

Returns an array of slicers on the sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets all slicers in the spreadsheet.constslicers=sheet.getSlicers();// Logs the slicer titles to the console.for(constslicerofslicers){console.log(slicer.getTitle());}

Return

Slicer[] — The list of slicers on this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getTabColorObject()

Gets the sheet tab color, ornull if the sheet tab has no color.

// This example assumes there is a sheet named "Sheet1"constss=SpreadsheetApp.getActiveSpreadsheet();constfirst=ss.getSheetByName('Sheet1');constcolor=first.getTabColorObject();

Return

Color|null — The sheet tab color, ornull if the sheet tab has no color.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getType()

Returns the type of the sheet.

The default type of sheet isSheetType.GRID. A sheet that contains a single embeddedobject such as anEmbeddedChart is anSheetType.OBJECT sheet.

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

Return

SheetType — The type of the sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hasHiddenGridlines()

Returnstrue if the sheet's gridlines are hidden; otherwise returnsfalse.Gridlines are visible by default.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Checks if the spreadsheet has hidden gridelines and logs the result to the// console.console.log(sheet.hasHiddenGridlines());

Return

Booleantrue if gridlines are hidden;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideColumn(column)

Hides the column or columns in the given range.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This hides the first columnletrange=sheet.getRange('A1');sheet.hideColumn(range);// This hides the first 3 columnsrange=sheet.getRange('A:C');sheet.hideColumn(range);

Parameters

NameTypeDescription
columnRangeThe column range to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideColumns(columnIndex)

Hides a single column at the given index. Use 1-index for this method.

To hide more than one column using an index, usehideColumns(columnIndex, numColumns).

To hide more than one column using a range, usehideColumn().

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Hides the first columnsheet.hideColumns(1);

Parameters

NameTypeDescription
columnIndexIntegerThe index of the column to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideColumns(columnIndex, numColumns)

Hides one or more consecutive columns starting at the given index. Use 1-index for this method.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Hides the first three columnssheet.hideColumns(1,3);

Parameters

NameTypeDescription
columnIndexIntegerThe starting index of the columns to hide.
numColumnsIntegerThe number of columns to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideRow(row)

Hides the rows in the given range.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This hides the first rowconstrange=sheet.getRange('A1');sheet.hideRow(range);

Parameters

NameTypeDescription
rowRangeThe row range to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideRows(rowIndex)

Hides the row at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Hides the first rowsheet.hideRows(1);

Parameters

NameTypeDescription
rowIndexIntegerThe index of the row to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideRows(rowIndex, numRows)

Hides one or more consecutive rows starting at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Hides the first three rowssheet.hideRows(1,3);

Parameters

NameTypeDescription
rowIndexIntegerThe starting index of the rows to hide.
numRowsIntegerThe number of rows to hide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

hideSheet()

Hides this sheet. Has no effect if the sheet is already hidden. If this method is called on theonly visible sheet, it throws an exception.

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

Return

Sheet — The current sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertChart(chart)

Adds a new chart to this sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This creates a simple bar chart from the first three rows// of the first two columns of the spreadsheetconstchart=sheet.newChart().setChartType(Charts.ChartType.BAR).addRange(sheet.getRange('A1:B4')).setPosition(5,5,0,0).setOption('title','Dynamic Chart').build();sheet.insertChart(chart);

Parameters

NameTypeDescription
chartEmbeddedChartThe chart to insert.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumnAfter(afterPosition)

Inserts a column after the given column position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts a column after the first column positionsheet.insertColumnAfter(1);

Parameters

NameTypeDescription
afterPositionIntegerThe column after which the new column should be added.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumnBefore(beforePosition)

Inserts a column before the given column position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts a column in the first column positionsheet.insertColumnBefore(1);

Parameters

NameTypeDescription
beforePositionIntegerThe column before which the new column should be added.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumns(columnIndex)

Inserts a blank column in a sheet at the specified location.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Shifts all columns by onesheet.insertColumns(1);

Parameters

NameTypeDescription
columnIndexIntegerThe index indicating where to insert a column.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumns(columnIndex, numColumns)

Inserts one or more consecutive blank columns in a sheet starting at the specified location.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Shifts all columns by threesheet.insertColumns(1,3);

Parameters

NameTypeDescription
columnIndexIntegerThe index indicating where to insert a column.
numColumnsIntegerThe number of columns to insert.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumnsAfter(afterPosition, howMany)

Inserts a given number of columns after the given column position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Inserts two columns after the first column on the first sheet of the// spreadsheet.sheet.insertColumnsAfter(1,2);

Parameters

NameTypeDescription
afterPositionIntegerThe column after which the new column should be added.
howManyIntegerThe number of columns to insert.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertColumnsBefore(beforePosition, howMany)

Inserts a number of columns before the given column position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts five columns before the first columnsheet.insertColumnsBefore(1,5);

Parameters

NameTypeDescription
beforePositionIntegerThe column before which the new column should be added.
howManyIntegerThe number of columns to insert.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertImage(blobSource, column, row)

Inserts aBlobSource as an image in the document at a given row and column. The imagesize is retrieved from the blob contents. The maximum supported blob size is 2MB.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constbinaryData=[];// TODO(developer): Replace with your binary data.constblob=Utilities.newBlob(binaryData,'image/png','MyImageName');sheet.insertImage(blob,1,1);

Parameters

NameTypeDescription
blobSourceBlobSourceThe blob containing the image contents, MIME type, and (optionally) name.
columnIntegerThe column position.
rowIntegerThe row position.

Return

OverGridImage — The inserted image.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

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

Inserts aBlobSource as an image in the document at a given row and column, with apixel offset. The image size is retrieved from the blob contents. The maximum supported blobsize is 2MB.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constbinaryData=[];// TODO(developer): Replace with your binary data.constblob=Utilities.newBlob(binaryData,'image/png','MyImageName');sheet.insertImage(blob,1,1,10,10);

Parameters

NameTypeDescription
blobSourceBlobSourceThe blob containing the image contents, MIME type, and (optionally) name.
columnIntegerThe column position.
rowIntegerThe row position.
offsetXIntegerThe horizontal offset from cell corner in pixels.
offsetYIntegerThe vertical offset from cell corner in pixels.

Return

OverGridImage — The inserted image.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertImage(url, column, row)

Inserts an image in the document at a given row and column.

The provided URL must be publicly accessible.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];sheet.insertImage('https://www.google.com/images/srpr/logo3w.png',1,1);

Parameters

NameTypeDescription
urlStringThe URL of the image.
columnIntegerThe grid column position.
rowIntegerThe grid row position.

Return

OverGridImage — The inserted image.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

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

Inserts an image in the document at a given row and column, with a pixel offset.

The provided URL must be publicly accessible.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];sheet.insertImage('https://www.google.com/images/srpr/logo3w.png',1,1,10,10,);

Parameters

NameTypeDescription
urlStringThe URL for the image.
columnIntegerThe column position.
rowIntegerThe row position.
offsetXIntegerThe horizontal offset from cell corner in pixels.
offsetYIntegerThe vertical offset from cell corner in pixels.

Return

OverGridImage — The Inserted image.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRowAfter(afterPosition)

Inserts a row after the given row position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts a row after the first row positionsheet.insertRowAfter(1);

Parameters

NameTypeDescription
afterPositionIntegerThe row after which the new row should be added.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRowBefore(beforePosition)

Inserts a row before the given row position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts a row before the first row positionsheet.insertRowBefore(1);

Parameters

NameTypeDescription
beforePositionIntegerThe row before which the new row should be added.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRows(rowIndex)

Inserts a blank row in a sheet at the specified location.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Shifts all rows down by onesheet.insertRows(1);

Parameters

NameTypeDescription
rowIndexIntegerThe index indicating where to insert a row.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRows(rowIndex, numRows)

Inserts one or more consecutive blank rows in a sheet starting at the specified location.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Shifts all rows down by threesheet.insertRows(1,3);

Parameters

NameTypeDescription
rowIndexIntegerThe index indicating where to insert a row.
numRowsIntegerThe number of rows to insert.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRowsAfter(afterPosition, howMany)

Inserts a number of rows after the given row position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts five rows after the first rowsheet.insertRowsAfter(1,5);

Parameters

NameTypeDescription
afterPositionIntegerThe row after which the new rows should be added.
howManyIntegerThe number of rows to insert.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertRowsBefore(beforePosition, howMany)

Inserts a number of rows before the given row position.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This inserts five rows before the first rowsheet.insertRowsBefore(1,5);

Parameters

NameTypeDescription
beforePositionIntegerThe row before which the new rows should be added.
howManyIntegerThe number of rows to insert.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

insertSlicer(range, anchorRowPos, anchorColPos)

Adds a new slicer to this sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets the range of the sheet.constrange=sheet.getRange('A1:D10');// Inserts the slicer with a random range into the sheet.constinsertSlicers=sheet.insertSlicer(range.randomize(),1,10);// Logs the insert slicer result to the console.console.log(insertSlicers);

Parameters

NameTypeDescription
rangeRangeThe range over which slicer slicer is created.
anchorRowPosIntegerThe slicer's top side is anchored in this row.
anchorColPosIntegerThe slicer's top side is anchored in this col.

Return

Slicer — The newly inserted slicer.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

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

Adds a new slicer to this sheet.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Gets the range.constrange=sheet.getRange('A1:D10');// Inserts a slicer using the random range function.constinsertSlicers=sheet.insertSlicer(range.randomize(),1,10,0,0);// Logs the insert slicer result to the console.console.log(insertSlicers);

Parameters

NameTypeDescription
rangeRangeThe range over which slicer slicer is created.
anchorRowPosIntegerThe slicer's top side is anchored in this row.
anchorColPosIntegerThe slicer's top side is anchored in this col.
offsetXIntegerThe horizontal offset from cell corner in pixels.
offsetYIntegerThe vertical offset from cell corner in pixels.

Return

Slicer — The newly inserted slicer.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

isColumnHiddenByUser(columnPosition)

Returns whether the given column is hidden by the user.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Columns start at 1Logger.log(sheet.isColumnHiddenByUser(1));

Parameters

NameTypeDescription
columnPositionIntegerThe position of the column to examine.

Return

Booleantrue if the column is hidden,false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

isRightToLeft()

Returnstrue if this sheet layout is right-to-left. Returnsfalse if the sheetuses the default left-to-right layout.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Checks if a spreadsheet is ordered from right to left and logs the result to// the console.console.log(sheet.isRightToLeft());

Return

Booleantrue if right-to-left;false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

isRowHiddenByFilter(rowPosition)

Returns whether the given row is hidden by a filter (not a filter view).

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Rows start at 1Logger.log(sheet.isRowHiddenByFilter(1));

Parameters

NameTypeDescription
rowPositionIntegerThe position of the row to examine.

Return

Booleantrue if the row is hidden,false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

isRowHiddenByUser(rowPosition)

Returns whether the given row is hidden by the user.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Rows start at 1Logger.log(sheet.isRowHiddenByUser(1));

Parameters

NameTypeDescription
rowPositionIntegerThe position of the row to examine.

Return

Booleantrue if the row is hidden,false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

isSheetHidden()

Returnstrue if the sheet is currently hidden.

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

Return

Booleantrue if the sheet is hidden,false otherwise.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

moveColumns(columnSpec, destinationIndex)

Moves the columns selected by the given range to the position indicated by thedestinationIndex. ThecolumnSpec itself does not have to exactly represent an entirecolumn or group of columns to move—it selects all columns that the range spans.

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

Parameters

NameTypeDescription
columnSpecRangeA range spanning the columns that should be moved.
destinationIndexIntegerThe index that the columns should be moved to. Note that this index is based on the coordinates before the columns are moved. Existing data is shifted right to make room for the moved columns while the source columns are removed from the grid. Therefore, the data may end up at a different index than originally specified.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

moveRows(rowSpec, destinationIndex)

Moves the rows selected by the given range to the position indicated by thedestinationIndex. TherowSpec itself does not have to exactly represent an entire rowor group of rows to move—it selects all rows that the range spans.

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

Parameters

NameTypeDescription
rowSpecRangeA range spanning the rows that should be moved.
destinationIndexIntegerThe index that the rows should be moved to. Note that this index is based on the coordinates before the rows are moved. Existing data is shifted down to make room for the moved rows while the source rows are removed from the grid. Therefore, the data may end up at a different index than originally specified.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

newChart()

Returns a builder to create a new chart for this sheet.

This example shows how to create a new chart:

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

Return

EmbeddedChartBuilder — A builder to create a new chart.


protect()

Creates an object that can protect the sheet from being edited except by users who havepermission. Until the script actually changes the list of editors for the sheet (by callingProtection.removeEditor(emailAddress),Protection.removeEditor(user),Protection.removeEditors(emailAddresses),Protection.addEditor(emailAddress),Protection.addEditor(user),Protection.addEditors(emailAddresses), or setting a newvalue forProtection.setDomainEdit(editable)), the permissions mirror those of thespreadsheet itself, which effectively means that the sheet remains unprotected. If the sheet isalready protected, this method returns an object representing its existing protection settings.A protected sheet may include unprotected regions.

// Protect the active sheet, then remove all other users from the list of// editors.constsheet=SpreadsheetApp.getActiveSheet();constprotection=sheet.protect().setDescription('Sample protected sheet');// Ensure the current user is an editor before removing others. Otherwise, if// the user's edit permission comes from a group, the script throws an exception// upon removing the group.constme=Session.getEffectiveUser();protection.addEditor(me);protection.removeEditors(protection.getEditors());if(protection.canDomainEdit()){protection.setDomainEdit(false);}

Return

Protection — An object representing the protection settings.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

removeChart(chart)

Removes a chart from the parent sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This removes all the embedded charts from the spreadsheetconstcharts=sheet.getCharts();for(constiincharts){sheet.removeChart(charts[i]);}

Parameters

NameTypeDescription
chartEmbeddedChartThe chart to remove.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setActiveRange(range)

Sets the specified range as theactive range in the active sheet, withthe top left cell in the range as thecurrent cell.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();constrange=sheet.getRange('A1:D4');sheet.setActiveRange(range);constselection=sheet.getSelection();// Current cell: A1constcurrentCell=selection.getCurrentCell();// Active Range: A1:D4constactiveRange=selection.getActiveRange();

Parameters

NameTypeDescription
rangeRangeThe range to set as the active range.

Return

Range — The newly active range.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setActiveRangeList(rangeList)

Sets the specified list of ranges as theactive ranges in theactive sheet. The last range in the list is set as theactive range.

constsheet=SpreadsheetApp.getActiveSheet();constrangeList=sheet.getRangeList(['D4','B2:C4']);sheet.setActiveRangeList(rangeList);constselection=sheet.getSelection();// Current cell: B2constcurrentCell=selection.getCurrentCell();// Active range: B2:C4constactiveRange=selection.getActiveRange();// Active range list: [D4, B2:C4]constactiveRangeList=selection.getActiveRangeList();

Parameters

NameTypeDescription
rangeListRangeListThe list of ranges to select.

Return

RangeList — The newly selected list of ranges.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setActiveSelection(range)

Sets the active selection region for this sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constrange=sheet.getRange('A1:D4');sheet.setActiveSelection(range);

Parameters

NameTypeDescription
rangeRangeThe range to set as the active selection.

Return

Range — The newly active range.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setActiveSelection(a1Notation)

Sets the active selection, as specified in A1 notation or R1C1 notation.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];sheet.setActiveSelection('A1:D4');

Parameters

NameTypeDescription
a1NotationStringThe range to set as active, as specified in A1 notation or R1C1 notation.

Return

Range — The newly active range.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setColumnGroupControlPosition(position)

Sets the position of the column group control toggle on the sheet.

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

Parameters

NameTypeDescription
positionGroupControlTogglePositionThe position of the column group control toggle.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setColumnWidth(columnPosition, width)

Sets the width of the given column in pixels.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first column to a width of 200 pixelssheet.setColumnWidth(1,200);

Parameters

NameTypeDescription
columnPositionIntegerThe position of the given column to set.
widthIntegerThe width in pixels to set it to.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setColumnWidths(startColumn, numColumns, width)

Sets the width of the given columns in pixels.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first three columns to a width of 200 pixelssheet.setColumnWidths(1,3,200);

Parameters

NameTypeDescription
startColumnIntegerThe starting column position to change.
numColumnsIntegerThe number of columns to change.
widthIntegerThe width in pixels to set it to.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setConditionalFormatRules(rules)

Replaces all currently existing conditional format rules in the sheet with the input rules.Rules are evaluated in their input order.

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

Parameters

NameTypeDescription
rulesConditionalFormatRule[]The new conditional format rules.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setCurrentCell(cell)

Sets the specified cell as thecurrent cell.

If the specified cell is present in an already selected range, then that range becomes theactive range with the cell as the current cell.

If the specified cell is not present in any selected range, then any existing selection isremoved and the cell becomes the current cell and the active range.

Note:The specifiedRange must consist of one cell, otherwise it throws anexception.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();constcell=sheet.getRange('B5');sheet.setCurrentCell(cell);constselection=sheet.getSelection();// Current cell: B5constcurrentCell=selection.getCurrentCell();

Parameters

NameTypeDescription
cellRangeThe cell to set as the current cell.

Return

Range — The newly set current cell.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setFrozenColumns(columns)

Freezes the given number of columns. If zero, no columns are frozen.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Freezes the first columnsheet.setFrozenColumns(1);

Parameters

NameTypeDescription
columnsIntegerThe number of columns to freeze.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setFrozenRows(rows)

Freezes the given number of rows. If zero, no rows are frozen.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Freezes the first rowsheet.setFrozenRows(1);

Parameters

NameTypeDescription
rowsIntegerThe number of rows to freeze.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setHiddenGridlines(hideGridlines)

Hides or reveals the sheet gridlines.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets file, you can us eSpreadsheetApp.getActiveSpreadsheet()// instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Hides the gridlines in the sheet.sheet.setHiddenGridlines(true);

Parameters

NameTypeDescription
hideGridlinesBooleanIftrue, hide gridlines in this sheet; otherwise show the gridlines.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setName(name)

Sets the sheet name.

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

Parameters

NameTypeDescription
nameStringThe new name for the sheet.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setRightToLeft(rightToLeft)

Sets or unsets the sheet layout to right-to-left.

// 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.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets a sheet by its name.constsheet=ss.getSheetByName('Sheet1');// Sets the sheet layout, so that the sheet is ordered from right to left.sheet.setRightToLeft(true);

Parameters

NameTypeDescription
rightToLeftBooleanIftrue, the sheet layout is set to right-to-left, with cell A1 at the top right corner. Iffalse, the sheet layout is set to the default left-to-right, with cell A1 at the top left.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setRowGroupControlPosition(position)

Sets the position of the row group control toggle on the sheet.

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

Parameters

NameTypeDescription
positionGroupControlTogglePositionThe position of the row group control toggle.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setRowHeight(rowPosition, height)

Sets the row height of the given row in pixels. By default, rows grow to fit cell contents. Ifyou want to force rows to a specified height, usesetRowHeightsForced(startRow, numRows, height).

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first row to a height of 200 pixelssheet.setRowHeight(1,200);

Parameters

NameTypeDescription
rowPositionIntegerThe row position to change.
heightIntegerThe height in pixels to set it to.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setRowHeights(startRow, numRows, height)

Sets the height of the given rows in pixels. By default, rows grow to fit cell contents. If youwant to force rows to a specified height, usesetRowHeightsForced(startRow, numRows, height).

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first three rows to a height of 20 pixelssheet.setRowHeights(1,3,20);

Parameters

NameTypeDescription
startRowIntegerThe starting row position to change.
numRowsIntegerThe number of rows to change.
heightIntegerThe height in pixels to set it to.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setRowHeightsForced(startRow, numRows, height)

Sets the height of the given rows in pixels. By default, rows grow to fit cell contents. Whenyou usesetRowHeightsForced, rows are forced to the specified height even if thecell contents are taller than the row height.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sets the first three rows to a height of 5 pixels.sheet.setRowHeightsForced(1,3,5);

Parameters

NameTypeDescription
startRowIntegerThe starting row position to change.
numRowsIntegerThe number of rows to change.
heightIntegerThe height in pixels to set it to.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setTabColor(color)

Sets the sheet tab color.

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

Parameters

NameTypeDescription
colorStringA color code in CSS notation (like'#ffffff' or'white'), ornull to reset the tab color.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setTabColorObject(color)

Sets the sheet tab color.

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

Parameters

NameTypeDescription
colorColorThe sheet tab color to set.

Return

Sheet — This sheet, for chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

showColumns(columnIndex)

Unhides the column at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Unhides the first columnsheet.showColumns(1);

Parameters

NameTypeDescription
columnIndexIntegerThe index of the column to unhide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

showColumns(columnIndex, numColumns)

Unhides one or more consecutive columns starting at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Unhides the first three columnssheet.showColumns(1,3);

Parameters

NameTypeDescription
columnIndexIntegerThe starting index of the columns to unhide.
numColumnsIntegerThe number of columns to unhide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

showRows(rowIndex)

Unhides the row at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Unhides the first rowsheet.showRows(1);

Parameters

NameTypeDescription
rowIndexIntegerThe index of the row to unhide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

showRows(rowIndex, numRows)

Unhides one or more consecutive rows starting at the given index.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Unhides the first three rowssheet.showRows(1,3);

Parameters

NameTypeDescription
rowIndexIntegerThe starting index of the rows to unhide.
numRowsIntegerThe number of rows to unhide.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

showSheet()

Makes the sheet visible. Has no effect if the sheet is already visible.

constsheet=SpreadsheetApp.getActiveSheet();sheet.showSheet();

Return

Sheet — The current sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

sort(columnPosition)

Sorts a sheet by column, ascending.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sorts the sheet by the first column, ascendingsheet.sort(1);

Parameters

NameTypeDescription
columnPositionIntegerThe column to sort by.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

sort(columnPosition, ascending)

Sorts a sheet by column. Takes a parameter to specify ascending or descending.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// Sorts the sheet by the first column, descendingsheet.sort(1,false);

Parameters

NameTypeDescription
columnPositionIntegerThe column to sort by.
ascendingBooleantrue for ascending sorts,false for descending.

Return

Sheet — The sheet, useful for method chaining.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

unhideColumn(column)

Unhides the column in the given range.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This unhides the first column if it was previously hiddenconstrange=sheet.getRange('A1');sheet.unhideColumn(range);

Parameters

NameTypeDescription
columnRangeThe range to unhide, if hidden.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

unhideRow(row)

Unhides the row in the given range.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This unhides the first row if it was previously hiddenconstrange=sheet.getRange('A1');sheet.unhideRow(range);

Parameters

NameTypeDescription
rowRangeThe range to unhide, if hidden.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

updateChart(chart)

Updates the chart on this sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];// This code is going to loop through all the charts and change them to// column chartsconstcharts=sheet.getCharts();for(constiincharts){constchart=charts[i];constnewChart=chart.modify().setChartType(Charts.ChartType.COLUMN).build();sheet.updateChart(newChart);}

Parameters

NameTypeDescription
chartEmbeddedChartThe chart to update.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

Deprecated methods

getSheetProtection()

Deprecated. For spreadsheets created in the newer version of Google Sheets, usegetProtections(type), which returns the more powerfulProtection class. Although this method is deprecated, it remains available for compatibility with the older version of Sheets

Returns aPageProtection instance describing the permissions for the current sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constpermissions=sheet.getSheetProtection();permissions.setProtected(true);permissions.addUser('user@example.com');// Logs the users that have access to edit this sheet. Note that this// is different from access to the entire spreadsheet - getUsers()// only returns users if permissions.isProtected() is set to true.constusers=permissions.getUsers();Logger.log(users);

Return

PageProtection — An object describing sheet access permissions.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

getTabColor()

Deprecated. Replaced bygetTabColorObject()

Gets the sheet tab color, ornull if the sheet tab has no color.

// This example assumes there is a sheet named "first"constss=SpreadsheetApp.getActiveSpreadsheet();constfirst=ss.getSheetByName('first');constcolor=first.getTabColor();

Return

String|null — Color code in CSS notation (such as'#ffffff').

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

setSheetProtection(permissions)

Deprecated. For spreadsheets created in the newer version of Google Sheets, useprotect(), which returns the more powerfulProtection class. Although this method is deprecated, it remains available for compatibility with the older version of Sheets

Sets the permissions for the current sheet.

constss=SpreadsheetApp.getActiveSpreadsheet();constsheet=ss.getSheets()[0];constpermissions=sheet.getSheetProtection();// This copies the permissions on the first sheet to the second sheetconstsheetToClonePermissionsTo=ss.getSheets()[1];sheetToClonePermissionsTo.setSheetProtection(permissions);

Parameters

NameTypeDescription
permissionsPageProtectionThe access permissions object to set on this sheet.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

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

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.