1
votes

I wrote a function to share files automatically with users using google apps script. However, I want to add a custom message while sharing (like we do with manual sharing). I want this so that I don't have to send emails to the users separately. I was initially sending email along with file url but the email was blocked (since message contained links). Is there a way to add custom message?

function fileshare(url, email) {

  var f = SpreadsheetApp.openByUrl(url);
  var id = f.getId();
  try{
    //f.addViewer(email);
    DriveApp.getFileById(id).addViewer(email);

  }
  catch (e) {
    Logger.log("File could not be shared, please check if email address is valid");
  }
}
1

1 Answers

2
votes

I believe your goal as follows.

  • You want to put the custom message in the invitation email when you share a file with an user.
  • You want to achieve this using Google Apps Script.

For this, how about this answer?

Modification points:

  • In order to use the custom message to the invitation email of shared file, in this case, Drive API is used. When the method of "Permissions: insert" in Drive API is used, the custom message can be added.
    • In the current stage, the version of Drive API at Advanced Google services is v2.

When your script is modified, it becomes as follows.

Modified script:

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

DriveApp.getFileById(id).addViewer(email);
var customMessage = "sample message";  // Please set the custom message here.
var resource = {role: "reader", type: "user", value: email};
Drive.Permissions.insert(resource, id, {emailMessage: customMessage});
  • In this modification, the user is set as a reader. This is the same with addViewer.

References: