Class Spreadsheet

  • The document provides a summary of methods for interacting with Google Sheets using theSpreadsheet object in Google Apps Script.

  • Common operations include managing sheets, ranges, cells, and collaborators.

  • Methods are available for handling spreadsheet properties, data sources, and UI elements like custom menus.

  • The document also lists deprecated methods, often with suggestions for more current alternatives using the Drive service.

Spreadsheet

Access and modify Google Sheets files. Common operations are adding new sheets and addingcollaborators.

Methods

MethodReturn typeBrief description
addDeveloperMetadata(key)SpreadsheetAdds developer metadata with the specified key to the top-level spreadsheet.
addDeveloperMetadata(key, visibility)SpreadsheetAdds developer metadata with the specified key and visibility to the spreadsheet.
addDeveloperMetadata(key, value)SpreadsheetAdds developer metadata with the specified key and value to the spreadsheet.
addDeveloperMetadata(key, value, visibility)SpreadsheetAdds developer metadata with the specified key, value, and visibility to the spreadsheet.
addEditor(emailAddress)SpreadsheetAdds the given user to the list of editors for theSpreadsheet.
addEditor(user)SpreadsheetAdds the given user to the list of editors for theSpreadsheet.
addEditors(emailAddresses)SpreadsheetAdds the given array of users to the list of editors for theSpreadsheet.
addMenu(name, subMenus)voidCreates a new menu in the Spreadsheet UI.
addViewer(emailAddress)SpreadsheetAdds the given user to the list of viewers for theSpreadsheet.
addViewer(user)SpreadsheetAdds the given user to the list of viewers for theSpreadsheet.
addViewers(emailAddresses)SpreadsheetAdds the given array of users to the list of viewers for theSpreadsheet.
appendRow(rowContents)SheetAppends a row to the bottom of the current data region in the sheet.
autoResizeColumn(columnPosition)SheetSets the width of the given column to fit its contents.
copy(name)SpreadsheetCopies the spreadsheet and returns the new one.
createDeveloperMetadataFinder()DeveloperMetadataFinderReturns aDeveloperMetadataFinder for finding developer metadata within the scope ofthis spreadsheet.
createTextFinder(findText)TextFinderCreates a text finder for the spreadsheet, which can be used to find and replace text withinthe spreadsheet.
deleteActiveSheet()SheetDeletes the currently active 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.
deleteSheet(sheet)voidDeletes the specified sheet.
duplicateActiveSheet()SheetDuplicates the active sheet and makes it the active sheet.
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()RangeListReturns the list of active ranges in the active sheet ornull if there are no activeranges.
getActiveSheet()SheetGets the active sheet in a spreadsheet.
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getBandings()Banding[]Returns all the bandings in this spreadsheet.
getBlob()BlobReturn the data inside this object as a blob.
getColumnWidth(columnPosition)IntegerGets the width in pixels of the given column.
getCurrentCell()RangeReturns 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.
getDataSourceRefreshSchedules()DataSourceRefreshSchedule[]Gets the refresh schedules of this spreadsheet.
getDataSourceSheets()DataSourceSheet[]Returns all the data source sheets in the spreadsheet.
getDataSourceTables()DataSourceTable[]Gets all the data source tables.
getDataSources()DataSource[]Returns all the data sources in the spreadsheet.
getDeveloperMetadata()DeveloperMetadata[]Gets the developer metadata associated with the top-level spreadsheet.
getEditors()User[]Gets the list of editors for thisSpreadsheet.
getFormUrl()StringReturns the URL for the form that sends its responses to this spreadsheet, ornull ifthis spreadsheet has no associated form.
getFrozenColumns()IntegerReturns the number of frozen columns.
getFrozenRows()IntegerReturns the number of frozen rows.
getId()StringGets a unique identifier for this spreadsheet.
getImages()OverGridImage[]Returns all over-the-grid images on the sheet.
getIterativeCalculationConvergenceThreshold()NumberReturns the threshold value used during iterative calculation.
getLastColumn()IntegerReturns the position of the last column that has content.
getLastRow()IntegerReturns the position of the last row that has content.
getMaxIterativeCalculationCycles()IntegerReturns the maximum number of iterations to use during iterative calculation.
getName()StringGets the name of the document.
getNamedRanges()NamedRange[]Gets all the named ranges in this spreadsheet.
getNumSheets()IntegerReturns the number of sheets in this spreadsheet.
getOwner()UserReturns the owner of the document, ornull for a document in a shared drive.
getPredefinedSpreadsheetThemes()SpreadsheetTheme[]Returns the list of predefined themes.
getProtections(type)Protection[]Gets an array of objects representing all protected ranges or sheets in the spreadsheet.
getRange(a1Notation)RangeReturns the range as specified in A1 notation or R1C1 notation.
getRangeByName(name)RangeReturns a named range, ornull if no range with the given name is found.
getRangeList(a1Notations)RangeListReturns theRangeList collection representing the ranges in the same sheet specifiedby a non-empty list of A1 notations or R1C1 notations.
getRecalculationInterval()RecalculationIntervalReturns the calculation interval for this spreadsheet.
getRowHeight(rowPosition)IntegerGets the height in pixels of the given row.
getSelection()SelectionReturns the currentSelection in the spreadsheet.
getSheetById(id)SheetGets the sheet with the given ID.
getSheetByName(name)SheetReturns a sheet with the given name.
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.
getSheets()Sheet[]Gets all the sheets in this spreadsheet.
getSpreadsheetLocale()StringGets the spreadsheet locale.
getSpreadsheetTheme()SpreadsheetThemeReturns the current theme of the spreadsheet, ornull if no theme is applied.
getSpreadsheetTimeZone()StringGets the time zone for the spreadsheet.
getUrl()StringReturns the URL for the given spreadsheet.
getViewers()User[]Gets the list of viewers and commenters for thisSpreadsheet.
hideColumn(column)voidHides the column or columns in the given range.
hideRow(row)voidHides the rows in the given range.
insertColumnAfter(afterPosition)SheetInserts a column after the given column position.
insertColumnBefore(beforePosition)SheetInserts a column before the given column position.
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.
insertDataSourceSheet(spec)DataSourceSheetInserts a newDataSourceSheet in the spreadsheet and starts data execution.
insertImage(blobSource, column, row)OverGridImageInserts aSpreadsheet as an image in the document at a given row and column.
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImageInserts aSpreadsheet 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.
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.
insertSheet()SheetInserts a new sheet into the spreadsheet, using a default sheet name.
insertSheet(sheetIndex)SheetInserts a new sheet into the spreadsheet at the given index.
insertSheet(sheetIndex, options)SheetInserts a new sheet into the spreadsheet at the given index and uses optional advancedarguments.
insertSheet(options)SheetInserts a new sheet into the spreadsheet, using a default sheet name and optional advancedarguments.
insertSheet(sheetName)SheetInserts a new sheet into the spreadsheet with the given name.
insertSheet(sheetName, sheetIndex)SheetInserts a new sheet into the spreadsheet with the given name at the given index.
insertSheet(sheetName, sheetIndex, options)SheetInserts a new sheet into the spreadsheet with the given name at the given index and usesoptional advanced arguments.
insertSheet(sheetName, options)SheetInserts a new sheet into the spreadsheet with the given name and uses optional advancedarguments.
insertSheetWithDataSourceTable(spec)SheetInserts a new sheet in the spreadsheet, creates aDataSourceTable spanning theentire sheet with the given data source specification, and starts data execution.
isColumnHiddenByUser(columnPosition)BooleanReturns whether the given column is hidden by the user.
isIterativeCalculationEnabled()BooleanReturns whether iterative calculation is activated in this spreadsheet.
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.
moveActiveSheet(pos)voidMoves the active sheet to the given position in the list of sheets.
moveChartToObjectSheet(chart)SheetCreates a newSheetType.OBJECT sheet and moves the provided chart to it.
refreshAllDataSources()voidRefreshes all supported data sources and their linked data source objects, skipping invaliddata source objects.
removeEditor(emailAddress)SpreadsheetRemoves the given user from the list of editors for theSpreadsheet.
removeEditor(user)SpreadsheetRemoves the given user from the list of editors for theSpreadsheet.
removeMenu(name)voidRemoves a menu that was added byaddMenu(name, subMenus).
removeNamedRange(name)voidDeletes a named range with the given name.
removeViewer(emailAddress)SpreadsheetRemoves the given user from the list of viewers and commenters for theSpreadsheet.
removeViewer(user)SpreadsheetRemoves the given user from the list of viewers and commenters for theSpreadsheet.
rename(newName)voidRenames the document.
renameActiveSheet(newName)voidRenames the current active sheet to the given new name.
resetSpreadsheetTheme()SpreadsheetThemeRemoves the applied theme and sets the default theme on the spreadsheet.
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.
setActiveSheet(sheet)SheetSets the given sheet to be the active sheet in the spreadsheet.
setActiveSheet(sheet, restoreSelection)SheetSets the given sheet to be the active sheet in the spreadsheet, with an option to restore themost recent selection within that sheet.
setColumnWidth(columnPosition, width)SheetSets the width of the given column in pixels.
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.
setIterativeCalculationConvergenceThreshold(minThreshold)SpreadsheetSets the minimum threshold value for iterative calculation.
setIterativeCalculationEnabled(isEnabled)SpreadsheetSets whether iterative calculation is activated in this spreadsheet.
setMaxIterativeCalculationCycles(maxIterations)SpreadsheetSets the maximum number of calculation iterations that should be performed during iterativecalculation.
setNamedRange(name, range)voidNames a range.
setRecalculationInterval(recalculationInterval)SpreadsheetSets how often this spreadsheet should recalculate.
setRowHeight(rowPosition, height)SheetSets the row height of the given row in pixels.
setSpreadsheetLocale(locale)voidSets the spreadsheet locale.
setSpreadsheetTheme(theme)SpreadsheetThemeSets a theme on the spreadsheet.
setSpreadsheetTimeZone(timezone)voidSets the time zone for the spreadsheet.
show(userInterface)voidDisplays a custom user interface component in a dialog centered in the user's browser'sviewport.
sort(columnPosition)SheetSorts a sheet by column, ascending.
sort(columnPosition, ascending)SheetSorts a sheet by column.
toast(msg)voidShows a popup window in the lower right corner of the spreadsheet with the given message.
toast(msg, title)voidShows a popup window in the lower right corner of the spreadsheet with the given message andtitle.
toast(msg, title, timeoutSeconds)voidShows a popup window in the lower right corner of the spreadsheet with the given title andmessage, that stays visible for a certain length of time.
unhideColumn(column)voidUnhides the column in the given range.
unhideRow(row)voidUnhides the row in the given range.
updateMenu(name, subMenus)voidUpdates a menu that was added byaddMenu(name, subMenus).
waitForAllDataExecutionsCompletion(timeoutInSeconds)voidWaits until all the current executions in the spreadsheet complete, timing out after theprovided number of seconds.

