addEditors() through Advanced Drive Service withou

2019-06-28 07:55发布

问题:

My code processes and merges user submitted Google Form data into a Google Doc, shares the file with users determined through Google Sheet Functions, and emails a notification of the new submission to those same users.

The group of users starts as a single value (list in the cell -- user1@email.com, user2@email.com, etc.) and is then split(", ") into an array for use in addEditors().

I need to set editing permissions for those users without sending the email notification associated with that action while using the DriveApp File.addEditors() method. As I understand it, this requires the Advanced Drive Service, which I have already enabled for the script.

What is the syntax and exact code for adding these permissions without notification?

Sample of my code below --

function mergeApplication() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Merge Data");
  var range = sheet.getActiveRange();
  var formSheet = ss.getSheetByName("Form Responses");
  var lastRow = formSheet.getLastRow();
  var lastColumn = sheet.getMaxColumns();
  function checkAndComplete() {
    var urlColumn = lastColumn;
    var checkColumn = (urlColumn - 1);
    var checkRange = sheet.getRange(2, checkColumn, (lastRow - 1), 1);
    var check = checkRange.getBackgrounds();
    var red = "#ff0404";
    var yellow = "#ffec0a";
    var green = "#3bec3b";
    for (var i = 0; i < check.length; i++) {
      if (check[i] == green) {
        continue;
      } else {
        var statusCell = sheet.getRange((i+2), checkColumn, 1, 1);
        var urlCell = sheet.getRange((i+2), urlColumn, 1, 1);
        var dataRow = sheet.getRange((i+2), 1, 1, (lastColumn - 2));
        var emailTag = sheet.getRange((i+2), (checkColumn - 2)).getValue();
        var emailEditors = emailTag.split(", ");

...skipping down through merge code to where it will be used...

var docCopy = docToUse.makeCopy(docName, folderDestination);
var docId = docCopy.getId();
var docToSend = DriveApp.getFileById(docId);
docToSend.addEditors(emailEditors);

回答1:

Here's a utility function to silently give a user write permissions on a Google Drive file.

/**
 * Add write permission for the given user to the given document.
 *
 * Uses the Advanced Drive Service, which must be enabled.
 *
 * from: http://stackoverflow.com/a/37289790/1677912
 *
 * @param {string} docId      Document ID to add permissions to.
 * @param {string} userEmail  Email address of user to be given editor privileges.
 */
function addEditorSilent( docId, userEmail ) {
  var permissionResource = {
    role: 'writer',
    type: 'user',
    value: userEmail
  };
  var optionalArgs = {
    sendNotificationEmails: false
  };
  Drive.Permissions.insert(permissionResource, docId, optionalArgs);
}

Now, instead of:

var docCopy = docToUse.makeCopy(docName, folderDestination);
var docId = docCopy.getId();
var docToSend = DriveApp.getFileById(docId);
docToSend.addEditors(emailEditors);

Use:

var docCopy = docToUse.makeCopy(docName, folderDestination);
var docId = docCopy.getId();
for (var i=0; i<emailEditors.length; i++) {
  addEditorSilent( docId, emailEditors[i] );
}