Class File

  • Google DriveFile objects inDriveApp allow programmatic interaction with files.

  • The documentation lists various methods for managing file permissions, getting file information, and manipulating file content.

  • Specific methods exist for managing access, retrieving details like ID and name, and performing operations like copying or moving files.

  • Shortcut-specific methods provide information about the target of a shortcut file.

  • Scripts interacting with files require appropriate Google Drive authorization scopes.

File

A file in Google Drive. Files can be accessed or created fromDriveApp.

// Trash every untitled spreadsheet that hasn't been updated in a week.constfiles=DriveApp.getFilesByName('Untitled spreadsheet');while(files.hasNext()){constfile=files.next();if(newDate()-file.getLastUpdated() >7*24*60*60*1000){file.setTrashed(true);}}

Methods

MethodReturn typeBrief description
addCommenter(emailAddress)FileAdd the given user to the list of commenters for theFile.
addCommenter(user)FileAdd the given user to the list of commenters for theFile.
addCommenters(emailAddresses)FileAdd the given array of users to the list of commenters for theFile.
addEditor(emailAddress)FileAdds the given user to the list of editors for theFile.
addEditor(user)FileAdds the given user to the list of editors for theFile.
addEditors(emailAddresses)FileAdds the given array of users to the list of editors for theFile.
addViewer(emailAddress)FileAdds the given user to the list of viewers for theFile.
addViewer(user)FileAdds the given user to the list of viewers for theFile.
addViewers(emailAddresses)FileAdds the given array of users to the list of viewers for theFile.
getAccess(email)PermissionGets the permission granted to a specific user.
getAccess(user)PermissionGets the permission granted to a specific user.
getAs(contentType)BlobReturn the data inside this object as a blob converted to the specified content type.
getBlob()BlobReturn the data inside this object as a blob.
getDateCreated()DateGets the date theFile was created.
getDescription()StringGets the description for theFile.
getDownloadUrl()StringGets the URL that can be used to download the file.
getEditors()User[]Gets the list of editors for thisFile.
getId()StringGets the ID of theFile.
getLastUpdated()DateGets the date theFile was last updated.
getMimeType()StringGets the MIME type of the file.
getName()StringGets the name of theFile.
getOwner()UserGets the file owner.
getParents()FolderIteratorGets a collection of folders that are immediate parents of theFile.
getResourceKey()StringGets the resource key of theFile that is required to access items thathave been shared using a link.
getSecurityUpdateEligible()BooleanGets whether thisFile is eligible to apply the security update thatrequires a resource key for access when it's shared using a link.
getSecurityUpdateEnabled()BooleanGets whether thisFile requires a resource key for access when it'sshared using a link.
getSharingAccess()AccessGets which class of users can access theFile, besides any individualusers who have been explicitly given access.
getSharingPermission()PermissionGets the permission granted to those users who can access theFile,besides any individual users who have been explicitly given access.
getSize()IntegerGets the number of bytes used to store theFile in Drive.
getTargetId()StringIf this is a Shortcut, returns the ID of the item it points to.
getTargetMimeType()StringIf this is a Shortcut, returns the mime type of the item it points to.
getTargetResourceKey()StringIf the file is a shortcut, returns the resource key of the item it points to.
getThumbnail()Blob|nullGets a thumbnail image for the file, ornull if no thumbnail exists.
getUrl()StringGets the URL that can be used to open theFile in a Google App likeDrive or Docs.
getViewers()User[]Gets the list of viewers and commenters for thisFile.
isShareableByEditors()BooleanDetermines whether users with edit permissions to theFile are allowed toshare with other users or change the permissions.
isStarred()BooleanDetermines whether theFile has been starred in the user's Drive.
isTrashed()BooleanDetermines whether theFile is in the trash of the user's Drive.
makeCopy()FileCreates a copy of the file.
makeCopy(destination)FileCreates a copy of the file in the destination directory.
makeCopy(name)FileCreates a copy of the file and names it with the name provided.
makeCopy(name, destination)FileCreates a copy of the file in the destination directory and names it with the name provided.
moveTo(destination)FileMoves this item to the provided destination folder.
removeCommenter(emailAddress)FileRemoves the given user from the list of commenters for theFile.
removeCommenter(user)FileRemoves the given user from the list of commenters for theFile.
removeEditor(emailAddress)FileRemoves the given user from the list of editors for theFile.
removeEditor(user)FileRemoves the given user from the list of editors for theFile.
removeViewer(emailAddress)FileRemoves the given user from the list of viewers and commenters for theFile.
removeViewer(user)FileRemoves the given user from the list of viewers and commenters for theFile.
revokePermissions(emailAddress)FileRevokes the access to theFile granted to the given user.
revokePermissions(user)FileRevokes the access to theFile granted to the given user.
setContent(content)FileOverwrites the content of the file with a given replacement.
setDescription(description)FileSets the description for theFile.
setName(name)FileSets the name of theFile.
setOwner(emailAddress)FileChanges the owner of theFile.
setOwner(user)FileChanges the owner of theFile.
setSecurityUpdateEnabled(enabled)FileSets whether theFile requires a resource key for access when it'sshared using a link.
setShareableByEditors(shareable)FileSets whether users with edit permissions to theFile are allowed to sharewith other users or change the permissions.
setSharing(accessType, permissionType)FileSets which class of users can access theFile and what permissionsthose users are granted, besides any individual users who have been explicitly given access.
setStarred(starred)FileSets whether theFile is starred in the user's Drive.
setTrashed(trashed)FileSets whether theFile is in the trash of the user's Drive.

