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

Google Apps Script表单提交触发Cannot read property '0' of undefined错误求助

问题排查与修复方案

核心错误原因

  • 变量名引用错误:onGoodsOutboundingFormSubmit函数循环时调用了未定义的lpnList[key],实际应该使用当前函数内声明的lpnListTwo[key],这是触发TypeError: Cannot read property '0' of undefined的直接原因
  • 工作表操作逻辑错误:insertRowWithValues函数强制将活跃工作表设为lpnDeactivationSheet,但传入的inputSheet可能是LPN流转记录表,会导致两个表单提交的数据都写入同一张表,出现LPN记录丢失、错位的问题
  • 全局变量污染:lpnList、lpnListTwo未添加let/const声明,默认挂载为全局变量,多表单并发提交时会出现变量值覆盖,导致随机丢失LPN记录
  • 表单字段取值错误:event.namedValues返回的每个字段值都是数组格式,直接传入数组写入表格会显示乱码,所有字段都需要取[0]获取实际文本值

修复后完整代码

// 表单提交触发入口,根据提交的工作表分发处理逻辑
function formSubmit(e){
  Logger.log(e.range.getSheet().getName());
  switch(e.range.getSheet().getName()){
    case "3 - LPN Movement Form":
      onLPNMovementFormSubmit(e);
      break;
    case "4 - Outbounding Form":
      onGoodsOutboundingFormSubmit(e);
      break;
  }
}

// 全局工作表实例
const wms = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1097mFk6BTfopXwZ5fSX3h0YpLzaW2xs7YILwck4-6HA/');
const lpnDeactivationSheet = wms.getSheetByName("4 - Outbounding (LPN Deactivation)");
const lpnMovementSheet = wms.getSheetByName("3 - LPN Movement (Individual)");

// 向目标工作表第二行插入单条数据
function insertRowWithValues(inputSheet , dataArray){    
  inputSheet.insertRows(2, 1);
  inputSheet.getRange(2,1,1,dataArray.length)
    .setValues([dataArray]);
}

// 按换行符拆分LPN文本
function splitLines(inputText){  
  return inputText.split(/\r?\n/);
}

// 表单3提交处理逻辑
function onLPNMovementFormSubmit(event) {  
  const form = event.namedValues;
  // 增加空值校验避免报错
  if(!form['License Plate Number (LPN)'] || !form['License Plate Number (LPN)'][0]) return;
  const lpnList = splitLines(form['License Plate Number (LPN)'][0]);

  for (const key in lpnList){      
    insertRowWithValues(lpnMovementSheet, 
      [lpnList[key],
      form['Origin Location ID'][0],
      form['Destination Location ID'][0],
      form['Email address'][0],
      form['Timestamp'][0]]);
  }
}

// 表单4提交处理逻辑
function onGoodsOutboundingFormSubmit(event) {  
  const formTwo = event.namedValues;
  // 增加空值校验避免报错
  if(!formTwo['License Plate Number (LPN)'] || !formTwo['License Plate Number (LPN)'][0]) return;
  const lpnListTwo = splitLines(formTwo['License Plate Number (LPN)'][0]);
  
  for (const key in lpnListTwo){
    insertRowWithValues(lpnDeactivationSheet, 
      [lpnListTwo[key],
      formTwo['Outbound Request ID'][0],
      formTwo['PO Number'][0],
      formTwo['Goods Condition'][0],
      formTwo['Goods Condition Remarks'][0],
      formTwo['Email address'][0],
      formTwo['Timestamp'][0]]);
  }
}

额外优化建议

  • 批量写入代替逐行插入:当前逻辑每次拆分出1个LPN就插入1行,多LPN场景下会触发多次IO操作,效率低且容易引发并发冲突,可以先组装好所有要插入的行数据,一次性批量插入到表首
  • 替换数组遍历方式:使用for of遍历LPN数组,避免for in遍历可能拿到原型链额外属性的异常问题
  • 加锁处理并发:如果表单提交频率较高,可以使用LockService加锁,避免同时提交时出现行插入冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 22:15:05