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
addFile(child)FolderAdds the given file to the root of the user's Drive.
addFolder(child)FolderAdds the given folder to the root of the user's Drive.
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.
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.
removeFile(child)FolderRemoves the given file from the root of the user's Drive.
removeFolder(child)FolderRemoves the given folder from the root 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

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

NameTypeDescription
childFilethe child file to add

Return

Folder — this The new parent of the file added as a child.


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

NameTypeDescription
childFolderthe child folder to add

Return

Folder — this The new parent of the folder added as a child.


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


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


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


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


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


getFiles()

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

Return

FileIterator — a collection of all files in the user's 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


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


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


getFolders()

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

Return

FolderIterator — a collection of all folders in the user's 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


getRootFolder()

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

Return

Folder — the root folder of the user's 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


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


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


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


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

NameTypeDescription
childFilethe child file to remove

Return

Folder — this The previous parent of the child.


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

NameTypeDescription
childFolderthe child folder to remove

Return

Folder — this The previous parent of the child.


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


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

Enviar comentários sobre…