Deprecated methods

MethodReturn typeBrief description
getSheetProtection()PageProtectionReturns aPageProtection instance describing the permissions for the current sheet.
isAnonymousView()BooleanIndicates whether the document allows anonymous viewing.
isAnonymousWrite()BooleanIndicates whether the document allows edits from anonymous users.
setAnonymousAccess(anonymousReadAllowed, anonymousWriteAllowed)voidSets the document's policy on anonymous reading and writing.
setSheetProtection(permissions)voidSets the permissions for the current sheet.

Detailed documentation

addDeveloperMetadata(key)

Adds developer metadata with the specified key to the top-level spreadsheet.

// 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',);// Adds the key 'NAME' in the developer metadata for the spreadsheet.ss.addDeveloperMetadata('NAME');// Gets the first developer metadata object and logs its key.constdeveloperMetaData=ss.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());

Parameters

NameTypeDescription
keyStringThe key for the new developer metadata.

Return

Spreadsheet — This spreadsheet, 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 spreadsheet.

// 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',);// Adds the key 'NAME' in the developer metadata for the spreadsheet and sets// the visibility to the developer project that created the metadata.ss.addDeveloperMetadata('NAME',SpreadsheetApp.DeveloperMetadataVisibility.PROJECT,);// Gets the first developer metadata object and logs its key and visibility// setting.constdeveloperMetaData=ss.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());console.log(`Key:${developerMetaData.getKey()},.             Visibility:${developerMetaData.getVisibility()}`);

