justSteve
justSteve

Reputation: 5524

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.

Upvotes: 2

Views: 10494

Answers (2)

Arun Nagarajan
Arun Nagarajan

Reputation: 5665

At Google I/O 2013, DriveApp was launched. This allows developers to build use cases like Sharing to Anyone with link

https://developers.google.com/apps-script/reference/drive/

Sample code -

 var quizTemplate = DriveApp.getFileById(QUIZ_TEMPLATE_ID);  
  quizTemplate.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.VIEW);

or

  var openFile = DriveApp.getFileById(WIDE_OPEN_ID)
  openFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);

Upvotes: 9

Henrique G. Abreu
Henrique G. Abreu

Reputation: 17792

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());
}

Upvotes: 3

Related Questions