Detailed documentation

addCommenter(emailAddress)

Add the given user to the list of commenters for theFile. If the user wasalready on the list of viewers, this method promotes the user out of the list of viewers.

constfiles=DriveApp.getFilesByName('Test');// Loops through the fileswhile(files.hasNext()){constfile=files.next();file.addCommenter('hello@example.com');}

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

File — ThisFile, for chaining.

Authorization

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

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

addCommenter(user)

Add the given user to the list of commenters for theFile. If the user wasalready on the list of viewers, this method promotes the user out of the list of viewers.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Adds the active user as a commenter.while(files.hasNext()){constfile=files.next();file.addCommenter(Session.getActiveUser());}

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

File — ThisFile, for chaining.

Authorization

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

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

addCommenters(emailAddresses)

Add the given array of users to the list of commenters for theFile. If anyof the users were already on the list of viewers, this method promotes them out of the list ofviewers.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');while(files.hasNext()){constfile=files.next();// TODO(developer): Replace 'cloudysanfrancisco@gmail.com' and// 'baklavainthebalkans@gmail.com' with the email addresses to add as// commenters.constemails=['cloudysanfrancisco@gmail.com','baklavainthebalkans@gmail.com',];console.log(file.addCommenters(emails));}

Parameters

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

Return

File — ThisFile, for chaining.

Authorization

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

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

addEditor(emailAddress)

Adds the given user to the list of editors for theFile. 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

File — ThisFile, for chaining.

Authorization

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

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

addEditor(user)

Adds the given user to the list of editors for theFile. 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

File — ThisFile, for chaining.

Authorization

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

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

addEditors(emailAddresses)

Adds the given array of users to the list of editors for theFile. 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

File — ThisFile, for chaining.

Authorization

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

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

addViewer(emailAddress)

Adds the given user to the list of viewers for theFile. 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

File — ThisFile, for chaining.

Authorization

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

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

addViewer(user)

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

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

File — ThisFile, for chaining.

Authorization

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

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

addViewers(emailAddresses)

Adds the given array of users to the list of viewers for theFile. 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

File — ThisFile, for chaining.

Authorization

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

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

getAccess(email)

Gets the permission granted to a specific user. The method doesn't support returningpermissions for a Google Group or permissions inherited through Google Groups.

Parameters

