Adding individual collaborators to a Drive file works fine for a small team, but it breaks down the moment membership changes or a new hire needs access to everything a group already has.
This script shares a specific Drive file with a Google Group address, granting either edit or view access depending on a simple flag, so permission changes track group membership instead of a hand-maintained list.
Because Drive treats a group email the same way it treats an individual's email for sharing purposes, addEditor and addViewer work without any special group-handling code.
The script also demonstrates checking a file's current access before changing it, which avoids sending a redundant sharing notification email to people who already have access.
Need this built? Hire a Google Apps Script developer →
Sheet / project setup
| Resource | Name / value | Purpose |
|---|---|---|
| File | Drive file ID | Target document or sheet |
| Group | team@example.com | Google Group email |
| Access | addEditor / addViewer | Permission level |
What It Does
The script looks up a Drive file by ID and grants access to a Google Group's email address, choosing between addEditor and addViewer based on an ACCESS_LEVEL constant.
Before granting access it checks getEditors and getViewers to see whether the group is already present at that access level, skipping the call entirely when nothing would change.
Prerequisites
You need the file's Drive ID from its sharing URL and the exact email address of the Google Group, which you can find on the group's Google Groups settings page.
Confirm the group's sharing settings allow it to be added to Drive files, since some Workspace organizations restrict which groups can receive shared content.
Walkthrough
Set FILE_ID to the target document's ID and GROUP_EMAIL to the group's address, then choose EDIT or VIEW for ACCESS_LEVEL before running shareFileWithGroup.
Run the function once and open the file's sharing dialog in Drive to confirm the group now appears in the list of people with access at the level you specified.
Test the change from the perspective of a group member by asking a teammate in that group to open the file directly and confirm their access matches what you configured.
Edge Cases
If ACCESS_LEVEL is set to a value other than EDIT or VIEW, the script defaults to view access rather than throwing an error, so double-check the constant if editors are not getting write access.
Groups that have no members yet still get added successfully, since Drive sharing is applied to the group address itself and takes effect automatically as members join later.
Testing
Run the script against a throwaway test document first, verifying in the sharing dialog that the group shows the correct access level before pointing it at a real file.
Change ACCESS_LEVEL from VIEW to EDIT and re-run, then confirm the sharing dialog updates the group's permission in place rather than adding a second duplicate entry.
Hardening
Wrap the addEditor or addViewer call in a try/catch so a mistyped group email produces a clear log message instead of an unhandled exception that stops the script.
Record every sharing change in an audit sheet with the file name, group email, access level, and timestamp so permission history is easy to review later.
Variations
Extend the script to loop over an array of file IDs so an entire folder of documents can be shared with the same group in one run.
Use setSharing with DriveApp.Access.DOMAIN_WITH_LINK instead of a specific group when you want anyone in your Workspace domain to have access rather than just group members.
shareFileWithGroup.gs
shareFileWithGroup checks a file's current editors and viewers before calling addEditor or addViewer with a Google Group's email address at the configured access level.
// Share a Drive file with a Google Group at a chosen access level
function shareFileWithGroup() {
var FILE_ID = 'REPLACE_WITH_FILE_ID';
var GROUP_EMAIL = 'team-group@example.com';
var ACCESS_LEVEL = 'EDIT';
var file = DriveApp.getFileById(FILE_ID);
var editors = file.getEditors().map(function (u) { return u.getEmail(); });
var viewers = file.getViewers().map(function (u) { return u.getEmail(); });
if (ACCESS_LEVEL === 'EDIT') {
if (editors.indexOf(GROUP_EMAIL) === -1) {
file.addEditor(GROUP_EMAIL);
Logger.log('Added ' + GROUP_EMAIL + ' as editor');
}
} else {
if (viewers.indexOf(GROUP_EMAIL) === -1) {
file.addViewer(GROUP_EMAIL);
Logger.log('Added ' + GROUP_EMAIL + ' as viewer');
}
}
}- Line 8: Mapping getEditors to a plain array of email addresses makes it easy to check membership with indexOf a few lines later.
- Line 12: Checking whether the group is already an editor first avoids sending a redundant sharing notification for access it already has.
- Line 13: addEditor works with a group's email exactly the way it works with an individual's, since Drive treats both the same way for sharing.
- Line 16: Any ACCESS_LEVEL value other than the exact string EDIT falls through to the viewer branch, which is worth remembering if you mistype the constant.
- Line 18: addViewer grants read-only access, appropriate for groups that only need to review rather than edit the file.
Deploy this example
- 01
Open Apps Script
In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.
- 02
Paste and save
Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.
- 03
Authorize once
Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.
- 04
Add the trigger
Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.
Before you run: share file with group
- 1Target file's Drive ID copied from its sharing URL
- 2Google Group email address confirmed to be valid
- 3Desired access level decided: edit or view
- 4Group membership confirmed to include the right people
- 5Function run once and sharing dialog checked for the group
- 6Access level change tested by switching EDIT to VIEW
Frequently asked questions
Drive's default sharing notification behavior applies, though the script does not explicitly force a notification email, similar to normal manual sharing.
The script checks both getEditors and getViewers first, so switching ACCESS_LEVEL correctly adds the new level without you needing to manually remove the old one first.
Yes, loop over an array of group emails and call the same addEditor or addViewer logic for each one.
Yes, DriveApp.getFolderById supports the same addEditor and addViewer methods, so the identical pattern applies to folders.
Drive throws an error when the address does not resolve to a real group, which the try/catch in the hardening section catches and logs clearly.
Yes, call removeEditor or removeViewer with the same group email when access needs to be revoked.