Parameters

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

Return

Spreadsheet — This spreadsheet, 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 spreadsheet.

// 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',);// Adds the key 'NAME' and sets the value to 'GOOGLE' in the developer metadata// for the spreadsheet.ss.addDeveloperMetadata('NAME','GOOGLE');// Gets the first developer metadata object and logs its key and value.constdeveloperMetaData=ss.getDeveloperMetadata()[0];console.log(developerMetaData.getKey());console.log(`Key:${developerMetaData.getKey()}, Value:${developerMetaData.getValue()}`,);

Parameters

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

Return

Spreadsheet — This spreadsheet, 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 spreadsheet.

// 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',);// Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility// to any developer project with document access.ss.addDeveloperMetadata('NAME','GOOGLE',SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT,);// Gets the first developer metadata object and logs its key, value, and// visibility setting.constdeveloperMetaData=ss.getDeveloperMetadata()[0];console.log(`Key:${developerMetaData.getKey()},             Value:${developerMetaData.getValue()},             Visibility:${developerMetaData.getVisibility()}`);

Parameters

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

Return

Spreadsheet — This spreadsheet, 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

addEditor(emailAddress)

Adds the given user to the list of editors for theSpreadsheet. If the user was alreadyon the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Spreadsheet — ThisSpreadsheet, 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

addEditor(user)

Adds the given user to the list of editors for theSpreadsheet. If the user was alreadyon the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Spreadsheet — ThisSpreadsheet, 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

addEditors(emailAddresses)

Adds the given array of users to the list of editors for theSpreadsheet. If any of theusers were already on the list of viewers, this method promotes them out of the list ofviewers.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Spreadsheet — ThisSpreadsheet, 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

addMenu(name, subMenus)

Creates a new menu in the Spreadsheet UI.

Each menu entry runs a user-defined function. Usually, you want to call it from theonOpen() function so that the menu is automatically created when the spreadsheet is loaded.

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

Parameters

NameTypeDescription
nameStringThe name of the menu to be created.
subMenusObject[]An array of JavaScript maps withname andfunctionName parameters. You can use functions from included libraries, such as Library.libFunction1.

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

addViewer(emailAddress)

Adds the given user to the list of viewers for theSpreadsheet. If the user was alreadyon the list of editors, this method has no effect.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Spreadsheet — ThisSpreadsheet, 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

addViewer(user)

Adds the given user to the list of viewers for theSpreadsheet. If the user was alreadyon the list of editors, this method has no effect.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Spreadsheet — ThisSpreadsheet, 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

addViewers(emailAddresses)

Adds the given array of users to the list of viewers for theSpreadsheet. If any of theusers were already on the list of editors, this method has no effect for them.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Spreadsheet — ThisSpreadsheet, 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

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

copy(name)

Copies the spreadsheet and returns the new one.

// This code makes a copy of the current spreadsheet and names it appropriatelyconstss=SpreadsheetApp.getActiveSpreadsheet();ss.copy(`Copy of${ss.getName()}`);

Parameters

NameTypeDescription
nameStringThe name of the copy.

Return

Spreadsheet — This spreadsheet, 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 spreadsheet. By default this considers all metadata associated with the spreadsheet,sheets, rows, and columns.

// 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',);// Adds developer metadata to the spreadsheet.ss.addDeveloperMetadata('NAME','CHARLIE');ss.addDeveloperMetadata('COMPANY','EXAMPLE ORGANIZATION');ss.addDeveloperMetadata('TECHNOLOGY','JAVASCRIPT');// Creates a developer metadata finder.constdeveloperMetadataFinder=ss.createDeveloperMetadataFinder();// Finds the developer metadata objects with 'COMPANY' as the key.constgoogleMetadataFromSpreadsheet=developerMetadataFinder.withKey('COMPANY').find();// Gets the first result of developer metadata that has the key 'COMPANY' and// logs its value.console.log(googleMetadataFromSpreadsheet[0].getValue());

Return

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


createTextFinder(findText)

Creates a text finder for the spreadsheet, which can be used to find and replace text withinthe spreadsheet. The search starts from the first sheet of the spreadsheet.

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

Parameters

NameTypeDescription
findTextStringThe text to search for.

Return

TextFinder — TheTextFinder for the spreadsheet.


deleteActiveSheet()

Deletes the currently active sheet.

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

Return

Sheet — The new active 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

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

deleteSheet(sheet)

Deletes the specified sheet.

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

Parameters

NameTypeDescription
sheetSheetThe sheet 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

duplicateActiveSheet()

Duplicates the active sheet and makes it the active sheet.

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

Return

Sheet — The new 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

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


getActiveSheet()

Gets the active sheet in a spreadsheet.

The active sheet in a spreadsheet is the sheet that is being displayed in the spreadsheetUI.

constsheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

Return

Sheet — The active sheet in the spreadsheet.


getAs(contentType)

Return the data inside this object as a blob converted to the specified content type. Thismethod adds the appropriate extension to the filename—for example, "myfile.pdf". However, itassumes that the part of the filename that follows the last period (if any) is an existingextension that should be replaced. Consequently, "ShoppingList.12.25.2014" becomes"ShoppingList.12.25.pdf".