NameTypeDescription
emailStringThe email address of the user whose permissions should be checked. Google Groups aren't supported.

Return

Permission — The permissions granted to the user.

Authorization

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

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

getAccess(user)

Gets the permission granted to a specific user. The method doesn't support returningpermissions for a Google Group or permissions inherited through Google Groups.

Parameters

NameTypeDescription
userUserA representation of the user whose permissions should be checked.

Return

Permission — The permissions granted to the user.

Authorization

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

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

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.


getBlob()

Return the data inside this object as a blob.

Return

Blob — The data as a blob.


getDateCreated()

Gets the date theFile was created.

Return

Date — the date theFile was created

Authorization

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

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

getDescription()

Gets the description for theFile.

Return

String — the description for theFile

Authorization

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

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

getDownloadUrl()

Gets the URL that can be used to download the file. Only users with permission to open the filein Google Drive can access the URL. You can use this URL in a browser to download the file, butyou can't use to fetch the file withUrlFetchApp. If you want the contents of thefile in the script, usegetBlob().

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files and logs the download URLs to the console.while(files.hasNext()){constfile=files.next();console.log(file.getDownloadUrl());}

Return

String — The URL that can be used to download the file.

Authorization

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

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

getEditors()

Gets the list of editors for thisFile. If the user who executes the scriptdoes not have edit access to theFile, this method returns an empty array.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Adds the email addresses in the array as editors of each file.// TODO(developer): Replace 'cloudysanfrancisco@gmail.com'// and 'baklavainthebalkans@gmail.com' with valid email addresses.file.addEditors(['cloudysanfrancisco@gmail.com','baklavainthebalkans@gmail.com',]);// Gets a list of the file editors.consteditors=file.getEditors();// For each file, logs the editors' email addresses to the console.for(consteditorofeditors){console.log(editor.getEmail());}}

Return

User[] — If the user has edit access for thisFile, returns the list of editors. If the user doesn't have edit access, returns an empty array.

Authorization

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

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

getId()

Gets the ID of theFile.

Return

String — the ID of theFile

Authorization

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

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

getLastUpdated()

Gets the date theFile was last updated.

Return

Date — the date theFile was last updated

Authorization

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

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

getMimeType()

Gets the MIME type of the file.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files and logs the MIME type to the console.while(files.hasNext()){constfile=files.next();console.log(file.getMimeType());}

Return

String — The MIME type of the file.

Authorization

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

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

getName()

Gets the name of theFile.

Return

String — the name of theFile

Authorization

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

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

getOwner()

Gets the file owner.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files and logs the names of the file owners to the console.while(files.hasNext()){constfile=files.next();console.log(file.getOwner().getName());}

Return

User — The file owner.

Authorization

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

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

getParents()

Gets a collection of folders that are immediate parents of theFile.

Return

FolderIterator — a collection of folders that are immediate parents of theFile

Authorization

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

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

getResourceKey()

Gets the resource key of theFile that is required to access items thathave been shared using a link.

Return

String — The resource key of theFile.

Authorization

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

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

getSecurityUpdateEligible()

Gets whether thisFile is eligible to apply the security update thatrequires a resource key for access when it's shared using a link.

Drive requires a resource key to access some files or folders that have been shared using alink. This change is part of a security update. The update is turned on by default for eligiblefiles and folders. To turn the resource key requirement on or off for eligible files, usesetSecurityUpdateEnabled.

Learn more about theSecurity update for Google Drive.

Return

Boolean — Whether the resource key requirement can be applied for theFile.

Authorization

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

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

getSecurityUpdateEnabled()

Gets whether thisFile requires a resource key for access when it'sshared using a link. This requirement is turned on by default for eligible files and folders.To turn the resource key requirement on or off for eligible files, usesetSecurityUpdateEnabled.

Learn more about theSecurity update for Google Drive.

Return

Boolean — Whether the resource key requirement is enabled for thisFile.

Authorization

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

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

getSharingAccess()

Gets which class of users can access theFile, besides any individualusers who have been explicitly given access.

