Google is committed to advancing racial equity for Black communities. See how.

Class DriveApp

DriveApp

Allows scripts to create, find, and modify files and folders in Google Drive.

// Log the name of every file in the user's Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
  Logger.log(file.getName());
}

Properties

PropertyTypeDescription
AccessAccessAn enum representing classes of users who can access a file or folder, besides any individual users who have been explicitly given access.
PermissionPermissionAn enum representing the permissions granted to users who can access a file or folder, besides any individual users who have been explicitly given access.

Methods

MethodReturn typeBrief description
continueFileIterator(continuationToken)FileIteratorResumes a file iteration using a continuation token from a previous iterator.
continueFolderIterator(continuationToken)FolderIteratorResumes a folder iteration using a continuation token from a previous iterator.
createFile(blob)FileCreates a file in the root of the user's Drive from a given Blob of arbitrary data.
createFile(name, content)FileCreates a text file in the root of the user's Drive with the given name and contents.
createFile(name, content, mimeType)FileCreates a file in the root of the user's Drive with the given name, contents, and MIME type.
createFolder(name)FolderCreates a folder in the root of the user's Drive with the given name.
createShortcut(targetId)FileCreates a shortcut to the provided Drive item ID, and returns it.
enforceSingleParent(value)voidEnables or disables enforceSingleParent behavior for all calls affecting item parents.
getFileById(id)FileGets the file with the given ID.
getFiles()FileIteratorGets a collection of all files in the user's Drive.
getFilesByName(name)FileIteratorGets a collection of all files in the user's Drive that have the given name.
getFilesByType(mimeType)FileIteratorGets a collection of all files in the user's Drive that have the given MIME type.
getFolderById(id)FolderGets the folder with the given ID.
getFolders()FolderIteratorGets a collection of all folders in the user's Drive.
getFoldersByName(name)FolderIteratorGets a collection of all folders in the user's Drive that have the given name.
getRootFolder()FolderGets the folder at the root of the user's Drive.
getStorageLimit()IntegerGets the number of bytes the user is allowed to store in Drive.
getStorageUsed()IntegerGets the number of bytes the user is currently storing in Drive.
getTrashedFiles()FileIteratorGets a collection of all the files in the trash of the user's Drive.
getTrashedFolders()FolderIteratorGets a collection of all the folders in the trash of the user's Drive.
searchFiles(params)FileIteratorGets a collection of all files in the user's Drive that match the given search criteria.
searchFolders(params)FolderIteratorGets a collection of all folders in the user's Drive that match the given search criteria.

Detailed documentation

continueFileIterator(continuationToken)

Resumes a file iteration using a continuation token from a previous iterator. This method is useful if processing an iterator in one execution would exceed the maximum execution time. Continuation tokens are generally valid for one week.

Parameters

NameTypeDescription
continuationTokenStringa continuation token from a previous file iterator

Return

FileIterator — a collection of files that remained in a previous iterator when the continuation token was generated


continueFolderIterator(continuationToken)

Resumes a folder iteration using a continuation token from a previous iterator. This method is useful if processing an iterator in one execution would exceed the maximum execution time. Continuation tokens are generally valid for one week.

Parameters

NameTypeDescription
continuationTokenStringa continuation token from a previous folder iterator

Return

FolderIterator — a collection of folders that remained in a previous iterator when the continuation token was generated


createFile(blob)

Creates a file in the root of the user's Drive from a given Blob of arbitrary data.

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.createFile(blob);

Parameters

NameTypeDescription
blobBlobSourcethe data for the new file

Return

File — the new file

Authorization

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

  • https://www.googleapis.com/auth/drive

createFile(name, content)

Creates a text file in the root of the user's Drive with the given name and contents. Throws an exception if content is larger than 50 MB.

// Create a text file with the content "Hello, world!"
DriveApp.createFile('New Text File', 'Hello, world!');

Parameters

NameTypeDescription
nameStringthe name of the new file
contentStringthe content for the new file

Return

File — the new file

Authorization

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

  • https://www.googleapis.com/auth/drive

createFile(name, content, mimeType)

Creates a file in the root of the user's Drive with the given name, contents, and MIME type. Throws an exception if content is larger than 10MB.

// Create an HTML file with the content "Hello, world!"
DriveApp.createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);

Parameters

NameTypeDescription
nameStringthe name of the new file
contentStringthe content for the new file
mimeTypeStringthe MIME type of the new file

Return

File — the new file

Authorization

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

  • https://www.googleapis.com/auth/drive

createFolder(name)