To view the daily quotas for conversions, seeQuotas for GoogleServices. Newly created Google Workspace domains might be temporarily subject to stricterquotas.

Parameters

NameTypeDescription
contentTypeStringThe MIME type to convert to. For most blobs,'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of'image/bmp','image/gif','image/jpeg', or'image/png' are also valid. For a Google Docs document,'text/markdown' is also valid.

Return

Blob — The data as a blob.


getBandings()

Returns all the bandings in this spreadsheet.

// 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 an array of the bandings in the spreadsheet.constbandings=ss.getBandings();// Logs the range of the first banding in the spreadsheet to the console.console.log(bandings[0].getRange().getA1Notation());

Return

Banding[] — The bandings in this 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

getBlob()

Return the data inside this object as a blob.

Return

Blob — The data as a blob.


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

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

getDataSourceRefreshSchedules()

Gets the refresh schedules of this spreadsheet.

// 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',);// Activates BigQuery operations for the connected spreadsheet.SpreadsheetApp.enableBigQueryExecution();// Gets the frequency type of the first referesh schedule in the array.constfrequencyType=ss.getDataSourceRefreshSchedules()[0].getFrequency().getFrequencyType().toString();// Logs the frequency type to the console.console.log(frequencyType);

Return

DataSourceRefreshSchedule[] — The refresh schedules of this 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

getDataSourceSheets()

Returns all the data source sheets in the spreadsheet.

// Turns data execution on for BigQuery data sources.SpreadsheetApp.enableBigQueryExecution();// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets the first data source sheet in the spreadsheet.constdataSource=ss.getDataSourceSheets()[0];// Gets the name of the data source sheet.console.log(dataSource.asSheet().getName());

Return

DataSourceSheet[] — An array of all the data source sheets.

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

getDataSources()

Returns all the data sources in the spreadsheet.

// Turns data execution on for BigQuery data sources.SpreadsheetApp.enableBigQueryExecution();// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets the data sources on the spreadsheet.constdataSources=ss.getDataSources();// Logs the name of the first column on the first data source.console.log(dataSources[0].getColumns()[0].getName());

Return

DataSource[] — An array of all the data sources.

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

Gets the developer metadata associated with the top-level spreadsheet.

// 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',);// Adds 'Google' as a key to the spreadsheet metadata.ss.addDeveloperMetadata('Google');// Gets the spreadsheet's metadata.constssMetadata=ss.getDeveloperMetadata();// Gets the first set of the spreadsheet's metadata and logs the key to the// console.console.log(ssMetadata[0].getKey());

Return

DeveloperMetadata[] — The developer metadata associated with this 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

getEditors()

Gets the list of editors for thisSpreadsheet.

Return

User[] — An array of users with edit permission.

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 spreadsheet, ornull ifthis spreadsheet has no associated form. If multiple forms send responses to this spreadsheet,the form URL returned is indeterminate. As an alternative, per sheet form URL associations canbe retrieved through theSheet.getFormUrl() method. Throws an exception if the userdoes not have permission to edit the spreadsheet.

// 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 the form URL from the spreadsheet.constformUrl=ss.getFormUrl();// Logs the form URL to the console.console.log(formUrl);

Return

String — The URL for the form that places its responses in this spreadsheet, ornull if this spreadsheet 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

getId()

Gets a unique identifier for this spreadsheet. A spreadsheet ID can be extracted from its URL.For example, the spreadsheet ID in the URLhttps://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is "abc1234567".

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

Return

String — The unique ID (or key) for the spreadsheet.


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

getIterativeCalculationConvergenceThreshold()

Returns the threshold value used during iterative calculation. When the results of successivecalculation differ by less than this value, the iterative calculation stops.

// 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',);// Sets the iterative calculation convergence threshold for the spreadsheet.ss.setIterativeCalculationConvergenceThreshold(2);// Logs the threshold to the console.console.log(ss.getIterativeCalculationConvergenceThreshold());

Return

Number — The convergence threshold.

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

getMaxIterativeCalculationCycles()

Returns the maximum number of iterations to use during iterative calculation.

// 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',);// Sets the max iterative calculation cycles for the spreadsheet.ss.setMaxIterativeCalculationCycles(10);// Logs the max iterative calculation cycles to the console.console.log(ss.getMaxIterativeCalculationCycles());

Return

Integer — The maximum number of calculation iterations.

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

Gets the name of the document.

constss=SpreadsheetApp.getActiveSpreadsheet();Logger.log(ss.getName());

Return

String — The name of 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

getNamedRanges()

Gets all the named ranges in this spreadsheet.

// The code below logs the name of the first named range.constnamedRanges=SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();for(leti=0;i <namedRanges.length;i++){Logger.log(namedRanges[i].getName());}

Return

NamedRange[] — An array of all the named ranges 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

getNumSheets()

Returns the number of sheets in this spreadsheet.

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

Return

Integer — The number of sheets in the spreadsheet.


getOwner()

Returns the owner of the document, ornull for a document in a shared drive.

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

Return

User — The owner of the document, ornull if the document is in a shared drive.

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

getPredefinedSpreadsheetThemes()

Returns the list of predefined themes.

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

Return

SpreadsheetTheme[] — List of predefined themes.

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 or sheets in the spreadsheet.

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

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

getRangeByName(name)

Returns a named range, ornull if no range with the given name is found. If multiplesheets of the spreadsheet use the same range name, specify the sheet name without additionalquotation marks — for example,getRangeByName('TaxRates') orgetRangeByName('Sheet Name!TaxRates'), but notgetRangeByName('"SheetName"!TaxRates').

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

Parameters

NameTypeDescription
nameStringThe name of the range to get.

Return

Range — The range of cells with the given name.


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

getRecalculationInterval()

Returns the calculation interval for this spreadsheet.

// 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',);// Logs the calculation interval for the spreadsheet to the console.console.log(ss.getRecalculationInterval().toString());

Return

