google-apps-scriptgoogle-drive-apifile-permissions

Change a document's permissions via Google Apps Script


I'm looking for sample script that resets the default permissions on an external spreadsheet based on the email address and DocumentID passed to the script. I intent to create a script that can parse information from an email message to acquire the DocumentID and email, execute the permission change from default to anyone with a link, then email the passed address with that link.

It appears that perms are controlled by the DocList API and I'm not finding samples of GAS interacting with that API.


Solution

  • AFAIK DocsList Services does not have a function to change the sharing mode, between private/anyone with a link/public, only to add/remove editors and viewers. But we can still achieve this by previously setting manually the share settings of a specific folder to "anyone with a link". Then, we have just to add the file to that folder to have it shared.

    A script to do that is particularly simple. e.g.

    function shareWithAnyoneAndEmail(documentID,email) {
      var sharedFolder = DocsList.getFolderById('id-to-your-previously-shared-folder');
      var file = DocsList.getFileById(documentID);
      file.addToFolder(sharedFolder);
      MailApp.sendEmail(email, 'Here is your file', file.getName()+'\n'+file.getUrl());
    }