Return

Access — which class of users can access theFile

Authorization

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

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

getSharingPermission()

Gets the permission granted to those users who can access theFile,besides any individual users who have been explicitly given access.

Return

Permission — the permissions granted to users who can access theFile

Authorization

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

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

getSize()

Gets the number of bytes used to store theFile in Drive. Note thatGoogle Workspace application files do not count toward Drive storage limits and thus return0 bytes.

Return

Integer — the number of bytes used to store theFile in Drive

Authorization

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

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

getTargetId()

If this is a Shortcut, returns the ID of the item it points to.

Otherwise it returnsnull.

// The ID of the file for which to make a shortcut and the ID of// the folder to which you want to add the shortcut.// TODO(developer): Replace the file and folder IDs with your IDs.constfileId='abc123456';constfolderId='xyz987654';// Gets the folder to add the shortcut to.constfolder=DriveApp.getFolderById(folderId);// Creates a shortcut of the file and moves it to the specified folder.constshortcut=DriveApp.createShortcut(fileId).moveTo(folder);// Logs the target ID of the shortcut.console.log(`${shortcut.getName()}=${shortcut.getTargetId()}`);

Return

String — The target item ID.


getTargetMimeType()

If this is a Shortcut, returns the mime type of the item it points to.

Otherwise it returnsnull.

// The ID of the file for which to make a shortcut and the ID of// the folder to which you want to add the shortcut.// TODO(developer): Replace the file and folder IDs with your IDs.constfileId='abc123456';constfolderId='xyz987654';// Gets the folder to add the shortcut to.constfolder=DriveApp.getFolderById(folderId);// Creates a shortcut of the file and moves it to the specified folder.constshortcut=DriveApp.createShortcut(fileId).moveTo(folder);// Logs the MIME type of the file that the shortcut points to.console.log(`MIME type of the shortcut:${shortcut.getTargetMimeType()}`);

Return

String — The target item mime type.


getTargetResourceKey()

If the file is a shortcut, returns the resource key of the item it points to. A resource key isan additional parameter that you need to pass to access files that have been shared using alink.

If the file isn't a shortcut, it returnsnull.

//  Gets a file by its ID.//  TODO(developer): Replace 'abc123456' with your file ID.constfile=DriveApp.getFileById('abc123456');// If the file is a shortcut, returns the resource key of the file that it// points to.console.log(file.getTargetResourceKey());

Return

String — The target item's resource key ornull if the file is not a shortcut.

Authorization

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

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

getThumbnail()

Gets a thumbnail image for the file, ornull if no thumbnail exists.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Logs the thumbnail image for each file to the console as a blob,// or null if no thumbnail exists.console.log(file.getThumbnail());}

Return

Blob|null — The thumbnail image for the file.

Authorization

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

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

getUrl()

Gets the URL that can be used to open theFile in a Google App likeDrive or Docs.

Return

String — the URL that can be used to view thisFile in a Google App like Drive or Docs

Authorization

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

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

getViewers()

Gets the list of viewers and commenters for thisFile. If the user whoexecutes the script does not have edit access to theFile, this methodreturns an empty array.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// For each file, logs the viewers' email addresses to the console.constviewers=file.getViewers();for(constviewerofviewers){console.log(viewer.getEmail());}}

Return

User[] — If the user has edit access for thisFile, returns the list of viewers and commenters. If the user doesn't have edit access, returns an empty array.

Authorization

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

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

isShareableByEditors()

Determines whether users with edit permissions to theFile are allowed toshare with other users or change the permissions.

Return

Booleantrue if users with edit permissions are allowed to share with other users or change the permissions;false if not

Authorization

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

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

isStarred()

Determines whether theFile has been starred in the user's Drive.

Return

Booleantrue if theFile is starred in the user's Drive;false if not

Authorization

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

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

isTrashed()

Determines whether theFile is in the trash of the user's Drive.

Return

Booleantrue if theFile is in the trash of the user's Drive;false if not