RecalculationInterval — The calculation interval for this 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

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

getSheetById(id)

Gets the sheet with the given ID. UseSheet.getSheetId().

constsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetById(12345);

Parameters

NameTypeDescription
idIntegerThe ID of the sheet to get.

Return

Sheet — The sheet with the given ID ornull if no sheet is found.


getSheetByName(name)

Returns a sheet with the given name.

If multiple sheets have the same name, the leftmost one is returned. Returnsnull ifthere is no sheet with the given name.

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

Parameters

NameTypeDescription
nameStringThe name of the sheet to get.

Return

Sheet — The sheet with the given name.


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

getSheets()

Gets all the sheets in this spreadsheet.

// The code below logs the name of the second sheetconstsheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();// Iterates through the sheets and logs the name and ID of each sheet.for(constsheetofsheets){Logger.log(`name:${sheet.getName()}, ID:${sheet.getSheetId()}`);}

Return

Sheet[] — An array of all the sheets in the spreadsheet.


getSpreadsheetLocale()

Gets the spreadsheet locale.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets the spreadsheet locale.constssLocale=ss.getSpreadsheetLocale();// Logs the locale to the console.console.log(ssLocale);

Return

String — The spreadsheet locale.

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

getSpreadsheetTheme()

Returns the current theme of the spreadsheet, ornull if no theme is applied.

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

Return

SpreadsheetTheme — The current applied theme.

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

getSpreadsheetTimeZone()

Gets the time zone for the spreadsheet.

// 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',);// Sets the time zone of the spreadsheet.ss.setSpreadsheetTimeZone('America/New_York');// Gets the time zone of the spreadsheet.constssTimeZone=ss.getSpreadsheetTimeZone();// Logs the time zone to the console.console.log(ssTimeZone);

Return

String — The time zone, specified in "long" format (for example, "America/New_York", as listed byJoda.org).

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

getUrl()

Returns the URL for the given spreadsheet.

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

Return

String — The URL for the given 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

getViewers()

Gets the list of viewers and commenters for thisSpreadsheet.

Return

User[] — An array of users with view or comment permission.

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

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

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

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

insertDataSourceSheet(spec)

Inserts a newDataSourceSheet in the spreadsheet and starts data execution. As aside effect, this also makes the new sheet the active sheet.

Throws an exception if the data source type is not enabled. UseSpreadsheetApp#enable...Execution() methods to enable data execution for specific data sourcetype.

// Activates BigQuery operations.SpreadsheetApp.enableBigQueryExecution();// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()// instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Builds a data source specification.// TODO (developer): Update the project ID to your own Google Cloud project ID.constdataSourceSpec=SpreadsheetApp.newDataSourceSpec().asBigQuery().setProjectId('project-id-1').setTableProjectId('bigquery-public-data').setDatasetId('ncaa_basketball').setTableId('mbb_historical_teams_games').build();// Adds the data source and its data to the spreadsheet.ss.insertDataSourceSheet(dataSourceSpec);

Parameters

NameTypeDescription
specDataSourceSpecThe data source specification to insert with.

Return

DataSourceSheet — The new 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

insertImage(blobSource, column, row)

Inserts aSpreadsheet 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 aSpreadsheet 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

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

insertSheet()

Inserts a new sheet into the spreadsheet, using a default sheet name. The new sheet becomes theactive sheet.

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

Return

Sheet — The new 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

insertSheet(sheetIndex)

Inserts a new sheet into the spreadsheet at the given index. The new sheet becomes the activesheet.

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

Parameters

NameTypeDescription
sheetIndexIntegerThe index of the newly created sheet. To insert a sheet as the first one in the spreadsheet, set it to 0.

Return

Sheet — The new 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

insertSheet(sheetIndex, options)

Inserts a new sheet into the spreadsheet at the given index and uses optional advancedarguments. The new sheet becomes the active sheet.

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

Parameters

NameTypeDescription
sheetIndexIntegerThe index of the newly created sheet. To insert a sheet as the first one in the spreadsheet, set it to 0.
optionsObjectOptional JavaScript advanced arguments.

Advanced parameters

NameTypeDescription
templateSheetAll data from the template sheet object is copied to the new sheet. The sheet template must be one of the sheets of the Spreadsheet object.

Return

Sheet — The new 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

insertSheet(options)

Inserts a new sheet into the spreadsheet, using a default sheet name and optional advancedarguments. The new sheet becomes the active sheet.

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

Parameters

NameTypeDescription
optionsObjectOptional JavaScript advanced arguments, listed below.

Advanced parameters

NameTypeDescription
templateSheetAll data from the template sheet object is copied to the new sheet. The sheet template must be one of the sheets of this Spreadsheet object.

Return

Sheet — The new 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

insertSheet(sheetName)

Inserts a new sheet into the spreadsheet with the given name. The new sheet becomes the activesheet.

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

Parameters

NameTypeDescription
sheetNameStringThe name of the new sheet.

Return

Sheet — The new 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

insertSheet(sheetName, sheetIndex)

Inserts a new sheet into the spreadsheet with the given name at the given index. The new sheetbecomes the active sheet.

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

Parameters

NameTypeDescription
sheetNameStringThe name of the new sheet.
sheetIndexIntegerThe index of the newly created sheet. To insert a sheet as the first one in the spreadsheet, set it to 0.

Return

Sheet — The new 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

insertSheet(sheetName, sheetIndex, options)

Inserts a new sheet into the spreadsheet with the given name at the given index and usesoptional advanced arguments. The new sheet becomes the active sheet.

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

Parameters

NameTypeDescription
sheetNameStringThe name of the new sheet.
sheetIndexIntegerThe index of the newly inserted sheet. To insert a sheet as the first one in a spreadsheet, set it to 0.
optionsObjectOptional JavaScript advanced arguments.

Advanced parameters

NameTypeDescription
templateSheetAll data from the template sheet object is copied to the new sheet. The sheet template must be one of the sheets of the Spreadsheet object.

