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
Property | Type | Description |
---|---|---|
Access | Access | An enum representing classes of users who can access a file or folder, besides any individual users who have been explicitly given access. |
Permission | Permission | An 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
Method | Return type | Brief description |
---|---|---|
addFile(child) | Folder | Adds the given file to the root of the user's Drive. |
addFolder(child) | Folder | Adds the given folder to the root of the user's Drive. |
continueFileIterator(continuationToken) | FileIterator | Resumes a file iteration using a continuation token from a previous iterator. |
continueFolderIterator(continuationToken) | FolderIterator | Resumes a folder iteration using a continuation token from a previous iterator. |
createFile(blob) | File | Creates a file in the root of the user's Drive from a given Blob of arbitrary data. |
createFile(name, content) | File | Creates a text file in the root of the user's Drive with the given name and contents. |
createFile(name, content, mimeType) | File | Creates a file in the root of the user's Drive with the given name, contents, and MIME type. |
createFolder(name) | Folder | Creates a folder in the root of the user's Drive with the given name. |
getFileById(id) | File | Gets the file with the given ID. |
getFiles() | FileIterator | Gets a collection of all files in the user's Drive. |
getFilesByName(name) | FileIterator | Gets a collection of all files in the user's Drive that have the given name. |
getFilesByType(mimeType) | FileIterator | Gets a collection of all files in the user's Drive that have the given MIME type. |
getFolderById(id) | Folder | Gets the folder with the given ID. |
getFolders() | FolderIterator | Gets a collection of all folders in the user's Drive. |
getFoldersByName(name) | FolderIterator | Gets a collection of all folders in the user's Drive that have the given name. |
getRootFolder() | Folder | Gets the folder at the root of the user's Drive. |
getStorageLimit() | Integer | Gets the number of bytes the user is allowed to store in Drive. |
getStorageUsed() | Integer | Gets the number of bytes the user is currently storing in Drive. |
getTrashedFiles() | FileIterator | Gets a collection of all the files in the trash of the user's Drive. |
getTrashedFolders() | FolderIterator | Gets a collection of all the folders in the trash of the user's Drive. |
removeFile(child) | Folder | Removes the given file from the root of the user's Drive. |
removeFolder(child) | Folder | Removes the given folder from the root of the user's Drive. |
searchFiles(params) | FileIterator | Gets a collection of all files in the user's Drive that match the given search criteria. |
searchFolders(params) | FolderIterator | Gets a collection of all folders in the user's Drive that match the given search criteria. |
Detailed documentation
addFile(child)
Adds the given file to the root of the user's Drive. This method does not move the file out of its existing parent folder; a file can have more than one parent simultaneously.
Parameters
Name | Type | Description |
---|---|---|
child | File | the child file to add |
Return
Folder
— this The new parent of the file added as a child.
Authorization
Scripts that use this method require authorization with one or more of the following scopes:
-
https://www.googleapis.com/auth/drive
addFolder(child)
Adds the given folder to the root of the user's Drive. This method does not move the folder out of its existing parent folder; a folder can have more than one parent simultaneously.
Parameters
Name | Type | Description |
---|---|---|
child | Folder | the child folder to add |
Return
Folder
— this The new parent of the folder added as a child.
Authorization
Scripts that use this method require authorization with one or more of the following scopes:
-
https://www.googleapis.com/auth/drive
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
Name | Type | Description |
---|---|---|
continuationToken | String | a 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
Name | Type | Description |
---|---|---|
continuationToken | String | a 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
Name | Type | Description |
---|---|---|
blob | BlobSource | the 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
Name | Type | Description |
---|---|---|
name | String | the name of the new file |
content | String | the 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
Name | Type | Description |
---|---|---|
name | String | the name of the new file |
content | String | the content for the new file |
mimeType | String | the 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
Name | Type | Description |
---|---|---|
name | String | the 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
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
Name | Type | Description |
---|---|---|
id | String | the 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
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
getFilesByName(name)
Gets a collection of all files in the user's Drive that have the given name.
Parameters
Name | Type | Description |
---|---|---|
name | String | the 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
getFilesByType(mimeType)
Gets a collection of all files in the user's Drive that have the given MIME type.
Parameters
Name | Type | Description |
---|---|---|
mimeType | String | the 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
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
Name | Type | Description |
---|---|---|
id | String | the 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
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
getFoldersByName(name)
Gets a collection of all folders in the user's Drive that have the given name.
Parameters
Name | Type | Description |
---|---|---|
name | String | the 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
getRootFolder()
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
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
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
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
removeFile(child)
Removes the given file from the root of the user's Drive. This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.
Parameters
Name | Type | Description |
---|---|---|
child | File | the child file to remove |
Return
Folder
— this The previous parent of the child.
Authorization
Scripts that use this method require authorization with one or more of the following scopes:
-
https://www.googleapis.com/auth/drive
removeFolder(child)
Removes the given folder from the root of the user's Drive. This method does not delete the folder or its contents, but if a folder is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.
Parameters
Name | Type | Description |
---|---|---|
child | Folder | the child folder to remove |
Return
Folder
— this The previous parent of the child.
Authorization
Scripts that use this method require authorization with one or more of the following scopes:
-
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
Name | Type | Description |
---|---|---|
params | String | the 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
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
Name | Type | Description |
---|---|---|
params | String | the 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