You need to enable JavaScript to run this app. 如何为Google表单到文档工作流添加Google Sheet审批触发步骤
优惠活动
大模型
产品
解决方案
定价
更多

如何为Google表单到文档工作流添加Google Sheet审批触发步骤

适配审批复选框触发的解决方案

1. 先调整表格结构

  • 保留F列作为「审批」复选框列
  • 新增G列,命名为已同步,用于标记该行数据是否已经写入Google Doc,避免重复添加

2. 替换原有代码为以下版本

// 审批勾选触发的主函数
function onApprove(e) {
  // 先判断触发编辑的是不是F列的复选框,且状态为勾选
  const range = e.range;
  const sheet = range.getSheet();
  const row = range.getRow();
  const column = range.getColumn();
  // F列对应索引是6,且表头在第1行,所以行号要大于1,且值为true(勾选)
  if (column !== 6 || row <= 1 || range.getValue() !== true) return;
  // 判断G列对应行是否已标记为已同步,避免重复执行
  const synced = sheet.getRange(row, 7).getValue();
  if (synced === "是") return;

  // 加锁避免并发冲突
  const lock = LockService.getScriptLock();
  lock.waitLock(30000);

  try {
    // 读取当前行的提交数据,索引对应:1=时间戳,2=姓名,3=所属机构,4=国家
    const date = new Date(sheet.getRange(row, 1).getValue()).toLocaleDateString();
    const name = sheet.getRange(row, 2).getValue();
    const affiliation = sheet.getRange(row, 3).getValue();
    const country = sheet.getRange(row, 4).getValue();

    const tableCells = [name, affiliation, country, date];

    // 打开目标Doc写入数据
    const letter = DocumentApp.openById('替换为你的Google Doc ID');
    const body = letter.getBody();
    const table = body.getTables()[0];
    const tableRow = table.appendTableRow();
    tableCells.forEach(cell => tableRow.appendTableCell(cell));

    letter.saveAndClose();
    // 写入同步标记,避免下次重复触发
    sheet.getRange(row,7).setValue("是");
  } catch (err) {
    console.error("执行出错:" + err.message);
  } finally {
    lock.releaseLock();
  }
}

3. 配置可安装触发器

因为简单的onEdit触发器没有访问Google Doc的权限,需要手动配置可安装触发器:

  • 打开脚本编辑器,点击左侧「触发器」菜单
  • 点击右下角「添加触发器」
  • 选择要运行的函数为onApprove
  • 选择事件来源为「电子表格」
  • 选择事件类型为「编辑时」
  • 保存后按照提示授权脚本的访问权限即可

注意事项

  • 如果你的表格列顺序和示例不同,需要对应修改代码中getRange(row, 列索引)的第二个参数
  • 务必替换代码中替换为你的Google Doc ID为实际文档的ID
  • 如果不需要同步标记,可删除对应判断和赋值逻辑,但可能会出现重复写入的问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 12:21:04