Return

Sheet — The new 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

insertSheet(sheetName, options)

Inserts a new sheet into the spreadsheet with the given name and uses optional advancedarguments. The new sheet becomes the active sheet.

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

Parameters

NameTypeDescription
sheetNameStringThe name of the new sheet.
optionsObjectOptional JavaScript advanced arguments.

Advanced parameters

NameTypeDescription
templateSheetAll data from the template sheet object is copied to the new sheet. The sheet template must be one of the sheets of the Spreadsheet object.

Return

Sheet — The new 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

insertSheetWithDataSourceTable(spec)

Inserts a new sheet in the spreadsheet, creates aDataSourceTable spanning theentire sheet with the given data source specification, and starts data execution. As a sideeffect, makes the new sheet the active sheet.

Throws an exception if the data source type is not enabled. UseSpreadsheetApp#enable...Execution() methods to enable data execution for specific data sourcetype.

// Activates BigQuery operations.SpreadsheetApp.enableBigQueryExecution();// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()// instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Adds a sheet and sets cell A1 as the parameter cell.constparameterCell=ss.insertSheet('parameterSheet').getRange('A1');// Sets the value of the parameter cell to 'Duke'.parameterCell.setValue('Duke');constquery='select * from `bigquery-public-data`.`ncaa_basketball`.'+'`mbb_historical_tournament_games` WHERE win_school_ncaa = @SCHOOL';// Adds a data source with a query parameter.// TODO(developer): Update the project ID to your own Google Cloud project ID.constdataSourceSpec=SpreadsheetApp.newDataSourceSpec().asBigQuery().setProjectId('project-id-1').setRawQuery(query).setParameterFromCell('SCHOOL','parameterSheet!A1').build();// Adds sheets for the data source and data source table to the spreadsheet.ss.insertSheetWithDataSourceTable(dataSourceSpec);

Parameters

NameTypeDescription
specDataSourceSpecThe data source specification to insert with.

Return

Sheet — The new 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

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

isIterativeCalculationEnabled()

Returns whether iterative calculation is activated in this spreadsheet.

// 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',);// Activates iterative calculation on the spreadsheet.ss.setIterativeCalculationEnabled(true);// Logs whether iterative calculation is activated for the spreadsheet.console.log(ss.isIterativeCalculationEnabled());

Return

Booleantrue if iterative calculation is activated,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

moveActiveSheet(pos)

Moves the active sheet to the given position in the list of sheets. Throws an exception if theposition is negative or greater than the number of sheets.

// This example assumes that there are 2 sheets in the current// active spreadsheet: one named "first" in position 1 and another named// "second" in position 2.constspreadsheet=SpreadsheetApp.getActiveSpreadsheet();// Gets the "first" sheet and activates it.constsheet=spreadsheet.getSheetByName('first').activate();// Logs 'Current index of sheet: 1'console.log('Current index of sheet: %s',sheet.getIndex());spreadsheet.moveActiveSheet(2);// Logs 'New index of sheet: 2'console.log('New index of sheet: %s',sheet.getIndex());

Parameters

NameTypeDescription
posIntegerThe 1-index position to move the active sheet to in the list of sheets.

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

moveChartToObjectSheet(chart)

Creates a newSheetType.OBJECT sheet and moves the provided chart to it. If the chartis already on its own sheet, that sheet is returned without creating a new one.

constsheet=SpreadsheetApp.getActiveSheet();constchart=sheet.newChart().setPosition(1,1,0,0).build();sheet.insertChart(chart);constobjectSheet=SpreadsheetApp.getActive().moveChartToObjectSheet(chart);

Parameters

NameTypeDescription
chartEmbeddedChartThe chart to move.

Return

Sheet — The sheet that the chart is on.

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

refreshAllDataSources()

Refreshes all supported data sources and their linked data source objects, skipping invaliddata source objects.

UseSpreadsheetApp#enable...Execution() methods to enable data execution forspecific data source type.

// Activates BigQuery operations.SpreadsheetApp.enableBigQueryExecution();// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()// instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Gets the first data source sheet on the spreadsheet.constdataSheet=ss.getDataSourceSheets()[0];// Refreshes all data sources on the spreadsheet.ss.refreshAllDataSources();// Logs the last refreshed time of the first data source sheet.console.log(`Last refresh time:${dataSheet.getStatus().getLastRefreshedTime()}`,);

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

removeEditor(emailAddress)

Removes the given user from the list of editors for theSpreadsheet. This method doesn'tblock users from accessing theSpreadsheet if they belong to a class of users who havegeneral access—for example, if theSpreadsheet is shared with the user's entiredomain, or if theSpreadsheet is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Spreadsheet — ThisSpreadsheet, 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

removeEditor(user)

Removes the given user from the list of editors for theSpreadsheet. This method doesn'tblock users from accessing theSpreadsheet if they belong to a class of users who havegeneral access—for example, if theSpreadsheet is shared with the user's entiredomain, or if theSpreadsheet is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

Parameters

NameTypeDescription
userUserA representation of the user to remove.

Return

Spreadsheet — ThisSpreadsheet, 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

removeMenu(name)

Removes a menu that was added byaddMenu(name, subMenus). Thename argumentshould have the same value as the corresponding call toaddMenu(name, subMenus).

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

Parameters

NameTypeDescription
nameStringThe name of the menu 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

removeNamedRange(name)

Deletes a named range with the given name. Throws an exception if no range with the given nameis found in the spreadsheet.

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

Parameters

NameTypeDescription
nameStringThe range name.

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

removeViewer(emailAddress)

Removes the given user from the list of viewers and commenters for theSpreadsheet. Thismethod has no effect if the user is an editor, not a viewer or commenter. This method alsodoesn't block users from accessing theSpreadsheet if they belong to a class of users whohave general access—for example, if theSpreadsheet is shared with the user'sentire domain, or if theSpreadsheet is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Spreadsheet — ThisSpreadsheet 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

