// Drive · Apps Script

Share a Drive file with a specific user in Google Drive.

How to use Google Apps Script to share a Drive file with one person by email using addEditor or addViewer, without accidentally making the file public to anyone with the link.

I want to programmatically share a Google Drive file with a specific person's email address from Apps Script, and I'm not sure whether to use addEditor, setSharing, or something else entirely.

The script

copy · paste · trigger
shareFile.gs
Apps Script
// Share a Drive file with a specific user via email
// Requires Drive scope: https://www.googleapis.com/auth/drive

function shareFileWithUser(fileId, recipientEmail) {
  var file = DriveApp.getFileById(fileId);

  // Grant edit access and send a notification email
  file.addEditor(recipientEmail);

  // Grant view-only access instead (comment out addEditor above)
  // file.addViewer(recipientEmail);

  Logger.log('Shared ' + file.getName() + ' with ' + recipientEmail);
}

function example() {
  var FILE_ID = '1A2B3C4D5E6F7G8H9I0J';
  var EMAIL   = 'colleague@example.com';
  shareFileWithUser(FILE_ID, EMAIL);
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

addEditor vs. addViewer: pick the right method first

DriveApp exposes two person-targeted sharing methods: addEditor(email) and addViewer(email). addEditor puts the recipient in the Editors list, which means they can modify the file and re-share it if your domain settings allow. addViewer puts them in the Viewers list — read and download, nothing more. The call is identical either way; only the permission level differs.

Both methods send a notification email to the recipient by default. That email comes from Google's sharing infrastructure, not from your Apps Script account, so it lands in their inbox looking like a standard Drive share. If you want to suppress the email, use the array form: file.addEditors([email]) does not send a notification, which is worth knowing when you are bulk-sharing hundreds of files in a loop and do not want to flood inboxes.

The first time I hit a situation where users complained they never got an access notification, the culprit was that I had switched to the array form for batch performance and forgotten it silences the email. Keep that trade-off in mind before you optimize.

What setSharing actually does (and why it is the wrong tool here)

setSharing(accessType, permissionType) controls the file's general link-sharing policy, not access for a named individual. Calling setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW) makes the file readable by anyone who has the URL — no login required. That is a fundamentally different action from giving one person access.

The two are easy to conflate because the Drive UI presents them on the same sharing dialog. In script, they operate on separate axes. You can have a file that is restricted to specific people (the default) and still add editors by email, or you can make a file link-public and also add named editors on top of that. setSharing sets the floor; addEditor and addViewer add named individuals above it.

Over-sharing happens when a developer reaches for setSharing because the name sounds generic enough to cover any sharing task. Under-sharing happens when someone calls setSharing(PRIVATE, NONE) thinking that removes a specific person — it does not; you need removeEditor(email) or removeViewer(email) for that.

Getting the file ID reliably

The fileId parameter in the snippet is the long alphanumeric string in the file's URL, between /d/ and /edit. You can also get it programmatically: DriveApp.getFilesByName('Report.pdf').next().getId() works, but it matches by name only and breaks silently if the name is not unique. For production use, store the file ID as a Script Property (PropertiesService.getScriptProperties().getProperty('REPORT_FILE_ID')) so it survives file renames.

One scope note: DriveApp methods require the https://www.googleapis.com/auth/drive OAuth scope. If your appsscript.json only declares drive.file (the narrower scope that covers files the script created), you will get a permissions error when trying to share a file the script did not create. The fix is to add the full drive scope to your manifest, which triggers a broader consent prompt for users.

I keep the file ID in Script Properties rather than hardcoded because the file occasionally gets replaced with a new version, and updating one property beats hunting through code.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Does addEditor notify the person by email?
Yes. The single-address form file.addEditor(email) sends a standard Drive sharing notification. The array form file.addEditors([email]) does not send a notification, which matters when sharing at scale.
How do I share with view-only access instead of edit access?
Replace addEditor(email) with addViewer(email). Everything else in the snippet stays the same. The recipient can read and download but cannot modify the file or manage its sharing settings.
How do I remove a person's access later?
Call file.removeEditor(email) or file.removeViewer(email) depending on the permission level you originally granted. setSharing(PRIVATE, NONE) does not remove named individuals; it only changes the link-sharing policy.
Can I share a file owned by someone else in my domain?
Only if the file has been shared with you at the Editor level and your domain policy allows editors to manage sharing. If the file was shared with you as a viewer, DriveApp will throw an exception. You need at least edit permission on the file to call addEditor or addViewer on it.