addViewer the method used with the class DriveApp always sends the email to the recipient. Can I disable the email?

末鹿安然 提交于 2019-12-11 13:06:51

问题


Code A and B do the same things but in case A when add a viewer, the command addViewer don't send the email to currentUser. Instead, in the case B when add a viewer the currentUser receive an email of file sharing.

//CASE A
var f = DocsList.getFolderById(folder.getId());  
f.addViewer(currentUser);

//CASE B
var f = DriveApp.getFolderById(folder.getId());
f.addViewer(currentUser);

I would like to share wihouth automatic email because at the end of procedure I'll send a custom email (with the file inside folder link) if the procedure completed successfull, otherwaise I will delete the folder. How can I do? DocList will soon be deprecated!!


回答1:


To do this you'll need to use the Drive API permissions.insert method [notice it's not DriveApp] to silently insert a permission.

The Drive API is exposed to Google Apps Script through the Google Drive Advanced Service. To use it you'll need to enable it for your project.

Once that's done your permission insert code might look like this:

/**
 * Insert a new permission without sending notification email.
 *
 * @param {String} fileId ID of the file to insert permission for.
 * @param {String} value User or group e-mail address, domain name or
 *                       {@code null} "default" type.
 * @param {String} type The value "user", "group", "domain" or "default".
 * @param {String} role The value "owner", "writer" or "reader".
 */
function insertSilentPermission(fileId, value, type, role) {
  var request = Drive.Permissions.insert({
    'value': value,
    'type': type,
    'role': role,
    'withLink': false
  },
  fileId,
  {
    'sendNotificationEmails': false
  });
}

I've edited this answer to include Laura's feedback below so it now works as expected.




回答2:


the method require "p" uppercase: Drive.Permissions.insert But after this little thing, the function returns an error:

The numbers of arguments is invalid. Expected 2-3

I tried with this:

Drive.Permissions.insert(
{
  'role': 'reader',
  'type': 'user',
  'value': 'username@test.it'
},
fileId,  
{
  'sendNotificationEmails': 'false'
});

It works fine but only for FILE. For FOLDER (in fileId i put a folder ID) happens a strange thing:

Before run this code sharing Settings of folder are:

  • Share only with specific persons
  • person@example.com can view
  • admin@example.com is owner

After running the code sharing Settings of folder become:

  • anyone in example.com with the link can view --> this is WRONG
  • username@example.com can view --> this is OK
  • person@example.com can view
  • admin@example.com is owner



回答3:


If you are sharing a file with Google Scripts, the email notification will always go to the viewers and editors. The option to not send that email is only available if you share a file manually inside Google Drive.



来源:https://stackoverflow.com/questions/29645226/addviewer-the-method-used-with-the-class-driveapp-always-sends-the-email-to-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!