removeViewer(user)

Removes the given user from the list of viewers and commenters for theSpreadsheet. Thismethod has no effect if the user is an editor, not a viewer. This method also doesn't blockusers from accessing theSpreadsheet if they belong to a class of users who have generalaccess—for example, if theSpreadsheet is shared with the user's entire domain, orif theSpreadsheet is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

Parameters

NameTypeDescription
userUserA representation of the user to remove.

Return

Spreadsheet — ThisSpreadsheet 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

rename(newName)

Renames the document.

constss=SpreadsheetApp.getActiveSpreadsheet();ss.rename('This is the new name');

Parameters

NameTypeDescription
newNameStringThe new name for the document.

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

renameActiveSheet(newName)

Renames the current active sheet to the given new name.

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

Parameters

NameTypeDescription
newNameStringThe new name for the current active 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

resetSpreadsheetTheme()

Removes the applied theme and sets the default theme on the spreadsheet.

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

Return

SpreadsheetTheme — The default theme.

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

setActiveSheet(sheet)

Sets the given sheet to be the active sheet in the spreadsheet. The Google Sheets UI displaysthe chosen sheet unless the sheet belongs to a different spreadsheet.

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

Parameters

NameTypeDescription
sheetSheetThe sheet to set as the active sheet.

Return

Sheet — The active 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

setActiveSheet(sheet, restoreSelection)

Sets the given sheet to be the active sheet in the spreadsheet, with an option to restore themost recent selection within that sheet. The Google Sheets UI displays the chosen sheet unlessthe sheet belongs to a different spreadsheet.

constspreadsheet=SpreadsheetApp.getActiveSpreadsheet();constfirstSheet=spreadsheet.getSheets()[0];constsecondSheet=spreadsheet.getSheets()[1];// Set the first sheet as the active sheet and select the range D4:F4.spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate();// Switch to the second sheet to do some work.spreadsheet.setActiveSheet(secondSheet);// Switch back to first sheet, and restore its selection.spreadsheet.setActiveSheet(firstSheet,true);// The selection of first sheet is restored, and it logs D4:F4constrange=spreadsheet.getActiveSheet().getSelection().getActiveRange();Logger.log(range.getA1Notation());

Parameters

NameTypeDescription
sheetSheetThe new active sheet.
restoreSelectionBooleanTftrue, the most recent selection of the new active sheet becomes selected again as the new sheet becomes active; iffalse, the new sheet becomes active without changing the current selection.

Return

Sheet — The new active 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

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

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

setIterativeCalculationConvergenceThreshold(minThreshold)

Sets the minimum threshold value for iterative calculation. When the results of successivecalculation differ by less than this value, the iterative calculation stops. This value must benon-negative, and defaults to 0.05.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Sets the iterative calculation convergence threshold for the spreadsheet.ss.setIterativeCalculationConvergenceThreshold(2);// Logs the threshold to the console.console.log(ss.getIterativeCalculationConvergenceThreshold());

Parameters

NameTypeDescription
minThresholdNumberThe minimum convergence threshold (must be non-negative).

Return

Spreadsheet — This spreadsheet, 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

setIterativeCalculationEnabled(isEnabled)

Sets whether iterative calculation is activated in this spreadsheet. If the maximum number ofcalculation cycles and convergence threshold have not previously been set when the calculationis activated, they default to 50 and 0.05 respectively. If either has been set previously, theyretain their previous values.

// 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',);// Activates iterative calculation on the spreadsheet.ss.setIterativeCalculationEnabled(true);// Logs whether iterative calculation is activated for the spreadsheet.console.log(ss.isIterativeCalculationEnabled());

Parameters

NameTypeDescription
isEnabledBooleantrue if iterative calculation should be enabled;false otherwise.

Return

Spreadsheet — This spreadsheet, 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

setMaxIterativeCalculationCycles(maxIterations)

Sets the maximum number of calculation iterations that should be performed during iterativecalculation. This value must be between 1 and 10,000 (inclusive), and defaults to 50.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Sets the max iterative calculation cycles for the spreadsheet.ss.setMaxIterativeCalculationCycles(10);// Logs the max iterative calculation cycles to the console.console.log(ss.getMaxIterativeCalculationCycles());

Parameters

NameTypeDescription
maxIterationsIntegerThe maximum number of calculation iterations (between 1 and 10,000).

Return

Spreadsheet — This spreadsheet, 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

setNamedRange(name, range)

Names a range.

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

Parameters

NameTypeDescription
nameStringThe name to give the range.
rangeRangeThe range specification.

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

setRecalculationInterval(recalculationInterval)

Sets how often this spreadsheet should recalculate.

// 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',);// Sets the  calculation interval for the spreadsheet to 'ON_CHANGE'.constinterval=ss.setRecalculationInterval(SpreadsheetApp.RecalculationInterval.ON_CHANGE,);// Logs the calculation interval to the console.console.log(interval);

Parameters

NameTypeDescription
recalculationIntervalRecalculationIntervalThe new recalculation interval.

Return

