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!!
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.
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
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.