Object reference
DriveApp.getFileById and Drive files
DriveApp.getFileById(id) returns a DriveApp.File for a Google Drive file, such as a Doc, Sheet, or PDF. Pass the file ID as a string. The call throws an exception if the file does not exist or the executing account cannot access it.
DriveApp.FileStart here
Get this object first
In a URL such as https://drive.google.com/file/d/REPLACE_WITH_FILE_ID/view or https://docs.google.com/document/d/REPLACE_WITH_FILE_ID/edit, copy the part between /d/ and the next slash. Replace the placeholder below with that ID, keeping the quotes. Pass the ID rather than the full URL or file name. Run showFileById and open the execution log to see the file name and URL.
function showFileById() {
const fileId = 'REPLACE_WITH_FILE_ID';
const file = DriveApp.getFileById(fileId);
Logger.log(file.getName());
Logger.log(file.getUrl());
} Then use it
Common actions
Choose an action, then open its example only when you need the code.
Log the name, URL, size, and type
Logs four details about the file. If getFileById throws an exception, check that you copied the file ID and can open the file URL using the account running the script. An installable trigger runs as its creator. A file shared by link may also require a resource key; use DriveApp.getFileByIdAndResourceKey(id, resourceKey) when its URL includes resourcekey.
DriveApp.FileShow example code
function logFileDetails() {
const file = DriveApp.getFileById('REPLACE_WITH_FILE_ID');
Logger.log('Name: ' + file.getName());
Logger.log('URL: ' + file.getUrl());
Logger.log('Size in bytes: ' + file.getSize());
Logger.log('MIME type: ' + file.getMimeType());
} Make a copy of the file (this creates a new file)
Creates a copy of the file in Drive with "Copy of" added to its name. The original file is not changed.
DriveApp.FileShow example code
function copyFile() {
const file = DriveApp.getFileById('REPLACE_WITH_FILE_ID');
const copy = file.makeCopy('Copy of ' + file.getName());
Logger.log('Created: ' + copy.getUrl());
} Get a Doc as a PDF blob
Converts a Google Doc file to PDF data in memory and logs its size. It does not save a new file to Drive.
DriveApp.FileShow example code
function logDocAsPdfSize() {
const file = DriveApp.getFileById('REPLACE_WITH_FILE_ID');
const pdfBlob = file.getAs('application/pdf');
Logger.log('PDF size in bytes: ' + pdfBlob.getBytes().length);
} Technical reference
Exact names and signatures
Open method details when you need them.
At a glance
- Service
- DriveApp
- Reference symbol
DriveApp.File- Object
File- Runtime
- From signature data
Methods and signatures
Browse 49 methods
addCommenter
- addCommenter(emailAddress: string): DriveApp.File
- addCommenter(user: User): DriveApp.File
addCommenters
- addCommenters(emailAddresses: string[]): DriveApp.File
addEditor
- addEditor(emailAddress: string): DriveApp.File
- addEditor(user: User): DriveApp.File
addEditors
- addEditors(emailAddresses: string[]): DriveApp.File
addViewer
- addViewer(emailAddress: string): DriveApp.File
- addViewer(user: User): DriveApp.File
addViewers
- addViewers(emailAddresses: string[]): DriveApp.File
getAccess
- getAccess(email: string): DriveApp.Permission
- getAccess(user: User): DriveApp.Permission
getAs
- getAs(contentType: string): Blob
getBlob
- getBlob(): Blob
getDateCreated
- getDateCreated(): Date
getDescription
- getDescription(): string | null
getDownloadUrl
- getDownloadUrl(): string
getEditors
- getEditors(): User[]
getId
- getId(): string
getLastUpdated
- getLastUpdated(): Date
getMimeType
- getMimeType(): string
getName
- getName(): string
getOwner
- getOwner(): User
getParents
- getParents(): DriveApp.FolderIterator
getResourceKey
- getResourceKey(): string
getSecurityUpdateEligible
- getSecurityUpdateEligible(): boolean
getSecurityUpdateEnabled
- getSecurityUpdateEnabled(): boolean
getSharingAccess
- getSharingAccess(): DriveApp.Access
getSharingPermission
- getSharingPermission(): DriveApp.Permission
getSize
- getSize(): number
getTargetId
- getTargetId(): string | null
getTargetMimeType
- getTargetMimeType(): string | null
getTargetResourceKey
- getTargetResourceKey(): string | null
getThumbnail
- getThumbnail(): Blob | null
getUrl
- getUrl(): string
getViewers
- getViewers(): User[]
isShareableByEditors
- isShareableByEditors(): boolean
isStarred
- isStarred(): boolean
isTrashed
- isTrashed(): boolean
makeCopy
- makeCopy(): DriveApp.File
- makeCopy(name: string): DriveApp.File
- makeCopy(destination: DriveApp.Folder): DriveApp.File
- makeCopy(name: string, destination: DriveApp.Folder): DriveApp.File
moveTo
- moveTo(destination: DriveApp.Folder): DriveApp.File
removeCommenter
- removeCommenter(emailAddress: string): DriveApp.File
- removeCommenter(user: User): DriveApp.File
removeEditor
- removeEditor(emailAddress: string): DriveApp.File
- removeEditor(user: User): DriveApp.File
removeViewer
- removeViewer(emailAddress: string): DriveApp.File
- removeViewer(user: User): DriveApp.File
revokePermissions
- revokePermissions(emailAddress: string): DriveApp.File
- revokePermissions(user: User): DriveApp.File
setContent
- setContent(content: string): DriveApp.File
setDescription
- setDescription(description: string): DriveApp.File
setName
- setName(name: string): DriveApp.File
setOwner
- setOwner(emailAddress: string): DriveApp.File
- setOwner(user: User): DriveApp.File
setSecurityUpdateEnabled
- setSecurityUpdateEnabled(enabled: boolean): DriveApp.File
setShareableByEditors
- setShareableByEditors(shareable: boolean): DriveApp.File
setSharing
- setSharing(accessType: DriveApp.Access, permissionType: DriveApp.Permission): DriveApp.File
setStarred
- setStarred(starred: boolean): DriveApp.File
setTrashed
- setTrashed(trashed: boolean): DriveApp.File
Need the whole workflow?
Return to a task guide
Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.
Browse Google Drive tasks