Spreadsheet — This spreadsheet, 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, useSheet.setRowHeightsForced(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

setSpreadsheetLocale(locale)

Sets the spreadsheet locale.

// Opens the spreadsheet file by its URL. If you created your script from within// a Google Sheets spreadsheet, you can use// SpreadsheetApp.getActiveSpreadsheet() instead.// TODO(developer): Replace the URL with your own.constss=SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit',);// Sets the spreadsheet locale.ss.setSpreadsheetLocale('fr');// Gets the spreadsheet locale.constssLocale=ss.getSpreadsheetLocale();// Logs the locale to the console.console.log(ssLocale);

Parameters

NameTypeDescription
localeStringThe locale code to use (for example, 'en', 'fr', or 'en_US').

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

setSpreadsheetTheme(theme)

Sets a theme on the spreadsheet.

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

Parameters

NameTypeDescription
themeSpreadsheetThemeThe theme to apply.

Return

SpreadsheetTheme — The new current theme.

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

setSpreadsheetTimeZone(timezone)

Sets the time zone for the spreadsheet.

// 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',);// Sets the time zone of the spreadsheet.ss.setSpreadsheetTimeZone('America/New_York');// Gets the time zone of the spreadsheet.constssTimeZone=ss.getSpreadsheetTimeZone();// Logs the time zone to the console.console.log(ssTimeZone);

Parameters

NameTypeDescription
timezoneStringThe time zone, specified in "long" format (for example, "America/New_York", as listed byJoda.org).

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

show(userInterface)

Displays a custom user interface component in a dialog centered in the user's browser'sviewport. The server-side script's execution isnot suspended. To communicate with theserver side, the user interface component must make asynchronous callbacks to the server-sidescript.

If the server-side script previously displayed a dialog that has not yet been dismissed,then the existing dialog is replaced with the newly requested dialog's user interface.

The following code snippet displays a simpleHtmlService application in a dialog with thespecified title, height, and width:

consthtmlApp=HtmlService.createHtmlOutput('<p>A change of speed, a change of style...</p>',).setTitle('My HtmlService Application').setWidth(250).setHeight(300);SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);// The script resumes execution immediately after showing the dialog.

Parameters

NameTypeDescription
userInterfaceObjectAnHtmlOutput.

Authorization

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

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

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

toast(msg)

Shows a popup window in the lower right corner of the spreadsheet with the given message.

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

Parameters

NameTypeDescription
msgStringThe message to be shown in the toast.

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

toast(msg, title)

Shows a popup window in the lower right corner of the spreadsheet with the given message andtitle.

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

Parameters

NameTypeDescription
msgStringThe message to be shown in the toast.
titleStringThe optional title of the toast.

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

toast(msg, title, timeoutSeconds)

Shows a popup window in the lower right corner of the spreadsheet with the given title andmessage, that stays visible for a certain length of time.

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

Parameters

NameTypeDescription
msgStringThe message to be shown in the toast.
titleStringThe optional title of the toast.
timeoutSecondsNumberThe timeout in seconds; ifnull, the toast defaults to 5 seconds; if negative, the toast remains until dismissed.

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

updateMenu(name, subMenus)

Updates a menu that was added byaddMenu(name, subMenus). Works exactly likeaddMenu(name, subMenus).

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

Parameters

NameTypeDescription
nameStringThe name of the menu to update.
subMenusObject[]An array of JavaScript maps withname andfunctionName parameters. You can use functions from included libraries, such as Library.libFunction1.

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

waitForAllDataExecutionsCompletion(timeoutInSeconds)

Waits until all the current executions in the spreadsheet complete, timing out after theprovided number of seconds. Throws an exception if the executions are not completed when timingout, but does not cancel the data executions.

Parameters

NameTypeDescription
timeoutInSecondsIntegerThe time to wait for data executions, in seconds. The maximum is 300 seconds.

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, useSheet.getProtections(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

isAnonymousView()

Deprecated. As of January 2014 this function is deprecated and not available in the new version of Google Sheets.

Indicates whether the document allows anonymous viewing. As this is no longer supported in thenew version of Google Sheets, useFile.getSharingAccess() andFile.getSharingPermission() instead.

// Determine if the document allows anonymous viewing via the Drive API.constss=SpreadsheetApp.getActiveSpreadsheet();constfile=DriveApp.getFileById(ss.getId());constaccess=file.getSharingAccess();constpermission=file.getSharingPermission();constisAnonymousAccess=access===DriveApp.Access.ANYONE||access===DriveApp.Access.ANYONE_WITH_LINK;constisAnonymousEdit=isAnonymousAccess &&permission!==DriveApp.Permission.NONE;

Return

Booleantrue if the document allows anonymous viewing,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

See also


isAnonymousWrite()

Deprecated. As of January 2014 this function is deprecated and not available in the new version of Google Sheets.

Indicates whether the document allows edits from anonymous users. As this is no longersupported in thenew version ofGoogle Sheets, useFile.getSharingAccess() andFile.getSharingPermission() instead.

// Determine if the document allow anonymous edits via the Drive API.constss=SpreadsheetApp.getActiveSpreadsheet();constfile=DriveApp.getFileById(ss.getId());constaccess=file.getSharingAccess();constpermission=file.getSharingPermission();constisAnonymousAccess=access===DriveApp.Access.ANYONE||access===DriveApp.Access.ANYONE_WITH_LINK;constisAnonymousEdit=isAnonymousAccess &&permission===DriveApp.Permission.EDIT;

Return

Booleantrue if the document allows anonymous editing,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

See also


setAnonymousAccess(anonymousReadAllowed, anonymousWriteAllowed)

Deprecated. As of January 2014 this function is deprecated and not available in the new version of Google Sheets.

Sets the document's policy on anonymous reading and writing. As this is no longer supported inthenew version of Google Sheets,useFile.setSharing(accessType, permissionType) as an alternative.

// Set the document's policy on anonymous reading and writing via the Drive API.constss=SpreadsheetApp.getActiveSpreadsheet();constfile=DriveApp.getFileById(ss.getId());// Set anonymous read.file.setSharing(DriveApp.Access.ANYONE,DriveApp.Permission.VIEW);// Set anonymous write.file.setSharing(DriveApp.Access.ANYONE,DriveApp.Permission.EDIT);// Disable anonymous access.file.setSharing(DriveApp.Access.PRIVATE,file.getSharingPermission());

Parameters

NameTypeDescription
anonymousReadAllowedBooleantrue to allow anonymous reads;false otherwise.
anonymousWriteAllowedBooleantrue to allow anonymous reads;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

See also


setSheetProtection(permissions)

Deprecated. For spreadsheets created in the newer version of Google Sheets, useSheet.protect(), 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-08-04 UTC.