google-apps-scriptgoogle-drive-apiemail-notifications

Surpress automatic email notifications


I am using this code for sharing the access to PDF-files and it seems to do what I need it to do:

function setPDFAuth(pdfName) {
  var files = DriveApp.getFilesByName(pdfName);
  var folders = DriveApp.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    if( folder.getName() == 'Reports'){
      var editors = folder.getEditors();
    }
  };

  for(var i = 0; i<editors.length;i++){  
    var editor = editors[i].getEmail();
    while (files.hasNext()) {
      var pdfFile = files.next().addEditor(editor);
    };
  };
//    Logger.log(folder.getName());
};

Ther is one unexpected side-effect: the automatic email notification for each PDF shared.

How can I prevent the sending of an email notification for each PDF shared???


Solution

  • In your situation, in order to create the permission, how about using Drive API instead of Drive service (DriveApp)? When your showing script is modified, it becomes as follows.

    Before you run this script, please enable Drive API v3 at Advanced Google services.

    From:

    var pdfFile = files.next().addEditor(editor);
    

    To:

    Drive.Permissions.create({ role: "writer", type: "user", emailAddress: editor }, files.next().getId(), { sendNotificationEmail: false });
    

    Note:

    Reference: