Change Folder Permissions in Google Drive with Apps Script

C
Published in: Google Apps Script - Google Drive

This Google Apps Script will change the access permissions of the specified Google Drive folder from Public to Private at a custom date and time. When you initialize the script, it creates a time-based trigger that is responsible for change the shared permissions.

Google Scripts has a simple file.removeViewer(user) method to remove one or more users from a shared File but it doesn’t seem to work when the file /folder is shared with Public. Thus the workaround, as used in this script, is to create a copy of the shared folder and delete the original one thus expiring the shared links.


// Enter the full URL of the public Google Docs folder
var FOLDER_URL = "https://docs.google.com/folder/d/1234567890/edit";

// Enter the expiry date in YYYY-MM-DD HH:MM format (local time zone)
var EXPIRY_TIME = "2013-02-15 18:30";

function getFolderID() {

    var search = /docs\.google\.com\/folder\/d\/(.*)\//g;
    var results = search.exec(FOLDER_URL);

    var id = "0";

    if (search.lastIndex)
        id = results[1];

    return id;
}

function Start() {

    var ID = getFolderID();

    if (ID == "0") {
        MailApp.sendEmail(Session.getActiveUser(), "Error",
            "Check the URL of the shared Google Docs folder : " + FOLDER_URL);
        return;
    }

    var time = EXPIRY_TIME;

    var expireAt = new Date(time.substr(0, 4),
        time.substr(5, 2) - 1,
        time.substr(8, 2),
        time.substr(11, 2),
        time.substr(14, 2));

    if (!isNaN(expireAt.getTime()))
        ScriptApp.newTrigger("autoExpire").timeBased().at(expireAt).create();
    else
        MailApp.sendEmail(Session.getActiveUser(), "Error",
            "The auto-expiry date isn't in proper format. Please use YYYY-MM-DD HH:MM");

}

function autoExpire() {

    try {

        var folder = DocsList.getFolderById(Initialize());

        if (folder) {

            var name = folder.getName();
            var copy = DocsList.createFolder(name + " (Private)");

            var files = folder.getFiles();

            for (var i = 0; i < files.length; i++) {
                files[i].removeFromFolder(folder);
                files[i].addToFolder(copy);
            }

            folder.setTrashed(true);
            copy.rename(name);

            MailApp.sendEmail(Session.getActiveUser(), "Success",
                "Your shared files are no longer public and the new (private) URL is :" + copy.getUrl());
        }
    } catch (e) {
        MailApp.sendEmail(Session.getActiveUser(), "Error",
            "Could not set the expiry date for your file. " + e.toString());
    }
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Drive

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube