Skip to content

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.File

Start 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.

Code.gs
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.File
Show example code
Code.gs
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.File
Show example code
Code.gs
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.File
Show example code
Code.gs
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

From signature data Google source
  • addCommenter(emailAddress: string): DriveApp.File
  • addCommenter(user: User): DriveApp.File

addCommenters

From signature data Google source
  • addCommenters(emailAddresses: string[]): DriveApp.File

addEditor

From signature data Google source
  • addEditor(emailAddress: string): DriveApp.File
  • addEditor(user: User): DriveApp.File

addEditors

From signature data Google source
  • addEditors(emailAddresses: string[]): DriveApp.File

addViewer

From signature data Google source
  • addViewer(emailAddress: string): DriveApp.File
  • addViewer(user: User): DriveApp.File

addViewers

From signature data Google source
  • addViewers(emailAddresses: string[]): DriveApp.File

getAccess

From signature data Google source
  • getAccess(email: string): DriveApp.Permission
  • getAccess(user: User): DriveApp.Permission

getAs

From signature data Google source
  • getAs(contentType: string): Blob

getBlob

From signature data Google source
  • getBlob(): Blob

getDateCreated

From signature data Google source
  • getDateCreated(): Date

getDescription

From signature data Google source
  • getDescription(): string | null

getDownloadUrl

From signature data Google source
  • getDownloadUrl(): string

getEditors

From signature data Google source
  • getEditors(): User[]

getId

From signature data Google source
  • getId(): string

getLastUpdated

From signature data Google source
  • getLastUpdated(): Date

getMimeType

From signature data Google source
  • getMimeType(): string

getName

From signature data Google source
  • getName(): string

getOwner

From signature data Google source
  • getOwner(): User

getParents

From signature data Google source
  • getParents(): DriveApp.FolderIterator

getResourceKey

From signature data Google source
  • getResourceKey(): string

getSecurityUpdateEligible

From signature data Google source
  • getSecurityUpdateEligible(): boolean

getSecurityUpdateEnabled

From signature data Google source
  • getSecurityUpdateEnabled(): boolean

getSharingAccess

From signature data Google source
  • getSharingAccess(): DriveApp.Access

getSharingPermission

From signature data Google source
  • getSharingPermission(): DriveApp.Permission

getSize

From signature data Google source
  • getSize(): number

getTargetId

From signature data Google source
  • getTargetId(): string | null

getTargetMimeType

From signature data Google source
  • getTargetMimeType(): string | null

getTargetResourceKey

From signature data Google source
  • getTargetResourceKey(): string | null

getThumbnail

From signature data Google source
  • getThumbnail(): Blob | null

getUrl

From signature data Google source
  • getUrl(): string

getViewers

From signature data Google source
  • getViewers(): User[]

isShareableByEditors

From signature data Google source
  • isShareableByEditors(): boolean

isStarred

From signature data Google source
  • isStarred(): boolean

isTrashed

From signature data Google source
  • isTrashed(): boolean

makeCopy

From signature data Google source
  • makeCopy(): DriveApp.File
  • makeCopy(name: string): DriveApp.File
  • makeCopy(destination: DriveApp.Folder): DriveApp.File
  • makeCopy(name: string, destination: DriveApp.Folder): DriveApp.File

moveTo

From signature data Google source
  • moveTo(destination: DriveApp.Folder): DriveApp.File

removeCommenter

From signature data Google source
  • removeCommenter(emailAddress: string): DriveApp.File
  • removeCommenter(user: User): DriveApp.File

removeEditor

From signature data Google source
  • removeEditor(emailAddress: string): DriveApp.File
  • removeEditor(user: User): DriveApp.File

removeViewer

From signature data Google source
  • removeViewer(emailAddress: string): DriveApp.File
  • removeViewer(user: User): DriveApp.File

revokePermissions

From signature data Google source
  • revokePermissions(emailAddress: string): DriveApp.File
  • revokePermissions(user: User): DriveApp.File

setContent

From signature data Google source
  • setContent(content: string): DriveApp.File

setDescription

From signature data Google source
  • setDescription(description: string): DriveApp.File

setName

From signature data Google source
  • setName(name: string): DriveApp.File

setOwner

From signature data Google source
  • setOwner(emailAddress: string): DriveApp.File
  • setOwner(user: User): DriveApp.File

setSecurityUpdateEnabled

From signature data Google source
  • setSecurityUpdateEnabled(enabled: boolean): DriveApp.File

setShareableByEditors

From signature data Google source
  • setShareableByEditors(shareable: boolean): DriveApp.File

setSharing

From signature data Google source
  • setSharing(accessType: DriveApp.Access, permissionType: DriveApp.Permission): DriveApp.File

setStarred

From signature data Google source
  • setStarred(starred: boolean): DriveApp.File

setTrashed

From signature data Google source
  • 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