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

求助:将Google Form触发的行移动脚本替换onEdit触发实现

Google表单提交触发行移动解决方案

需求说明

将「Form responses」工作表中表单提交的行,根据第2列的值(如IN/OUT),移动到预先定义或单元格指定的对应工作表中。

原onEdit参考代码

function onEdit(e) {
  const ss = e.source;
  const range = e.range;
  const sheet = range.getSheet();
  if (sheet.getName() !== "Form responses") return;
  const targetSheetName = range.getValue();
  const targetSheet = ss.getSheetByName(targetSheetName);
  if (!targetSheet) return;
  const row = range.getRow();
  const rowData = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
  targetSheet.appendRow(rowData);
  sheet.deleteRow(row);
}

修改后的表单提交触发脚本

方式1:预先定义工作表映射

function onFormSubmit(e) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const formSheet = ss.getSheetByName("Form responses");
  const rowNum = e.range.getRow();
  const rowData = formSheet.getRange(rowNum, 1, 1, formSheet.getLastColumn()).getValues()[0];
  
  // 获取第2列的值(数组索引从0开始,对应表格的B列)
  const targetKey = rowData[1];
  
  // 预先定义关键字与工作表的映射
  const sheetMapping = {
    "IN": "入库记录表",
    "OUT": "出库记录表"
  };
  
  const targetSheetName = sheetMapping[targetKey];
  if (!targetSheetName) {
    // 无匹配时存入默认表(需预先创建)
    const defaultSheet = ss.getSheetByName("未分类记录");
    if (defaultSheet) defaultSheet.appendRow(rowData);
    formSheet.deleteRow(rowNum);
    return;
  }
  
  const targetSheet = ss.getSheetByName(targetSheetName);
  if (!targetSheet) return;
  
  targetSheet.appendRow(rowData);
  formSheet.deleteRow(rowNum);
}

方式2:通过单元格配置映射关系

如果需要灵活修改映射,可创建「配置表」(A列存关键字如IN/OUT,B列存对应工作表名称),使用以下脚本:

function onFormSubmit(e) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const formSheet = ss.getSheetByName("Form responses");
  const rowNum = e.range.getRow();
  const rowData = formSheet.getRange(rowNum, 1, 1, formSheet.getLastColumn()).getValues()[0];
  
  const targetKey = rowData[1];
  
  // 从配置表读取映射关系
  const configSheet = ss.getSheetByName("配置表");
  const configData = configSheet.getDataRange().getValues();
  const sheetMapping = {};
  configData.forEach(row => {
    if (row[0] && row[1]) sheetMapping[row[0]] = row[1];
  });
  
  const targetSheetName = sheetMapping[targetKey];
  if (!targetSheetName) {
    const defaultSheet = ss.getSheetByName("未分类记录");
    if (defaultSheet) defaultSheet.appendRow(rowData);
    formSheet.deleteRow(rowNum);
    return;
  }
  
  const targetSheet = ss.getSheetByName(targetSheetName);
  if (!targetSheet) return;
  
  targetSheet.appendRow(rowData);
  formSheet.deleteRow(rowNum);
}

触发器设置步骤

  1. 打开Google表格,点击顶部「扩展程序」→「Apps脚本」进入脚本编辑器。
  2. 复制上述代码到编辑器,保存项目(命名如「FormRowMover」)。
  3. 点击左侧「触发器」图标(时钟样式),点击「添加触发器」。
  4. 配置触发器:
    • 选择函数:onFormSubmit
    • 选择部署类型:「Head」
    • 事件源:「电子表格」
    • 事件类型:「表单提交」
  5. 保存并授权脚本权限(首次运行需授权,按提示操作即可)。

注意事项

  • 确保「Form responses」「配置表」「入库记录表」等工作表名称与脚本中完全一致(区分大小写)。
  • 测试时可手动提交表单,验证行是否正确移动到目标工作表。
  • 如果不需要默认表,可删除对应代码块。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:13:33