Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 911
justSteve Avatar asked Mar 17 '26 07:03

justSteve


2 Answers

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);
like image 56
Arun Nagarajan Avatar answered Mar 20 '26 20:03

Arun Nagarajan


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());
}
like image 40
Henrique G. Abreu Avatar answered Mar 20 '26 18:03

Henrique G. Abreu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!