Creates a folder in the root of the user's Drive with the given name.

Parameters

NameTypeDescription
nameStringthe name of the new folder

Return

Folder — the new folder

Authorization

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

  • https://www.googleapis.com/auth/drive

createShortcut(targetId)

Creates a shortcut to the provided Drive item ID, and returns it.

Parameters

NameTypeDescription
targetIdStringThe file ID of the target file or folder.

Return

File — The new shortcut.

Authorization

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

  • https://www.googleapis.com/auth/drive

enforceSingleParent(value)

Enables or disables enforceSingleParent behavior for all calls affecting item parents.

See the Simplifying Google Drive’s folder structure and sharing models blog for more details.

Parameters

NameTypeDescription
valueBooleanThe new state of the enforceSingleParent flag.

Authorization

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

  • https://www.googleapis.com/auth/drive

getFileById(id)

Gets the file with the given ID. Throws a scripting exception if the file does not exist or the user does not have permission to access it.

Parameters

NameTypeDescription
idStringthe ID of the file

Return

File — the file with the given ID

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFiles()

Gets a collection of all files in the user's Drive.

Return

FileIterator — a collection of all files in the user's Drive

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByName(name)

Gets a collection of all files in the user's Drive that have the given name.

Parameters

NameTypeDescription
nameStringthe name of the files to find

Return

FileIterator — a collection of all files in the user's Drive that have the given name

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByType(mimeType)

Gets a collection of all files in the user's Drive that have the given MIME type.

Parameters

NameTypeDescription
mimeTypeStringthe MIME type of the files to find

Return

FileIterator — a collection of all files in the user's Drive that have the given MIME type

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolderById(id)

Gets the folder with the given ID. Throws a scripting exception if the folder does not exist or the user does not have permission to access it.

Parameters

NameTypeDescription
idStringthe ID of the folder

Return

Folder — the folder with the given ID

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolders()

Gets a collection of all folders in the user's Drive.

Return

FolderIterator — a collection of all folders in the user's Drive

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFoldersByName(name)

Gets a collection of all folders in the user's Drive that have the given name.

Parameters

NameTypeDescription
nameStringthe name of the folders to find

Return

FolderIterator — a collection of all folders in the user's Drive that have the given name

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getRootFolder()

Gets the folder at the root of the user's Drive.

Return

Folder — the root folder of the user's Drive

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getStorageLimit()

Gets the number of bytes the user is allowed to store in Drive.

Return

Integer — the number of bytes the user is allowed to store in Drive

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getStorageUsed()

Gets the number of bytes the user is currently storing in Drive.

Return

Integer — the number of bytes the user is currently storing in Drive

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getTrashedFiles()

Gets a collection of all the files in the trash of the user's Drive.

Return

FileIterator — a collection of files in the trash

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getTrashedFolders()

Gets a collection of all the folders in the trash of the user's Drive.

Return

FolderIterator — a collection of folders in the trash

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

searchFiles(params)

Gets a collection of all files in the user's Drive that match the given search criteria. The search criteria are detailed the Google Drive SDK documentation. Note that the params argument is a query string that may contain string values, so take care to escape quotation marks correctly (for example "title contains 'Gulliver\\'s Travels'" or 'title contains "Gulliver\'s Travels"').

// Log the name of every file in the user's Drive that modified after February 28,
// 2013 whose name contains "untitled".
var files = DriveApp.searchFiles(
    'modifiedDate > "2013-02-28" and title contains "untitled"');
while (files.hasNext()) {
  var file = files.next();
  Logger.log(file.getName());
}

Parameters

NameTypeDescription
paramsStringthe search criteria, as detailed in the Google Drive SDK documentation

Return

FileIterator — a collection of all files in the user's Drive that match the search criteria

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

searchFolders(params)

Gets a collection of all folders in the user's Drive that match the given search criteria. The search criteria are detailed the Google Drive SDK documentation. Note that the params argument is a query string that may contain string values, so take care to escape quotation marks correctly (for example "title contains 'Gulliver\\'s Travels'" or 'title contains "Gulliver\'s Travels"').

// Log the name of every folder in the user's Drive that you own and is starred.
var folders = DriveApp.searchFolders('starred = true and "me" in owners');
while (folders.hasNext()) {
  var folder = folders.next();
  Logger.log(folder.getName());
}

Parameters

NameTypeDescription
paramsStringthe search criteria, as detailed in the Google Drive SDK documentation

Return

FolderIterator — a collection of all folders in the user's Drive that match the search criteria

Authorization

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

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

Deprecated methods