【问题标题】:Copy and Share Google Document to new email but suppress notification将 Google 文档复制并共享到新电子邮件,但禁止通知
【发布时间】:2021-10-25 06:38:27
【问题描述】:

我有以下代码来复制模板并与有权访问该模板的同一个人共享。

但是,我有两个问题:

  1. 我不希望他们在每次制作副本并与他们共享时都收到通知
  2. 我还想与其他人分享以进行编辑。可以编辑的人的电子邮件在 Google 工作表的 A 列中。这个新人也不应该收到通知。
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('Create My Checklist');
  menu.addItem('New Checklist', 'createNewGoogleDocs')
  menu.addToUi();
}
function createNewGoogleDocs() {
  //This value should be the id of your document template that we created in the last step
  const googleDocTemplate = DriveApp.getFileById('1qRQ07PDmz1il9IftM9GIJfY37vTusfQZhhNS1BRELJQ');
  
  //This value should be the id of the folder where you want your completed documents stored
  const destinationFolder = DriveApp.getFolderById('1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1')
  //Here we store the sheet as a variable
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Data')
  
  //Now we get all of the values as a 2D array
  const rows = sheet.getDataRange().getValues();
  
  const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); // Added
  //Start processing each spreadsheet row
  rows.forEach(function(row, index){
    //Here we check if this row is the headers, if so we skip it
    if (index === 0) return;
    //Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
    if (row[10]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy("eResignation Checklist - " + row[2] + " - " + row[4] + " - " +  row[5], destinationFolder)
    copy.addEditors(emailAddresses);
    //Once we have the copy, we then open it using the DocumentApp
    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Workday ID}}', row[1]);
    body.replaceText('{{Full Name}}', row[2]);
    body.replaceText('{{Management Level}}', row[3]);
    body.replaceText('{{LoS}}', row[4]);
    body.replaceText('{{Cost Centre}}', row[5]);
    body.replaceText('{{Office Location}}', row[6]);
    
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //Store the url of our new document in a variable
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
   sheet.getRange(index + 1, 11).setValue(url)
  
  Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true});
  
})}

【问题讨论】:

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


    【解决方案1】:

    我相信你的目标如下。

    • 您想要共享复制的文件。
    • 可以从“A”列中检索到用于共享的电子邮件地址。
    • 共享文件时,您不想发送通知电子邮件。

    这样的话,下面的修改怎么样?

    修改脚本:

    为了使用sendNotificationEmails 的选项,使用了Drive API。所以please enable Drive API at Advanced Google services

    发件人:

      sheet.getRange(index + 1, 11).setValue(url)
      
    })}
    

    收件人:

      sheet.getRange(index + 1, 11).setValue(url)
      
      Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false}); // Added
      
    })}
    

    参考:

    编辑:

    来自以下回复,

    我真的不确定为什么它说找不到文件,因为复制的文件在共享驱动器中> 文件夹 ID:1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1

    我注意到您正在使用共享云端硬盘。从以下回复中,

    我已经按照我现在在脚本中看到的内容再次编辑了代码。它似乎部分工作 - 对于第 [0] 行中的电子邮件,他们没有收到任何通知 const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail());如何禁用此群组的通知?

    我注意到您的脚本与最初的脚本有所不同。在这种情况下,请进行如下修改。

    发件人:

      sheet.getRange(index + 1, 11).setValue(url)
      
    })}
    

    收件人:

      sheet.getRange(index + 1, 11).setValue(url)
      
      emailAddresses.concat(row[0]).forEach(v => 
        Drive.Permissions.insert({role: "writer", type: "user", value: v}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true})
      );
      
    })}
    

    【讨论】:

    • 嗨@Tanaike,有两个组需要对文件的编辑权限:1)已经在模板中的人(与相同的人共享)2)第[0]行中的电子邮件地址- 如果 abc@gmail.com 在单元格 A2 中,则该副本对于 abc@gmail.com 和组 #1 应该是可编辑的。但是,我不希望他们中的任何人在创建副本时收到通知,因为我们稍后只会发送包含文档链接的电子邮件。我尝试根据您的建议调整代码,但是,我收到一条错误消息,指出驱动器未定义。
    • @Shamie 感谢您的回复。我不得不为我糟糕的英语水平道歉。很遗憾,我无法理解您的回复。根据您对The email of the person who can edit is in the google sheet, in column A. 的问题,我了解到您想使用“A”列中的电子邮件进行共享。我的理解正确吗?
    • @Shamie 还有,关于I tried adjusting the code as per your suggestion, however, i received an error message that Drive is not defined.,我不得不再次为我糟糕的英语水平道歉。关于我提出的脚本,我在回答中写了In order to use the option of sendNotificationEmails, Drive API is used. So please enable Drive API at Advanced Google services.。根据您的回复,我认为您可能还没有启用 Drive API。我担心你可能听不懂我的英语。如果我误解了,我再次道歉。
    • @Shamie 而且,我无法理解2) The email address in row [0] - if abc@gmail.com is in cell A2, then the copy should be editable for abc@gmail.com and group #1.。可以问一下具体情况吗?
    • 亲爱的@Tanaike,你的英语很好,请不要担心。我相信我缺乏知识让我感到困惑。我已启用 Google Sheet 和 Google doc API,但我仍然收到此错误 ReferenceError: Drive is not definedDetails.... 此行:Const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail()); 这是为了获取模板中的人员的电子邮件,然后与他们分享。当我分享时,我不希望他们收到通知。然后,我想和第[0]行的邮箱分享,我也不希望这个人收到通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多