Share a Drive document without notifying user with Google Apps Script
Asked Answered
G

4

19

I am creating a workflow process in Apps Script where a Doc is generated from a template and shared with various users for approval. The Script sends a customised email notifying a user that the document requires their approval but they also receive a second email at each stage in the process from the user whose Drive the document is stored in saying "User has shared a document with you". Is there any way of disabling these alerts? When you share a document manually from your Drive, there is a checkbox option that allows you to choose whether or not the user receives a notification. However, I cannot find a way to disable this notification with Apps Script.

I am using doc.addEditors(users) to share the document.

Many Thanks

Gerlach answered 17/1, 2014 at 15:43 Comment(1)
E
13

There is a simple solution if you are working with Google Docs or Google SpreadSheets. You can use DocumentApp or SpreadSheetApp to share your Docs or SpreadSheets without email notification:

DocumentApp

var doc = DocumentApp.openById('124144')
doc.addEditor('[email protected]').addViewer('[email protected]')

SpreadSheetApp

var spreadSheet = SpreadsheetApp.openById('124144')
spreadSheet.addEditor('[email protected]').addViewer('[email protected]')

However, if you are working with documents that aren't Docs or SpreadSheets, you must share then using DriveApp and email notification will be send.

Entomophilous answered 26/3, 2015 at 13:20 Comment(2)
An excellent solution. Unfortunately, it's not possible to add a commenter via DocumentApp or SpreadsheetApp... only DriveApp. I guess there's no way to add a commenter to something in Drive without also sending them an email (excepting the Drive Advanced Service, of course).Anglicism
This solution no longer works, and sharing via DocumentApp or SpreadsheetApp now sends emails.Anglicism
B
21

Another option would be to use the Drive advanced service (which you should enable in the Resources menu in the Script Editor).

The code used should be

Drive.Permissions.insert(
   {
     'role': 'writer',
     'type': 'user',
     'value': '[email protected]'
   },
   fileId,
   {
     'sendNotificationEmails': 'false'
   });
Battleship answered 14/8, 2014 at 0:0 Comment(6)
Any idea what fileId is supposed to be? I can find the file if I try DriveApp.getFileById("fileID") but if I try the above code it says File not found.Sorrel
fileId is the 44 characters that appear as part of the URL when you open the file, but I don't think the Drive.Permissions.insert function is still availableVaruna
@Varuna Drive.Permissions.insert() is still available if you enable the Advanced Drive Service. It is not part of the standard DriveApp service.Battleship
@Sorrel make sure you have a valid fileId string first. If you still have issues you can try getting the file first var file = DriveApp.getFileById(fileId) and then insert a permission replacing fileId in the code example with file.getId().Battleship
This is working very well, Thank you. I use this code to share folders which are full of PDFs on the drive. The keyword is the Drive ;)Yaya
@Yaya I'm glad it's been helpful ;)Battleship
E
13

There is a simple solution if you are working with Google Docs or Google SpreadSheets. You can use DocumentApp or SpreadSheetApp to share your Docs or SpreadSheets without email notification:

DocumentApp

var doc = DocumentApp.openById('124144')
doc.addEditor('[email protected]').addViewer('[email protected]')

SpreadSheetApp

var spreadSheet = SpreadsheetApp.openById('124144')
spreadSheet.addEditor('[email protected]').addViewer('[email protected]')

However, if you are working with documents that aren't Docs or SpreadSheets, you must share then using DriveApp and email notification will be send.

Entomophilous answered 26/3, 2015 at 13:20 Comment(2)
An excellent solution. Unfortunately, it's not possible to add a commenter via DocumentApp or SpreadsheetApp... only DriveApp. I guess there's no way to add a commenter to something in Drive without also sending them an email (excepting the Drive Advanced Service, of course).Anglicism
This solution no longer works, and sharing via DocumentApp or SpreadsheetApp now sends emails.Anglicism
M
1

This isn't possible at the moment. More information about this topic can be found here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2829

A workaround suggested in the comments of the above issue is to use DocsList instead:

DocsList, SpreadsheetApp and DocumentApp have addEditor and addViewer methods that do not result in notification emails.

Moores answered 24/2, 2014 at 10:49 Comment(0)
R
0

Just wanted to point that Rodrigo Chiong's answer is still correct in 2024 and it works as expected.

However, one MUST use Apps Scripts' Drive API Service v2.

When selecting the v3, you notice some changes and think it's going to work, however, after making the minor adjustments it just never works and keeps sending the email notifications.

So, go for the v2 and you're safe.

Rowena answered 12/3 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.