Authorization

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

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

makeCopy()

Creates a copy of the file.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Creates a copy of each file and logs the file name to the console.console.log(file.makeCopy().getName());}

Return

File — The new copy.

Authorization

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

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

makeCopy(destination)

Creates a copy of the file in the destination directory.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Creates a copy of each file and adds it to the specified folder.// TODO(developer): Replace the folder ID with your own.constdestination=DriveApp.getFolderById('123456abcxyz');constcopiedFile=file.makeCopy(destination);// Logs the file names to the console.console.log(copiedFile.getName());}

Parameters

NameTypeDescription
destinationFolderThe directory to copy the file into.

Return

File — The new copy.

Authorization

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

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

makeCopy(name)

Creates a copy of the file and names it with the name provided.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Creates a copy of each file and sets the name to 'Test-Copy.'constfilename=file.makeCopy('Test-Copy');// Logs the copied file's name to the console.console.log(filename.getName());}

Parameters

NameTypeDescription
nameStringThe filename that should be applied to the new copy.

Return

File — The new copy.

Authorization

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

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

makeCopy(name, destination)

Creates a copy of the file in the destination directory and names it with the name provided.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Creates a copy of each file, sets the file name, and adds the copied file// to the specified folder.// TODO(developer): Replace the folder ID with your own.constdestination=DriveApp.getFolderById('123456abcxyz');constcopiedFile=file.makeCopy('Test-Copy',destination);// Logs the file names to the console.console.log(copiedFile.getName());}

Parameters

NameTypeDescription
nameStringThe filename that should be applied to the new copy.
destinationFolderThe directory to copy the file into.

Return

File — The new copy.

Authorization

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

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

moveTo(destination)

Moves this item to the provided destination folder.

The current user must be the owner of the file or have at least edit access to the item'scurrent parent folder in order to move the item to the destination folder.

Parameters

NameTypeDescription
destinationFolderThe folder that becomes the new parent.

Return

File — ThisFile, for chaining.

Authorization

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

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

removeCommenter(emailAddress)

Removes the given user from the list of commenters for theFile. This methoddoes not block users from access theFile if they belong to a class of userswho have general access — for example, if theFile is shared with the user'sentire domain.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Removes the given user from the list of commenters for each file.// TODO(developer): Replace the email with the email of the user you want to// remove.file.removeCommenter('cloudysanfrancisco@gmail.com');}

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

File — ThisFile, for chaining.

Authorization

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

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

removeCommenter(user)

Removes the given user from the list of commenters for theFile. This methoddoes not block users from access theFile if they belong to a class of userswho have general access — for example, if theFile is shared with the user'sentire domain.

// Gets a list of all files in Google Drive with the given name.// TODO(developer): Replace the file name with your own.constfiles=DriveApp.getFilesByName('Test');// Loops through the files.while(files.hasNext()){constfile=files.next();// Removes the given user from the list of commenters for each file.console.log(file.removeCommenter(Session.getActiveUser()));}

Parameters

NameTypeDescription
userUserA representation of the user to remove.

Return

File — ThisFile, for chaining.

Authorization

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

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

removeEditor(emailAddress)

Removes the given user from the list of editors for theFile. This method doesn'tblock users from accessing theFile if they belong to a class of users who havegeneral access—for example, if theFile is shared with the user's entiredomain, or if theFile 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

File — ThisFile, for chaining.

Authorization

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

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

removeEditor(user)

Removes the given user from the list of editors for theFile. This method doesn'tblock users from accessing theFile if they belong to a class of users who havegeneral access—for example, if theFile is shared with the user's entiredomain, or if theFile 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

File — ThisFile, for chaining.

Authorization

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

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

removeViewer(emailAddress)

Removes the given user from the list of viewers and commenters for theFile. Thismethod has no effect if the user is an editor, not a viewer or commenter. This method alsodoesn't block users from accessing theFile if they belong to a class of users whohave general access—for example, if theFile is shared with the user'sentire domain, or if theFile 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

