PFB
PFB

Reputation: 167

google apps script: is it possible to restrict the sharing settings of a file while still sending a sharing notification?

I have written a Google Apps Script function that is meant to share Google Drive files, but prevent the recipient from copying/downloading/printing said files.

function shareFile(Id,addr,role,opt,msg){
  var sendNotifications;
  var fileId = Drive.Files.get(Id);
  Logger.log('File "%s", restricted label was: %s', fileId.title, fileId.labels.restricted);

  
  // cambia l'etichetta per limitare l'accesso (stampa, copia, scarica)
  // changes the labels in order to restrict access
  if(opt == 0) fileId.labels.restricted = true;
  else fileId.labels.restricted = false;
  
  // aggiorna le proprietà
  // update labels
  Drive.Files.update(fileId, Id);
   
  // notifiche
  // notifications
  if(msg==="") sendNotifications='false';
  else sendNotifications='true';
   
   
 Drive.Permissions.insert(
        {
         'role': role,
         'type': 'user',
         'value': addr
        },
        Id,
        {
         'sendNotificationEmails': sendNotifications,
         'emailMessage': msg
         });       
}

In writing this function I was inspired by this and this.

My function does seem to work when no notification is sent to the recipient (the last argument is empty, msg=""). In this case the file appears in the "Shared with me" section of the recipient's Google Drive, and the only possible action for them is to create a shortcut to it. No download, copy, print, share allowed.

When msg is non empty, an email notification is sent to the recipient. The problem is that the document is attached to the email and the recipient can download it (and hence distribute it). Yet, if the document is opened from the "Shared with me" section of the recipient's Google Drive it appears to be still restricted. The recipient cannot download, print or copy it.

I find this a little weird, but maybe this is the intended behavior.

In principle I'd like to notify the recipient that a file was shared with him/her, possibly including a Google Drive link. Attaching the document to the email seems to defeat the purpose of the restricted labels. But I'm probably missing something, and chances are that what I want could be achieved in a much cleaner way.

I did some research, but ended up more confused. If I get it right, in this post Tanaike says that "labels.restricted" is deprecated. Could this be the reason why my script fails (assuming that it actually fails)? I still do not really understand Tanaike's workaround.

Thanks a lot for any insight Francesco

Upvotes: 0

Views: 613

Answers (1)

PFB
PFB

Reputation: 167

I'm answering my own question because (as suggested also by Tanaike) sending the notification via MailApp instead of Drive.Permissions.insert does the trick. Even when the email includes the URL of the shared file, the file itself stays restricted.

At variance with this behaviour, emailMessage in Drive.Permissions.insert apparently attaches a copy of the restricted file to the email notification, thus defeating the purpose of the restriction.

I'm pasting the new version of my function below (notice I had to change the first argument a little bit, in order to get the URL inside the function). I'm not sure whether this is the best way of doing that, I'm still a bit confused about file id's and file handles in DriveApp.

Also, I'm still curious about the behaviour of emailMessage in Drive.Permissions.insert.

function shareFile(hfile,addr,role,opt,msg){
  var sendNotifications;
  var Id = hfile.getId();  
  var fileId = Drive.Files.get(Id);
  
  var URL = hfile.getUrl();

  
  // changes the labels in order to restrict access
  if(opt == 0) fileId.labels.restricted = true;
  else fileId.labels.restricted = false;
  
  // update labels
  Drive.Files.update(fileId, Id);
  //Logger.log('File "%s", restricted label is (2): %s', fileId.title, fileId.labels.restricted);
   
  // notifications
  if(msg==="") sendNotifications='false';
  else sendNotifications='true';
  
  
  Drive.Permissions.insert(
        {
         'role': role,
         'type': 'user',
         'value': addr
        },
        Id,
        {
         'sendNotificationEmails': 'false'
         });
  
  if(msg!==""){
    MailApp.sendEmail({
      to: addr,
      subject: "file condiviso",
      htmlBody: 'troverai il file nella sezione "Condivisi con me" del tuo Google Drive.' + 'oppure seguendo questo <a href="' + URL +'">questo link</a>'
  });
  
  } 
  
 
 
  

         
}

Here opt=0 and role='reader'.

Upvotes: 1

Related Questions