You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script复制模板文档免通知共享及添加编辑者问题

谷歌文档模板复制共享脚本修复方案

需求确认

  • 生成模板副本后共享给模板原有编辑者时,不触发共享通知邮件
  • 额外将生成的文档共享给当前谷歌表格A列记录的人员为编辑者,该类新增编辑者同样不接收通知邮件

前置准备

请先开启Google Apps Script的高级Drive服务:

  1. 打开脚本编辑界面,点击左侧边栏「服务」旁的「+」按钮
  2. 在服务列表中找到「Drive API」,选中后点击「添加」即可生效

修改逻辑说明

  • 移除原代码中copy.addEditors(emailAddresses)方法:DriveApp内置的添加编辑者方法默认发送通知,且没有关闭开关
  • 替换为Drive API的权限插入方法,统一配置sendNotificationEmails: false参数关闭通知
  • 原代码中添加A列人员为编辑的逻辑已经配置了关闭通知参数,无需额外调整

完整可运行代码

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('Create My Checklist');
  menu.addItem('New Checklist', 'createNewGoogleDocs')
  menu.addToUi();
}
function createNewGoogleDocs() {
  // 此处填写你的文档模板ID
  const googleDocTemplate = DriveApp.getFileById('1qRQ07PDmz1il9IftM9GIJfY37vTusfQZhhNS1BRELJQ');
  
  // 此处填写生成文档的存储文件夹ID
  const destinationFolder = DriveApp.getFolderById('1JufckhwXlAXDAE3_-f60lHQQ-jqe9Mx1')
  // 读取存储数据的工作表
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Data')
  
  // 读取工作表所有数据为二维数组
  const rows = sheet.getDataRange().getValues();
  
  // 获取模板原有编辑者的邮箱列表
  const emailAddresses = googleDocTemplate.getEditors().map(e => e.getEmail());
  // 逐行处理工作表数据
  rows.forEach(function(row, index){
    // 跳过表头行
    if (index === 0) return;
    // 如果第11列(文档链接列)已有内容则跳过,避免重复生成
    if (row[10]) return;
    // 复制模板到目标文件夹,按规则命名
    const copy = googleDocTemplate.makeCopy("eResignation Checklist - " + row[2] + " - " + row[4] + " - " +  row[5], destinationFolder)
    // 给模板原有编辑者添加编辑权限,不发送通知邮件
    emailAddresses.forEach(email => {
      Drive.Permissions.insert(
        {role: "writer", type: "user", value: email},
        copy.getId(),
        {sendNotificationEmails: false, supportsAllDrives: true}
      )
    })
    // 打开新生成的文档用于内容替换
    const doc = DocumentApp.openById(copy.getId())
    const body = doc.getBody();
    
    // 替换文档中的占位符为表格对应行数据
    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]);
    
    // 保存并关闭文档
    doc.saveAndClose();
    const url = doc.getUrl();
    // 将生成的文档链接回写到表格第11列
    sheet.getRange(index + 1, 11).setValue(url)
  
    // 给当前行A列的人员添加编辑权限,不发送通知邮件
    Drive.Permissions.insert({role: "writer", type: "user", value: row[0]}, copy.getId(), {sendNotificationEmails: false, supportsAllDrives: true});
  
  })
}

内容的提问来源于stack exchange,提问作者Shamie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 13:06:03