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

Google Apps Script中MailApp.sendEmail收件人取值异常求助

问题分析与解决方案

我一眼就揪出问题所在啦!你的代码里有两个关键错误,直接导致eventObject.email无法正确获取,进而触发了“无收件人”的邮件发送报错:

1. return eventObject的位置完全错误

你把return eventObject写在了for循环的内部,这意味着第一次循环迭代就会直接终止函数并返回eventObject,后面的表单字段(包括你需要的email)根本没机会被赋值到eventObject里!最终eventObject.email就是undefined,邮件自然发不出去。

2. MailApp.sendEmail的位置不合理

你把发送邮件的代码放在了循环内部,还在return语句之后——这行代码其实永远不会被执行到,因为前面的return已经让函数提前退出了。


修正后的完整代码

我帮你调整了代码结构,把return和sendEmail的位置都改对了:

//Load the Moment.js library once.
var moment = Moment.load();
var GLOBAL = {
 //the id of the form we will use to create calendar events
 formId : "arbritrary form ID",
 //the id of the calendar we will create events on
 calendarId : "arbritrary calendar ID",
 //a mapping of form item titles to sections of the calendar event
 formMap : {
 eventTitle: "Person Name and Organisation",
 startTime : "Start Date / Time",
 endTime: "End Date / Time",
 description: "Description of work / Additional Notes",
 location: "Location of Work and/or Room ID",
 email: "Approver Email Address",
 },
}
function onFormSubmit() {
 var eventObject = getFormResponse();
 var event = createCalendarEvent(eventObject);
}
function getFormResponse() {
 // Get a form object by opening the form using the
 // form id stored in the GLOBAL variable object
 var form = FormApp.openById(GLOBAL.formId),
 //Get all responses from the form.
 //This method returns an array of form responses
 responses = form.getResponses(),
 //find the length of the responses array
 length = responses.length,
 //find the index of the most recent form response
 //since arrays are zero indexed, the last response
 //is the total number of responses minus one
 lastResponse = responses[length-1],
 //get an array of responses to every question item
 //within the form for which the respondent provided an answer
 itemResponses = lastResponse.getItemResponses(),
 //create an empty object to store data from the last
 //form response
 //that will be used to create a calendar event
 eventObject = {};
 //Loop through each item response in the item response array
 for (var i = 0, x = itemResponses.length; i<x; i++) {
 //Get the title of the form item being iterated on
 var thisItem = itemResponses[i].getItem().getTitle(),
 //get the submitted response to the form item being
 //iterated on
 thisResponse = itemResponses[i].getResponse();
 //based on the form question title, map the response of the
 //item being iterated on into our eventObject variable
 //use the GLOBAL variable formMap sub object to match
 //form question titles to property keys in the event object
 switch (thisItem) {
 case GLOBAL.formMap.eventTitle:
 eventObject.title = thisResponse;
 break;
 case GLOBAL.formMap.startTime:
 eventObject.startTime = thisResponse;
 break;
 case GLOBAL.formMap.endTime:
 eventObject.endTime = thisResponse;
 break;
 case GLOBAL.formMap.description:
 eventObject.description = thisResponse;
 break;
 case GLOBAL.formMap.location:
 eventObject.location = thisResponse;
 break;
 case GLOBAL.formMap.email:
 eventObject.email = thisResponse;
 break;
 }
 }
 // 循环结束后再发送邮件,此时eventObject已收集所有字段
 MailApp.sendEmail({ 
   to: eventObject.email, 
   subject: "A new request has been submitted", 
   htmlBody: "Please review at the following link: arbritrary link" 
 });
 // 最后返回eventObject给调用方
 return eventObject;
}

关键修改点说明

  • 把return eventObject从for循环内部移到循环结束之后,确保所有表单字段都被遍历并赋值到eventObject中。
  • 把MailApp.sendEmail也移到循环结束之后,此时eventObject.email已经正确获取到表单里填写的审批人邮箱,不会再出现“无收件人”的错误。
  • 现在函数会先完整遍历所有表单响应、收集好所有数据,再发送邮件,最后返回eventObject给onFormSubmit函数使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 13:43:15