addEditors()通过高级驱动器服务,无需通知电子邮件

时间:2016-05-17 22:15:37

标签: google-apps-script google-api google-drive-api

我的代码处理并将用户提交的Google表单数据合并到Google文档中,与通过Google表格函数确定的用户共享该文件,并通过电子邮件将新提交的通知发送给这些用户。

用户组作为单个值(单元格中的列表 - user1@email.com, user2@email.com, etc.)启动,然后split(", ")成为数组,以便在addEditors()中使用。

我需要为这些用户设置编辑权限,而不使用DriveApp File.addEditors()方法发送与该操作相关联的电子邮件通知。据我了解,这需要Advanced Drive Service,我已经enabled用于脚本。

在没有通知的情况下添加这些权限的语法和确切代码是什么?

我的代码示例如下 -

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(", ");

...通过合并代码跳过它将被使用的地方......

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

1 个答案:

答案 0 :(得分:2)

这是一个实用程序功能,可以静默授予用户对Google云端硬盘文件的写入权限。

/**
 * 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);
}

现在,而不是:

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

使用:

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