File — ThisFile for chaining.

Authorization

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

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

removeViewer(user)

Removes the given user from the list of viewers and commenters for theFile. Thismethod has no effect if the user is an editor, not a viewer. This method also doesn't blockusers from accessing theFile if they belong to a class of users who have generalaccess—for example, if theFile is shared with the user's entire domain, orif theFile 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

File — ThisFile for chaining.

Authorization

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

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

revokePermissions(emailAddress)

Revokes the access to theFile granted to the given user. This method doesn'tblock users from accessing theFile if they belong to a class of users whohave general access — for example, if theFile is shared with the user'sentire domain.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user whose access should be revoked.

Return

File — ThisFile, for chaining.

Authorization

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

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

revokePermissions(user)

Revokes the access to theFile granted to the given user. This method doesn'tblock users from accessing theFile if they belong to a class of users whohave general access — for example, if theFile is shared with the user'sentire domain.

Parameters

NameTypeDescription
userUserA representation of the user whose access should be revoked.

Return

File — ThisFile, for chaining.

Authorization

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

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

setContent(content)


setDescription(description)

Sets the description for theFile.

Parameters

NameTypeDescription
descriptionStringthe new description for theFile

Return

File — thisFile, for chaining

Authorization

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

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

setName(name)

Sets the name of theFile.

Parameters

NameTypeDescription
nameStringthe new name of theFile

Return

File — thisFile, for chaining

Authorization

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

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

setOwner(emailAddress)

Changes the owner of theFile. This method also gives the previous ownerexplicit edit access to theFile.

Parameters

NameTypeDescription
emailAddressStringthe email address of the user who should become the new owner

Return

File — thisFile, for chaining

Authorization

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

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

setOwner(user)

Changes the owner of theFile. This method also gives the previous ownerexplicit edit access to theFile.

Parameters

NameTypeDescription
userUsera representation of the user who should become the new owner

Return

File — thisFile, for chaining

Authorization

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

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

setSecurityUpdateEnabled(enabled)

Sets whether theFile requires a resource key for access when it'sshared using a link. Eligible files and folders are enabled by default.

Learn more about theSecurity update for Google Drive.

Parameters

NameTypeDescription
enabledBooleanWhether to enable the resource key requirement for theFile.

Return

File — ThisFile, for chaining.

Authorization

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

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

setShareableByEditors(shareable)

Sets whether users with edit permissions to theFile are allowed to sharewith other users or change the permissions. The default for a newFile istrue.

Parameters

NameTypeDescription
shareableBooleantrue if users with edit permissions should be allowed to share with other users or change the permissions;false if not

Return

File — thisFile, for chaining

Authorization

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

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

setSharing(accessType, permissionType)

Sets which class of users can access theFile and what permissionsthose users are granted, besides any individual users who have been explicitly given access.

// Creates a folder that anyone on the Internet can read from and write to.// (Domain administrators can prohibit this setting for users of a Google// Workspace domain.)constfolder=DriveApp.createFolder('Shared Folder');folder.setSharing(DriveApp.Access.ANYONE,DriveApp.Permission.EDIT);

Parameters

NameTypeDescription
accessTypeAccesswhich class of users should be able to access theFile
permissionTypePermissionthe permissions that should be granted to users who can access theFile

Return

File — thisFile, for chaining

Authorization

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

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

setStarred(starred)

Sets whether theFile is starred in the user's Drive. The default fornewFiles isfalse.

Parameters

NameTypeDescription
starredBooleantrue if theFile should be starred in the user's Drive;false if not

Return

File — thisFile, for chaining

Authorization

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

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

setTrashed(trashed)

Sets whether theFile is in the trash of the user's Drive. Only theowner may trash theFile. The default for newFiles isfalse.

Parameters

NameTypeDescription
trashedBooleantrue if theFile should be moved to the trash of the user's Drive;false if not

Return

File — thisFile, for chaining

Authorization

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

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

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

Last updated 2025-12-11 UTC.