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

Google表单OTP验证脚本报错修复求助(TypeError: e.response.withItemResponses is not a function)

Google表单OTP验证脚本报错修复求助(TypeError: e.response.withItemResponses is not a function)

嘿,我仔细看了你的问题和报错日志,核心问题其实很直白:Google Apps Script里的FormResponse对象根本没有withItemResponses()这个方法,你错误地用了这个不存在的链式调用,直接导致了TypeError报错。

先给你修复好的完整代码,主要改动就是把所有e.response.withItemResponses().setConfirmationMessage(...)改成直接调用e.response.setConfirmationMessage(...):

function onSubmit(e) {
  Logger.log("onSubmit triggered");

  var itemResponses = e.response.getItemResponses();
  var emailItem =
    itemResponses.find(item =>
                       item.getItem().getTitle() == "Your Email Address"); // Replace with your email question title

  if (!emailItem) {
    Logger.log("ERROR: Email question NOT found!");
    itemResponses.forEach(itemResponse => Logger.log("Item Title: " + itemResponse.getItem().getTitle()));
    return;
  }

  var email = emailItem.getResponse();
  Logger.log("Email: " + email);

  var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

  if (!emailRegex.test(email)) {
    Logger.log("ERROR: Invalid email address: " + email);
    // 修复:移除不存在的withItemResponses()调用
    e.response.setConfirmationMessage("Invalid email address. Please enter a valid email.");
    return;
  }

  // Get the edit URL
  var editUrl = e.response.getEditResponseUrl();
  Logger.log("Edit URL: " + editUrl);

  var cache = CacheService.getScriptCache();
  cache.put("pending_email", email, 300); // Store email for 5 minutes
  cache.put("edit_url", editUrl, 300); // Store edit URL

  // Generate and send OTP
  var otp = generateOTP();
  cache.put(email + "_otp", otp, 300); // Store OTP for 5 minutes
  sendEmail(email, otp, editUrl); // Send email with OTP and edit URL


  // 修复:移除不存在的withItemResponses()调用
  e.response.setConfirmationMessage("Thank you for submitting your email. Please check your inbox (and spam folder) for the OTP. Click the link in the email to complete your submission.");

  return;
}

function onFormSubmit(e) {
  Logger.log("onFormSubmit triggered");

  var itemResponses = e.response.getItemResponses();
  var emailItem = itemResponses.find(item => item.getItem().getTitle() == "Your Email Address"); // Replace with your email question title
  var email = emailItem.getResponse();
  var otpQuestion = itemResponses.find(item => item.getItem().getTitle() == "OTP"); // Replace with your OTP question title
  var submittedOtp = otpQuestion ? otpQuestion.getResponse() : null;

  var cache = CacheService.getScriptCache();
  var cachedOtp = cache.get(email + "_otp");

  if (submittedOtp && cachedOtp && submittedOtp == cachedOtp) {
    cache.put(email + "_verified", true, 3600);
    // 修复:移除不存在的withItemResponses()调用
    e.response.setConfirmationMessage("Your email has been verified. Thank you for your submission!");
  } else if (submittedOtp) {
    // 修复:移除不存在的withItemResponses()调用
    e.response.setConfirmationMessage("Incorrect OTP. Please try again.");
  } else {
    // 修复:移除不存在的withItemResponses()调用
    e.response.setConfirmationMessage("Please enter the OTP.");
  }
}

function generateOTP() {
  var otp = Math.floor(100000 + Math.random() * 900000); // 6-digit OTP
  return otp.toString();
}

function sendEmail(email, otp, editUrl) { // Add editUrl parameter
  var subject = "Your OTP for Google Form Verification";
  var body = "Your OTP is: " + otp + "\n\nPlease click the following link to complete your submission: " + editUrl; // Include editUrl

  try {
    MailApp.sendEmail(email, subject, body);
    Logger.log("OTP email sent successfully to: " + email);
  } catch (error) {
    Logger.log("ERROR sending OTP email: " + error);
  }
}

额外要注意的几个点:

  1. 触发器冲突问题:从你提供的截图看,你设置了两个“表单提交”触发,分别对应onSubmit和onFormSubmit,这会导致每次用户提交表单时两个函数都会被触发,很容易引发逻辑混乱。建议你保留一个函数即可,或者调整触发逻辑区分首次提交和OTP验证提交。
  2. OTP字段设置:确保你Google表单里的“OTP”字段允许编辑,这样用户通过编辑链接返回时才能正常填写OTP。
  3. 缓存有效期:你设置的缓存是5分钟,这个时长比较合理,如果担心用户可能需要更长时间,可以把300(单位:秒)这个参数适当调大。

你提供的错误日志:

Head    onSubmit    Trigger    Feb 24, 2025, 3:22:24 PM    1.062 s Failed

Cloud logs
Feb 24, 2025, 3:22:24 PM    Info    onSubmit triggered
Feb 24, 2025, 3:22:24 PM    Info    Email: ahkkcr@hotmail.com
Feb 24, 2025, 3:22:25 PM    Info    Edit URL: https://docs.google.com/forms/d/e/1FAIpQLSdtWPCzDm92Tah34gnj4UZHiZgQ22L6nQPxC5LrnwDCoUphsA/viewform?edit2=2_ABaOnud9q2OpMIXEeE6nvxwkIxPCr6JbP-5y9OQJoLoNKEN2va_vWQLR-nQZ8QauOy8iPHw
Feb 24, 2025, 3:22:25 PM    Info    OTP email sent successfully to: ahkkcr@hotmail.com
Feb 24, 2025, 3:22:25 PM    Error    TypeError: e.response.withItemResponses is not a function
                                        at onSubmit(Code:39:14)

Head    onFormSubmit    Trigger    Feb 24, 2025, 3:22:22 PM    1.267 s Failed

Cloud logs
Feb 24, 2025, 3:22:22 PM    Info    onFormSubmit triggered
Feb 24, 2025, 3:22:23 PM    Error    TypeError: e.response.withItemResponses is not a function
                                        at onFormSubmit(Code:62:16)

>TypeError: e.response.withItemResponses is not a function
                                    at onFormSubmit(Code:62:16)

触发器截图:

Triggers

备注:内容来源于stack exchange,提问作者Karthik C

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:37:59